温故而知新,UI学习中的大部分控件及常用的基础都整理了一下,很长~~~~~~~~~很长!!!!!!!...
生命周期,生命周期,生命周期,重要的事要說三遍,戰五渣的我最常用的好像只有Viewwillappear吧
?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
??? //得到手機主屏幕尺寸
??? self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
??? // Override point for customization after application launch.
??? self.window.backgroundColor = [UIColor whiteColor];
??? //把self.window 作為主窗體在界面顯示出來
??? [self.window makeKeyAndVisible];
??? return YES;
}
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark View
- (void)View{
??? //UIWindow是應用程序的窗體,所有的界面都顯示在UIWindow上
??? //一般情況下一個應用程序只有一個UIWindow,它類似與畫布,所有的界面都在上面描繪
??? //initWithFrame 傳給它一個顯示的區域大小,作為根都是屏幕大小
??? //使用UIScreen 獲取屏幕的大小,bounds就是屏幕的寬高
??? //CGRect描繪一個矩形,指定原點和寬高即可
?? ?
??? CGRect rect = [UIScreen mainScreen].bounds;
??? NSLog(@"screen bounds%@",NSStringFromCGRect(rect));
??? //iphone的屏幕坐標原點在左上角,x軸向右增加,y軸向下增加
??? self.window = [[UIWindow alloc]initWithFrame:rect];
??? //設置window的背景色,backgroundColor
??? //在ios中使用UIColor代表顏色,也可以指定red green blue三元色來指定
??? self.window.backgroundColor = [UIColor orangeColor];
?? ?
??? //UIView :view 是代表視圖,一個可見的顯示區域
??? UIView *view1 = [[UIView? alloc]initWithFrame:CGRectMake(10, 20, 200, 200)];
??? view1.backgroundColor = [UIColor redColor];
?? ?
??? //所有要顯示的控件都必須最終添加到window上,才能顯示出來、
??? //addSubView:添加一個子視圖,這樣view1就和window發生了關聯
??? //window就是view1的父視圖,view1就是子視圖
??? [self.window addSubview:view1];
?? ?
??? //指定frame時,原點的坐標系是父視圖的坐標系
??? UIView *subView1 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
??? subView1.backgroundColor = [UIColor blueColor];
??? //把subView1添加到view1上
??? [view1 addSubview:subView1];
?? ?
??? //根據自身的寬高加中心點的方式指定一個顯示區域
??? UIView *view2 = [[UIView alloc]init];
??? //指定bounds時,x,y坐標由于是參考自身的坐標系所以永遠為0,只需要指定寬高即可
??? //center 中心點
??? view2.bounds = CGRectMake(0, 0, 150, 150);
??? //center中心點的坐標系是參考父視圖
??? view2.center = CGPointMake(200, 300);
??? view2.backgroundColor = [UIColor blackColor];
??? //把通過指定bounds和center界定出來的view,添加到windows上
??? [self.window addSubview:view2];
??? //frame 本身就是靠bounds + center 計算出來的
????? [self.window bringSubviewToFront:view];
??? //把某一個view放到最下面
??? //[self.window sendSubviewToBack:<#(UIView *)#>];
??? //[self.window insertSubview:(UIView *) aboveSubview:(UIView *)];
??? //[self.window insertSubview:<#(UIView *)#> belowSubview:<#(UIView *)#>];
??? //[self.window insertSubview:<#(UIView *)#> atIndex:<#(NSInteger)#>]
??? //指定角的半徑
??? View.layer.cornerRadius = 50;
??? View.layer.borderColor = [UIColor redColor].CGColor;
??? View.layer.borderWidth = 2;
?? ?
??? //layer:
??? //1:每一個view都會對應一個layer,layer幫我們做的事情是渲染view的內容
??? //2:layer中有一些屬性是view沒有的,這樣我們可以對UIView做進一步的設置
??? //3:所有發生在view上的動畫,都是layer完成的。
?? ?
??? //UIView的作用:1:顯現layer渲染出來的界面,2:與用戶進行交互
?? ?
??? //UIViewAutoresizingFlexibleLeftMargin 子視圖的左邊根據父視圖的大小變化而變化
??? //UIViewAutoresizingFlexibleWidth 子視圖的寬度根據父視圖的大小變化而變化
??? //& | !
??? //autoresizingMask 設置子視圖怎么根據父視圖的大小變化而變化的屬性
??? blueView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
??? //subviews 把自己的子視圖放到數組中
??? NSArray *subViews = self.window.subviews;
??? NSLog(@"%@",subViews);
?? ?
??? //交換兩個視圖的次序,次序既為數組中的下表
??? [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
??? //----------------------------------------------------------------------
??? //UIview頁面翻轉動畫
?? ?
??? //??? UIView *redView = [self.window viewWithTag:100];
??? //??? CGRect rect = redView.frame;
??? //
??? //??? [UIView animateWithDuration:1 animations:^{
??? //??????? CGRect newRect = CGRectInset(rect, -10, -10);
??? //??????? redView.frame = newRect;
??? //??? }];
?? ?
??? //對最上層
??? NSArray *subViewArray = self.window.subviews;
??? UIView *topView = [subViewArray objectAtIndex:1];
?? ?
??? //動畫塊,注意必須成對的出現
??? //animationID 字符串表識一個annimation
??? //context:這里可以傳遞動畫結束時還需要使用的參數
??? [UIView beginAnimations:nil context:nil];
?? ?
??? //設置動畫行進的速度曲線
??? //??? typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
??? //??????? UIViewAnimationCurveEaseInOut,???????? // slow at beginning and end
??? //??????? UIViewAnimationCurveEaseIn,??????????? // slow at beginning
??? //??????? UIViewAnimationCurveEaseOut,?????????? // slow at end
??? //??????? UIViewAnimationCurveLinear
??? //??? };
?? ?
??? [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
?? ?
??? //整個動畫的持續時間
??? [UIView setAnimationDuration:1];
?? ?
??? //UIViewAnimationTransitionCurlUp 向上翻頁
??? //UIViewAnimationTransitionCurlDown 向下
??? //UIViewAnimationTransitionFlipFromLeft? 從左邊翻轉
??? //UIViewAnimationTransitionFlipFromRight 從右邊翻轉
?? ?
??? //forView 是在哪一個view上做動畫
??? //catche 是否要緩存最初捕獲的頁面,一般寫為YES,提高效率
??? [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.window cache:YES];
?? ?
??? //設置動畫的重復次數
??? [UIView setAnimationRepeatCount:1];
?? ?
??? //是否自動翻轉:YES 自動翻轉
??? //[UIView setAnimationRepeatAutoreverses:YES];
?? ?
??? //設置動畫的代理
??? [UIView setAnimationDelegate:self];
??? //設置動畫結束時調用的方法
??? //[UIView setAnimationDidStopSelector:@selector(annimationStop)];
?? ?
??? //??? [UIView beginAnimations:<#(NSString *)#> context:<#(void *)#>];
??? //??? [UIView commitAnimations];
?? ?
??? [UIView commitAnimations];
?? ?
??? //把上層的view 清掉
??? [topView removeFromSuperview];
}
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark UIimageView
- (void)imageView{
?? ?
}
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark Button
- (void)Button{
??? //通過系統提供的使用可以生成系統提供的按鈕
??? UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
??? //設置普通狀態下的按鈕的文本
??? [button setTitle:@"按鈕" forState:UIControlStateNormal];
??? //設置文本的顏色,狀態為Normal
??? [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
??? //高亮狀態下的文本的顏色
??? [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
??? //使用[UIImage imageNamed:@""] 通過指定圖片名稱的方式來生成一個UIImage
??? //@2x.png
??? //首先UIImage imageNamed如果沒指定圖片的后綴,默認格式png
??? //@2x 意思是2倍像素
??? //imageNamed :文件名,首先檢查是否為高清屏(視網膜屏),如果是就找文件
??? //文件名@2x.png,如果沒有對應的@2x的圖片,就使用指定的文件
??? UIImage *imageNormal = [UIImage imageNamed:@"bgCommon"];
??? UIImage *imageHightLight = [UIImage imageNamed:@"bgSelected"];
?? ?
??? //給button設置背景圖片,normal狀態下
??? //注意:button會對圖片根據button的區域大小進行拉伸,如果二者的寬高比不一致圖片可能變形
??? [button setBackgroundImage:imageNormal forState:UIControlStateNormal];
?? ?
??? //指定button在highlight模式下的背景圖片
??? [button setBackgroundImage:imageHightLight forState:UIControlStateHighlighted];
??? //當指定的事件(Event)發生時,調用target的selector
?? ?
??? //UIControlEventTouchDown 一旦按鈕被按下就觸發的buttonClickDown
??? [button addTarget:self action:@selector(buttonClickDown:) forControlEvents:UIControlEventTouchDown];
?? ?
??? //UIControlEventTouchUpInside :當手指按下后開始起來,inside意思是手指在UIButton的區域范圍只你
??? //當button的事件UIControlEventTouchUpInside發生時,調用self 的buttonClick的方法
??? [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark View
- (void)Lable{
??? UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 200, 50)];
??? label.backgroundColor = [UIColor greenColor];
?? ?
??? //指定UILabel顯示的文本
??? label.text = @"Hello world";
?? ?
??? //指定字體顏色
??? label.textColor = [UIColor redColor];
?? ?
??? //設置字體大小,默認字體大小是17
??? label.font = [UIFont systemFontOfSize:30];
?? ?
??? //指定字體
??? //label.font = [UIFont fo];
??? //指定字體的左右對齊方式
??? //NSTextAlignmentLeft??? 靠左 默認
??? //NSTextAlignmentCenter? 居中
??? //NSTextAlignmentRight?? 靠右
??? label.textAlignment = NSTextAlignmentCenter;
??? //設置為0,不限制 lable 行數
??? label.numberOfLines? = 0;
??? label.text = @"Application windows are expected to have a root view controller at the end of application launch";
?? ?
??? //設置adjustsFontSizeToFitWidth 的屬性為yes,可以自動調整字體的大小以適應整個的區域
??? label.adjustsFontSizeToFitWidth = YES;
??? //自動計算要顯示文本需要的區域大小
??? [label sizeToFit];
??? //如果需要自己控制換行,可以手動添加\n
??? label.text = @"窗前明月光\n疑似地上霜\n舉頭望明月\n低頭思校長";
??? int index = 0;
??? //得到所有的字體家族(一個字體家族包含的字體可能由斜體,粗體,下劃線)
??? //換行的模式,意識根據什么來進行換行
??? //NSLineBreakByWordWrapping 以單詞為單位進行換行
??? //NSLineBreakByCharWrapping 以字符為單位進行換行
??? //截斷的模式
??? //NSLineBreakByTruncatingHead? //頭部截斷
??? //NSLineBreakByTruncatingTail? //尾部截斷
??? //NSLineBreakByTruncatingMiddle //中間不顯示,使用...代替
??? NSArray *fontFamily = [UIFont familyNames];
??? for (NSString *family in fontFamily) {
?????? ?
??????? //得到字體家族中字體
??????? NSArray *fontArray = [UIFont fontNamesForFamilyName:family];
??????? for (NSString *fontName in fontArray) {
??????????? NSLog(@"%@",fontName);
??????????? UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, index*30 + 20, 200, 30)];
??????????? label.text = fontName;
??????????? label.font = [UIFont fontWithName:fontName size:15];
??????????? [self.window addSubview:label];
??????????? index++;
??????? }
??? }
?? ?
}
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark Timer計時器動畫
- (void)Timer{
??? //Time Interval:時間間隔,指定每隔多長時間定時器觸發一次,單位是秒
??? //target + selector? :當定時器觸發時,調用的方法
??? //userInfo:是額外的參數,通常設置為nil
??? //repeats:指定定時器是否一直重復,YES 一直重復 NO:不重復只觸發一次
?? ?
??? //timer會對傳入的targe的進行保留,(retainCount+1),防止timer觸發時,target釋放掉,這里對self的retainCount加一
??? //等到timer停掉的時候,才把self的retainCount減一`
??? //定義timer每隔0.1秒觸發一次
??? _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
??? //由于timer對self有保留,dealloc不會被調用
??? //不要在dealloc中停掉timer
??? //讓定時器失效,真個timer停掉
??? [_timer invalidate];
??? _timer = nil;
??? [UIView animateWithDuration:5 animations:^{
??????? //這里寫動畫的最終的狀態,也就是雪花的最終位置
??????? CGRect endFrame = CGRectMake(endXPos, 667-SNOW_H, SNOW_W, SNOW_H);
??????? snowImageView.frame = endFrame;
??????? snowImageView.alpha = 1.0;
??? } completion:^(BOOL finished) {
??????? //把自己從自己父視圖上刪除
??????? //[snowImageView removeFromSuperview];
?????? ?
??????? //模擬雪花被融化掉的動畫
??????? [UIView animateWithDuration:1 animations:^{
??????????? //alpha指透明度,1完全不透明
??????????? //0 是完全透明
??????????? //對于一個UIView默認的alpha值1
??????????? snowImageView.alpha = 0.0;
??????? } completion:^(BOOL finished) {
??????????? //這里正對alpha的動畫已經結束,這是可以把視圖從父視圖上刪除
??????????? [snowImageView removeFromSuperview];
??????? }];
??? }];
}
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark Timer彈簧動畫
- (void)vidag{
??? for (int index = 0; index < 4; index++) {
??????? UIView *ballView = [self.window viewWithTag:index+BASE_TAG];
?????? ?
??????? //1:duration :動畫的持續時間,單位秒
??????? //2:delay:動畫延遲多長時間開始 單位秒
??????? //3:Damping 阻尼,取值范圍0~1,一般 < 0.7 晃動比較厲害,為1不會有彈簧效果
??????? //4:Velocity:初始速度,單位 points/秒,一般設置為0就可以了
??????? //5:options:動畫的一些屬性,設置速度的曲線,或者翻轉的方式
??????? //6:animations :動畫內容
??????? //7:complete:動畫結束時回調的block
?????? ?
??????? [UIView animateWithDuration:1 delay:0.2*index usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
??????????? //??????????? CGPoint center = ballView.center;
??????????? //??????????? CGPoint newCenter = CGPointMake(center.x, center.y - 300);
??????????? //??????????? ballView.center = newCenter;
?????????? ?
??????????? //??????????? struct CGAffineTransform {
??????????? //??????????????? CGFloat a, b, c, d;
??????????? //??????????????? CGFloat tx, ty;
??????????? //??????????? };
?????????? ?
??????????? //ad縮放 bc旋轉 tx,ty位移,基礎的2D矩陣
??????????? //旋轉,參數指定為弧度,M_PI <-> 180
??????????? //CGAffineTransformMakeRotation(<#CGFloat angle#>)
?????????? ?
??????????? //縮放 ,sx:指x軸縮放的比例,sy 在y軸上的縮放比例
??????????? //CGAffineTransformMakeScale(<#CGFloat sx#>, <#CGFloat sy#>)
??????????? //平移 tx:在x軸上平移? ty 是在y上平移
??????????? //CGAffineTransformMakeTranslation(<#CGFloat tx#>, <#CGFloat ty#>)
?????????? ?
??????????? //指定在y軸上 平移
??????????? ballView.transform = CGAffineTransformMakeTranslation(0, -400);
?????????? ?
??????? } completion:^(BOOL finished) {
?????????? ?
??????? }];
??? }
??? for (int index = 0; index < 4; index++) {
??????? UIView *ballView = [self.window viewWithTag:index+BASE_TAG];
?????? ?
??????? [UIView animateWithDuration:1 delay:0.2*index usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
??????????? //??????????? CGPoint center = ballView.center;
??????????? //??????????? CGPoint newCenter = CGPointMake(center.x, center.y + 300);
??????????? //??????????? ballView.center = newCenter;
?????????? ?
??????????? //CGAffineTransformIdentity 指定一個矩陣,
??????????? //讓你的view回到最原始的狀態,沒有縮放,沒有旋轉,沒有平移
??????????? ballView.transform = CGAffineTransformIdentity;
??????? } completion:^(BOOL finished) {
?????????? ?
??????? }];
??? }
}
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark Notification 通知中心
- (void)notification{
??? _boy1 = [[Boy alloc]init];
?? ?
??? //把boy添加或注冊到通知中心,關注的事件是name中標示的字符串
??? //當其他對象發布通知時,通知中心中關注次事件的對象都會得到通知
??? //自動調用selector方法,即[boy findFriend:]
?? ?
??? //注意:通知中心不會把你注冊對象保留,當boy對象釋放的時候,一定要把自己從通知中心中移除
??? //通知中心:不會對你添加的對象進行比較,可能存在一個對象多次添加到通知中心的情況,添加幾次就會調用幾次
?? ?
??? //最有一個參數object:nil? 不管誰發送的findFriendNotification 通知自己都會接受
??? //如果object,非nil,指定為一個對象,意思是只關注該對象發送的通知
?? ?
??? //為了避免多次注冊到通知中心,先把_boy1從通知中心中移除,然后再添加
??? [[NSNotificationCenter defaultCenter] removeObserver:_boy1];
??? [[NSNotificationCenter defaultCenter] addObserver:_boy1 selector:@selector(findFriend:) name:@"findFriendNotification" object:nil];
??? //通知傳值一般寫法
??? //發通知方:
??? //postNotificationName:通知的名字 object:發通知的人,一般為 self? userInfo:要發送的信息是個字典(也就是要傳的值)
??? [NSNotificationCenter defaultCenter]postNotificationName:<#(NSString *)#> object:<#(id)#> userInfo:<#(NSDictionary *)#>];
??? //接收方:
??? //addObserver: 注冊人一般為 self select:收到通知后調用的方法;name:為通知名,要與發通知的名字一樣 object:后面寫就收誰的通知(即發通知人的名字),寫 nil意思是,只要通知名一樣都接收
??? [NSNotificationCenter defaultCenter]addObserver:<#(id)#> selector:@selector(resiveNotification:) name:<#(NSString *)#> object:<#(id)#>];
}
??? //調用一下方法來得到傳過來的值用字典接收
- (void)resiveNotification:(NSNotification*)nitification{
??????? NSDictionary *dic =[nitification userInfo];
??? }
??? //要注意,必須先注冊過才能收到通知,對于平行頁面,如 tabBar 里的各個界面用通知傳值,后面的界面還沒有生成,也就沒有注冊,可以把注冊方法寫到 init 中,可以實現,事先注冊,可以直接得到傳值
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark TextField 輸入框
- (void)TextField{
??? UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(85,20, 200, 50)];
??? //設置邊框類型
//??? UITextBorderStyleNone,
//??? UITextBorderStyleLine,
//??? UITextBorderStyleBezel,
//??? UITextBorderStyleRoundedRect
??? textField.borderStyle = UITextBorderStyleRoundedRect;
??? //暗文
??? textField.secureTextEntry = YES;
?? ?
??? //設置底部背景,通常和邊框類型為None的一起使用,對于圓角類型的沒有效果
??? textField.background = [UIImage imageNamed:@"bg.png"];
?? ?
??? //placeholder 占位符,開始編輯后消失,主要起到提示的作用,設置后灰色顯示提示
??? textField.placeholder = @"請輸入";
?? ?
??? //編輯后是否顯示編輯的刪除標識,可以把文本一次性的刪除
??? //
??? //UITextFieldViewModeNever,?????????? 永不顯示
??? //UITextFieldViewModeWhileEditing,??? 當編輯的時候顯示
??? //UITextFieldViewModeUnlessEditing,?? 當不編輯的時候顯示
??? //UITextFieldViewModeAlways?????????? 一直顯示
??? textField.clearButtonMode = UITextFieldViewModeWhileEditing;
?? ?
??? //橫向居中顯示
??? textField.textAlignment = NSTextAlignmentCenter;
?? ?
??? //調整文本垂直方向靠上,居中,靠下
??? //textField.contentVerticalAlignment
?? ?
??? //是指編輯框左邊的view和右邊的view
??? UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"qq.png"]];
??? imageView.frame = CGRectMake(0, 0, 30, 30);
??? textField.leftView = imageView;
??? //leftViewMode 顯示的模式
??? textField.leftViewMode = UITextFieldViewModeAlways;
?? ?
??? textField.rightView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"test.png"]];
??? textField.rightViewMode = UITextFieldViewModeAlways;
?? ?
??? //當再次編輯時,是否把上次編輯的內容清掉
??? textField.clearsOnBeginEditing = YES;
?? ?
??? //鍵盤類型
??? textField.keyboardType = UIKeyboardTypeDefault;
?? ?
??? //設置return的類型,
??? textField.returnKeyType = UIReturnKeyEmergencyCall;
?? ?
??? //設置textField的代理,這樣再開始編輯。
??? //結束編輯調用相應的代理方法
??? textField.delegate = self;
?? ?
??? //是否自動糾錯
??? textField.autocorrectionType = NO;
?? ?
??? //讓輸入框成為第一響應者,這樣鍵盤自動彈出
??? //所謂第一響應者就是鍵盤的輸入和該控件結合再一起
??? [textField becomeFirstResponder];
?? ?
??? //要讓鍵盤消失,就是讓textField 不是第一相應者,這樣鍵盤就會消失
??? [textField resignFirstResponder];
}
??? //-------------------------------------------------------------------------------------------
??? //UITextFiel的代理方法
??? - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
??? {
??????? //編輯框是否可以開始編輯,返回NO,編輯框沒法編輯
??????? return YES;
??? }
?? ?
??? - (void)textFieldDidBeginEditing:(UITextField *)textField;?????????? // became first responder
??? {
??????? _currentEditingTextField = textField;
??????? NSLog(@"編輯框開始編輯");
??? }
?? ?
??? - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
??? {
??????? //textField是否應該結束編輯,如果為NO,無法結束編輯
??????? return YES;
??? }
?? ?
??? - (void)textFieldDidEndEditing:(UITextField *)textField;???????????? {
??????? NSLog(@"已經結束編輯");
??? }
?? ?
??? - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
??? {
??????? //是否應該接受輸入的string,返回的時YES表示接受輸入的字符
??????? //NO 表示不接受輸入的字符
??????? //通常使用再輸入驗證上
??????? if([string isEqualToString:@"!"])
??????? {
??????????? //表示輸入0不接受
??????????? return NO;
??????? }
?????? ?
??????? //其他的字符接受
??????? return YES;
??? }
?? ?
??? - (BOOL)textFieldShouldReturn:(UITextField *)textField
??? {
??????? //當return鍵按下時調用的的代理方法
??????? //讓鍵盤消失,調用resignFirstResponder
??????? //[_currentEditingTextField resignFirstResponder];
??????? [textField resignFirstResponder];
??????? return YES;
??? }
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark 自定義textField鍵盤
- (void)自定義textField{
??? UITextField *textField = [[UITextField alloc]init];
??? //inputView 可以指定一個UIView,充當我們的輸入,當指定了view后,默認鍵盤就不會彈出
??? //customKeyboardView自定義一個 View 作為鍵盤,坐標設為0,0即可;
??? textField.inputView = [self customKeyboardView];
??? //inputAccessoryView設置自定義鍵盤上面的 bar,返回一個 View 就可以坐標設為0 ,0 。
??? textField.inputAccessoryView = [self customAccessoryView];
??? //----------------------————————————————————————————
??? //也可以創建一個 ToolBar作為自定義鍵盤上面的 Bar 如下
??? UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
??? toolBar.translucent = NO;
??? toolBar.barTintColor = [UIColor orangeColor];
??? //在 toolBar 上添加 item,同時可以直接添加 selector 方法
??? UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(itemClick:)];
??? item1.tag = 1;
??? //把所有 item 添加到 toolBar 上,用一個數組
??? toolBar.items = @[item,item1,item,item2,item,item3,item,item4,item];
??? //item 調用 select 可以進行鍵盤類型切換
??? //先拿到當前編輯的 textField,使其失去第一響應者,再設置自定義鍵盤,然后再設為第一響應者
??? UITextField *textField = [self findFirstResponder];
??? //先拿到當前編輯的 textField,使其失去第一響應者
??? [textField resignFirstResponder];
?? // 再設置自定義鍵盤
??? textField.inputView = [self createInputView];
?? // 然后再設為第一響應者
??? [textField becomeFirstResponder];
??? //縮回鍵盤也可以用
??? [self.view endEditing:YES];
?? // use to make the view or any subview that is the first responder resign
}
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark UINavigationController
- (void)UINavigationController {
????? [self automaticallyAdjustsScrollViewInsets];
??? //UINavigationController是一個容器controller,內部存放的是UIViewController,內部自己維護一個數組,顯示的內容就是他容器中的UIViewController的內容
??? //創建UINavigationController的時候,指定一個根
??? UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:firstVC];
??? //UINavigationController 作為容器controller的本質是內部維護了一個數組,數組中存放的都是UIViewController
??? //navController.viewControllers
??? //pushViewController 把一個UIViewController推入導航控制器
??? //self.navigationController 可以找到viewcontroller所在的容器
??? //根view通過superview找到自己父視圖類似
?? ?
??? //pushViewController:
??? //1:secondVC 放到了UINavigationController的容器內
??? //2:把secondVC.navigationController = 容器
??? //3:把secondVC.view顯示在當前的界面上
?? ?
??? [self.navigationController pushViewController:secondVC animated:YES];
??? //popViewControllerAnimated 推出到那個頁面
??????? [self.navigationController popViewControllerAnimated:YES];
??? popViewControllerAnimated:(BOOL)animated; // Returns the popped controller.彈出自己回到上一頁面
??? popToViewController:(UIViewController *)viewController animated:(BOOL)animated; // Pops view controllers until the one specified is on top. Returns the popped controllers.彈出到指定頁面
??? popToRootViewControllerAnimated:(BOOL)animated; // Pops until there's only a single view controller left on the stack. Returns the popped controllers.彈出到跟頁面
??? //UIBarMetricsDefault 人像模式,豎屏幕
??? //一旦使用圖片作為背景,圖片的大小由要求高度 44 (@2 88)> 44會向上拉伸
??? [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"導航背景@2x.png"] forBarMetrics:UIBarMetricsDefault];
?? ?
??? //UIBarMetricsCompact 風景模式,也就是橫屏,由于尺寸問題有可能造成圖片平鋪
?? ?
??? //得到原始的圖片
??? UIImage *image? = [UIImage imageNamed:@"navBg.png"];
??? //創建一個可以拉伸的圖片給他
?? ?
??? //UIImageResizingModeTile,???? 平鋪
??? //UIImageResizingModeStretch,? 拉伸
??? image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch];
?? ?
??? [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsCompact];
?? ?
??? //如果給導航欄設置設置背景,導航欄默認為不透明,坐標從導航欄下開始
??? //注意:對于在導航控制器的中UIViewController,默認情況下(半透明)他的視圖的坐標(0,0)點會被導航欄蓋掉,整個的高度 是狀態欄的20+導航欄的44 ,一共64個point
?? ?
??? //把導航欄改為不透明
??? self.navigationController.navigationBar.translucent = NO;
?? ?
??? //一旦導航欄變為不透明,視圖的(0,0)點坐標是從導航欄先開始
?? ?
??? //修改navigationBar的式樣,會影響到狀態欄到顯示
??? //UIBarStyleBlack 代表黑色式樣
??? //self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
??? //把導航欄修改為其他顏色
??? self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];
?? ?
??? //這里的tintColor可以設置回退按鈕的顏色
??? //self.navigationController.navigationBar.tintColor = [UIColor redColor];
?? ?
??? //修改標題的顏色,以及字體大小
??? //指定一個字典作為title的屬性,
??? //key NSFontAttributeName 指定自定
??? //key NSForegroundColorAttributeName?? 指定顯示的顏色
??? self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:30],NSForegroundColorAttributeName:[UIColor redColor]};
??? self.title = @"標題";
//----------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark 定制NavigationBar
??? //這里不是設置自身的一個回退按鈕,他是設置下一個導航進來的viewController的回退BarButtonItem
?? ?
??? self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(canleBarItemClick:)];
??? //由于圖片顯示渲染的問題,導致在navigationBar上設置的圖片顯示有問題
??? UIImage *image = [UIImage imageNamed:@"rightItem.png"];
??? //設置顯示image時的渲染模式,使用最初的圖片,不需要和背景混合
??? image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
??? //設置ViewContrller在導航欄上的展示內容
??? //每一個UIViewController都有一個navigationItem,由他來設置自己在naviagitonBar上的展示內容
??? //回退按鈕的顯現次序
??? //1:? 如果設置了leftBarButtonItem,那就顯示我設置的leftBarButonItem
??? //2:? 如果上一個UIViewContrller設置backBarButtonItem,那么顯示的就是back
??? //3:? 如果上面沒有設置,查看上一個UIViewController的title是否設置如果設置顯示的是上一個viewController的title
??? //? 3.1 如果上一個title文本比較多,顯示不下,直接就是用系統的Back來顯示
??? //4: 如果說以上都沒有設置,顯示的是Back,系統默認的
?? ?
??? //不想讓狀態欄顯示,需要重寫該方法
??? - (BOOL)prefersStatusBarHidden
??? {
??????? return NO;
??? }
??? //[UIApplication sharedApplication] 得到當前應用程序的對象
?? ?
??? //全局設置狀態欄的式樣,同時在info.plist中設置View Controller-base status Bar appearence 設置為NO
???? [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
??? //添加tap手勢
??? UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapEvent:)];
??? tap.delegate = self;
??? //添加一個 BOOL型成員變量_isShow;
??? [self.view addGestureRecognizer:tap];
??? - (void)handleTapEvent:(UITapGestureRecognizer*)gesture
??? {
??????? _isShow = !_isShow;
?????? ?
??????? //讓導航欄隱藏
??????? //[self.navigationController setNavigationBarHidden:_isShow animated:NO];
?????? ?
??????? //手動讓狀態欄刷新
??????? [self setNeedsStatusBarAppearanceUpdate];
??? }
??? - (BOOL)prefersStatusBarHidden
??? {
??????? return _isShow;
??? }
事件響應鏈,關鍵時候能幫很大的忙
//事件響應鏈:
//1:自己處理不會再傳遞事件的響應,如果自己不處理交給自己的父視圖處理
//2:父視圖不處理-》交給父視圖-》視圖控制器的view-》視圖控制器->UIWindow->UIApplication->Appdelegate->丟棄
//只要觸摸屏幕就會調用下列方法
//這兩種方式都可以讓事件傳遞下去,優先使用super的方式傳遞事件
//super 不僅可以讓事件進行傳遞下去,還會做更多的操作
//直接 nextResponder 有可能造成消息丟失
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
??? //開始觸摸
??? NSLog(@"ViewController:touchesBegan");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
??? //觸摸在移動
??? NSLog(@"ViewController:touchesMoved");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
??? //觸摸結束
??? NSLog(@"ViewController:touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
??? //觸摸取消,譬如來電時會結束當前的一個觸摸
??? NSLog(@"ViewController:touchesCancelled");
}
//----------------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark UITabBarController
- (void)UITabBarController{
//==============================================================
??? 使用系統的 tabBar 來生成
??? //UITabBarControler也是一個容器controller,本身的view不顯示,顯示自己孩子的viewController
??? //里面維護一個數組,viewControllers包含所有的子viewController
??? //通常TabBarController包含navigtionControoller
??? FirstViewController *firstViewController = [[FirstViewController alloc]init];
??? UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:firstViewController];
??? //使用自定義TabBarItem,title標題
??? //在沒有指定選中圖片的時候,默認使用藍色進行選中時候的著色
??? //firstViewController.tabBarItem.title?? //標題
??? //firstViewController.tabBarItem.image?? //未選中時的圖片
??? //firstViewController.tabBarItem.selectedImage //選中時的圖片
?? // item 可以自己定義 title,image
??? UIImage *image = [UIImage imageNamed:@"tab_c1.png"];
??? //渲染時不要混合,使用最原始的圖片輸出
??? image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
??? UITabBarItem *item11 = [[UITabBarItem alloc]initWithTitle:@"頁面1" image:[UIImage imageNamed:@"tab_0.png"] selectedImage:image];
?? ?
??? firstViewController.tabBarItem = item11;
??? UITabBarController *tabBarController = [[UITabBarController alloc]init];
??? //UITabBarController,維護一個數組,把生成的 viewController,加進去就可以了
??? //也可以 創建一個UINavigationController,用initWithRootViewController:firstViewController把firstViewController設為UINavigationController的跟頁面,把UINavigationController生成的對象作為,TabBar 的一個子頁面
??? tabBarController.viewControllers = @[firstViewController,secondViewController,thirdViewController,fourViewController,fiveViewController,sixViewController];
??? //可以制定tintColor給tabbarItem進行選中時的著色
??? tabBarController.tabBar.tintColor = [UIColor redColor];
??? //設置 tabBar 的顏色
??? tabBarController.tabBar.barTintColor = [UIColor purpleColor];
??? //設置tabBar的背景圖片
????? [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_black_bg"]];
??? //如果子viewController個數超過5個,UITabBarController自動幫我們聲稱more的導航控制器,把剩余的tabBarItem放到more對應的視圖控制器之內
??? //設置tabBar上文字的顏色
??? [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]} forState:UIControlStateNormal];
??? [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
}
?
#pragma mark UITabBar的代理方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
??? //紀錄當前選中的是哪一個viewController
??? //1:獲取viewController的次序
??? //2:保存到沙盒下
??? //3:從沙盒目錄中讀取
??? //4:啟動后重新設置選中的次序
??? NSLog(@"--->%ld",tabBarController.selectedIndex);
?? ?
??? //得到我的沙盒目錄 sandbox
?? ?
??? NSString *homePath = NSHomeDirectory();
??? NSLog(@"%@",homePath);
?? ?
??? //NSUserDefaults? 是使用本地化存儲的方式之一,
??? //實現的原理是在沙盒目錄下,保存的一個plist文件
??? //只適合保存比較少量數據,譬如用戶名,密碼,開機歡迎界面是否顯示的標志
??? [[NSUserDefaults standardUserDefaults] setInteger:tabBarController.selectedIndex forKey:@"currentSelectIndex"];
??? //注意要調用同步方法,由于NSUserDefault不會把數據馬上寫到本地文件
??? //會有延遲,調用synchronize 同步方法,會讓數據馬上同步到本地文件
??? [[NSUserDefaults standardUserDefaults] synchronize];
?? ?
??? //把key對應的值讀取出來
??? [[NSUserDefaults standardUserDefaults] integerForKey:@"currentSelectIndex"];
}
?
#pragma mark -------------------------------------
#pragma mark 自定義 TabBar
?- (void)customTabBar
??? {
??????? //先把自己的tabBar欄隱藏
??????? [self.tabBar setHidden:YES];
?????? ?
??????? //使用UIImageView充當我們的背景
??????? UIImageView *backgroundView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 667-49, 375, 49)];
??????? backgroundView.image = [UIImage imageNamed:@"tab_black_bg@2x.png"];
??????? backgroundView.userInteractionEnabled = YES;
??????? [self.view addSubview:backgroundView];
?????? ?
??????? NSArray *titleArray = @[@"歷史",@"收藏",@"分享",@"設置"];
?????? ?
??????? CGFloat buttonWidth = 375/self.viewControllers.count;
??????? CGFloat buttonHeight = 49;
??????? for (int index = 0; index < self.viewControllers.count;index++) {
??????????? UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
??????????? [button setTitle:[titleArray objectAtIndex:index] forState:UIControlStateNormal];
??????????? [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
??????????? [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
??????????? NSString *normalImageName = [NSString stringWithFormat:@"tab_%d",index];
??????????? NSString *selectImageName = [NSString stringWithFormat:@"tab_c%d",index];
?????????? ?
??????????? //設置button上的字體的大小
??????????? button.titleLabel.font = [UIFont systemFontOfSize:12];
?????????? ?
??????????? [button setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
??????????? [button setImage:[UIImage imageNamed:selectImageName] forState:UIControlStateSelected];
??????????? button.frame = CGRectMake(index*buttonWidth, 0, buttonWidth, buttonHeight);
?????????? ?
??????????? //對于button可以設置他image的內邊距,也可以設置title的內邊距,來調整button上文字和圖片的位置
??????????? //button.imageEdgeInsets 設置圖片的內邊距
??????????? //button.titleEdgeInsets 設置title的內邊距
??????????? button.imageEdgeInsets = UIEdgeInsetsMake(1, 15, 15, 0);
??????????? button.titleEdgeInsets = UIEdgeInsetsMake(30,-25, 0, 0);
?????????? ?
??????????? button.tag = 100 + index;
??????????? [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
?????????? ?
??????????? //設置初始顯示時選中第一個按鈕
??????????? if (index == 0) {
??????????????? button.selected = YES;
??????????? }
?????????? ?
??????????? [backgroundView addSubview:button];
??????? }
??? }
?
- (void)buttonClick:(UIButton*)button
{
??? //重置button的狀態
??? [self resetButtonStatus];
??? NSInteger index = button.tag - 100;
?? ?
??? //讓button處于select狀態
??? button.selected = YES;
?? ?
??? //修改UITabBarControler的selectedIndex ,會導致切換到對應的頁面
??? self.selectedIndex = index;
}
?
- (void)resetButtonStatus
{
??? for (int index = 0; index < self.viewControllers.count; index++) {
??????? UIButton *button = (UIButton*)[self.view viewWithTag:100+index];
??????? button.selected = NO;
??? }
}
?
//======================================================================================================
#pragma mark -------------------------------------
#pragma mark UITextView多行編輯文本框和警告框
- (void)testUITextView
{
??? // UITextView 是多行編輯的文本框
??? UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 80, 150, 80)];
??? //背景色
??? textView.backgroundColor = [UIColor grayColor];
??? //字體
??? textView.font = [UIFont systemFontOfSize:20];
??? //文字顏色
??? textView.textColor = [UIColor greenColor];
?? ?
??? //設置textView的代理
??? textView.delegate = self;
??? //自適應大小時設置為 no 不會跳動
??? //沒有自適應大小時,設置為 yes 可以超過本身行數輸入,可以滑動瀏覽
??? textView.scrollEnabled = NO;
?? ?
??? //使用它定制鍵盤
??? //textView.inputView
}
#pragma mark UITextView代理方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
??? NSLog(@"是否開始編輯");
??? return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
??? NSLog(@"是否應該");
??? return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
??? NSLog(@"應開始編輯");
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
??? NSLog(@"已經結束編輯");
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
??? //指示輸入的字符是否應該接收,這里可以有自己的判斷邏輯
??? if ([text isEqualToString:@"\n"]) {
??????? //\n代表回車
??????? //第一響應者的概念跟UITextField一樣
??????? [textView resignFirstResponder];
??????? return NO;
??? }
??? return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
??? //輸入框的文本發生了變化
??? NSLog(@"%@",textView.text);
??? //[textView sizeToFit];
??? CGFloat textViewWidth = textView.frame.size.width;
??? //文本框隨文字多少大小變化
??? CGSize newSize = [textView sizeThatFits:CGSizeMake(textViewWidth, MAXFLOAT)];
??? CGRect newFrame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textViewWidth, newSize.height);
?? ?
??? textView.frame = newFrame;
}
?
- (void)testUIActionSheet
{
??? UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"標題" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"other" otherButtonTitles:@"A",@"B",@"C", nil];
??? [actionSheet showInView:self.view];
}
?
- (void)testUIAlertView
{
??? //警告框,通常使用在提示用戶,或者讓用戶做出選擇
??? //對于otherButtonTitles,如果不需要,直接寫為nil
??? UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"標題" message:@"警告內容" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"A",@"B",@"C", nil];
??? [alterView show];
?? ?
??? //??? UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"是否繼續操作" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
??? //??? [alterView show];
}
#pragma mark UIActionSheet代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{??? //按鈕序號從上到下0——>
??? //還是要根據buttonIndex來決定是哪個按鈕被選中
??? NSLog(@"%ld",buttonIndex);
}
?
- (void)actionSheetCancel:(UIActionSheet *)actionSheet{
?? ?
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{
??? // before animation and showing view
}
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet{
??? // after animation
}
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
??? // before animation and hiding view
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
??? // after animation
}
#pragma mark UIAlertView代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
??? //注意,cancle對應的button,他的index 是0,其他的根據次序順序排列
??? //這樣我們根據buttonIndex來決定響應了哪一個按鈕
??? NSLog(@"%ld",buttonIndex);
}
- (void)alertViewCancel:(UIAlertView *)alertView{
??? //警告框取消
}
- (void)willPresentAlertView:(UIAlertView *)alertView{
??? // before animation and showing view
}
- (void)didPresentAlertView:(UIAlertView *)alertView{
??? // after animation
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
??? // before animation and hiding view
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
??? // after animation
}
//======================================================================================================
#pragma mark -------------------------------------
#pragma mark UIScrollView滾動視圖
- (void)UIScrollView{
??? CGRect rect = self.view.frame;
??? CGRect scrollViewFrame = CGRectInset(rect, 50, 100);
??? UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:scrollViewFrame];
??? scrollView.backgroundColor = [UIColor redColor];
??? scrollView.tag = 10;
??? //得到文件的路徑
??? NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"];
??? UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
??? //通過image.size 可以得到圖片的大小,使用 image 初始化 UIImageView,直接使用圖片的寬高作為 UIImage 的寬高
??? UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
??? [scrollView addSubview:imageView];
??? //contentSize 是指定scollView能顯示的內容的大小
??? //如果不設置contentSize的大小默認contentSize的大小就是scrollView的view的frame的大小
??? scrollView.contentSize = CGSizeMake(imageView.bounds.size.width, imageView.bounds.size.height);
??????? //scrollView是否有回彈效果
??? scrollView.bounces = NO;
??? //是否顯示水平滾動條
??? scrollView.showsHorizontalScrollIndicator = NO;
??? //是否顯示垂直滾動條
??? scrollView.showsVerticalScrollIndicator;
??? //重點:
??? //設置scrollView的內容偏移量
??? //cotentOffset的內容視圖的左上角到scrollView左上角的一個偏移量
??? //contentOffset.x? x軸的偏移量
??? //contentOffset.y? y軸的偏移量
??? //計算時以內容視圖的左上角為基準
??? //contentOffset默認從(0,0)開始
??? scrollView.contentOffset = CGPointMake(100, 0);
??? //設置scorllView的代理,當滾動事件事件發生時,相應的代理方法被調用
??? scrollView.delegate = self;
??? //設置scollView以分頁的效果顯示
??? scrollView.pagingEnabled = YES;
??? //minimumZoomScale 設置最小的放大系數
??? scrollView.minimumZoomScale = 1.0;
??? //設置最大的放大系數
??? scrollView.maximumZoomScale = 2.0;
??? //添加雙擊事件進行縮放
??? UITapGestureRecognizer *tapGestrue = [[UITapGestureRecognizer alloc]initWithTarget:self? action:@selector(handleTapEvent:)];
??? tapGestrue.numberOfTapsRequired = 2;
??? [scrollView addGestureRecognizer:tapGestrue];
?? ?
??? [self.view addSubview:scrollView];
}
- (void)handleTapEvent:(UITapGestureRecognizer*)gesture
{
??? static BOOL isZooming = NO;
??? UIScrollView *scrollView = (UIScrollView*)[self.view viewWithTag:100];
??? if (isZooming) {
??????? [scrollView setZoomScale:1.0 animated:YES];
??? }else{
??????? //scrollView.zoomScale = 2.0;
??????? [scrollView setZoomScale:2.0 animated:YES];
??? }
??? isZooming = !isZooming;
}
?
#pragma mark UIScrollViewDelegate
//只要scrollView滾動就會調用該方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
??? //由于該方法在視圖滾動中一直調用,所以不要在這里做耗時的計算
??? NSLog(@"視圖滾動");
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
??? NSLog(@"滾動視圖內容即將被拖動");
}
//decelerate 指定是否有減速動作
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
??? NSLog(@"滾動視圖的內容已經結束拖動");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
??? NSLog(@"即將開始減速");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
??? NSLog(@"減速結束,內容視圖停止");
}
//指定在scollView上哪一個視圖被縮放
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
??? return [scrollView.subviews objectAtIndex:0];
}
- (void)createPageControl
{
??? _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
??? _pageControl.center = CGPointMake(_scrollView.center.x,_scrollView.center.y+_scrollView.bounds.size.height/2-20);
??? //指定pageController有多少頁,由他決定圓點的個數
??? _pageControl.numberOfPages = 6;
?? ?
??? //pageIndicatorTintColor 圓點的顏色
??? _pageControl.pageIndicatorTintColor = [UIColor blueColor];
?? ?
??? //currentPageIndicatorTintColor? 當前頁的顏色
??? _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
?? ?
??? //_pageControl.userInteractionEnabled = NO;
??? //給pageControl 綁定valueChanged事件
??? [_pageControl addTarget:self action:@selector(pageControllChanged:) forControlEvents:UIControlEventValueChanged];
??? [self.view addSubview:_pageControl];
}
- (void)pageControllChanged:(UIPageControl*)pageControl
{
??? //首先得到pageControl目前顯示的是哪一頁
??? NSInteger currentPage = pageControl.currentPage;
?? ?
??? CGPoint offset = CGPointMake(currentPage*_scrollView.bounds.size.width, 0);
??? //需要使用代碼的方式滾動到相應的位置
??? [_scrollView setContentOffset:offset animated:YES];
}
#pragma mark -
#pragma mark UIScrollViewDelegate
- (void)createContentImageViews
{
??? for (int index = 1; index <= 6; index++) {
??????? NSString *imageName = [NSString stringWithFormat:@"%d.png",index];
??????? UIImage *image = [UIImage imageNamed:imageName];
??????? UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((index-1)*_scrollView.bounds.size.width, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
??????? imageView.image = image;
??????? [_scrollView addSubview:imageView];
??? }
?? ?
??? //這里設置以下contentSize的大小,指定scrollView顯示內容的區域大小
??? _scrollView.contentSize = CGSizeMake(6*_scrollView.bounds.size.width, _scrollView.bounds.size.height);
}
//只要設置了scrollView的分頁顯示,當手動(使用手指)滾動結束后,該代理方法會被調用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
??? NSLog(@"scrollViewDidEndDecelerating");
??? //得到當前的偏移
??? CGPoint offset = scrollView.contentOffset;
??? NSLog(@"offset x = %f y = %f",offset.x,offset.y);
??? //使用偏移量計算頁碼
??? NSInteger pageNumber = offset.x/scrollView.bounds.size.width;
??? NSLog(@"pageNumber = %ld",pageNumber);
?? ?
??? //設置pageController的當前頁,對應的原點高亮顯示
??? _pageControl.currentPage = pageNumber;
}
//該代理是使用代碼的方式設置contentOffset后的代理
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
??? NSLog(@"scrollViewDidEndScrollingAnimation");
}
?
轉載于:https://www.cnblogs.com/Lvfengxian/p/4902666.html
總結
以上是生活随笔為你收集整理的温故而知新,UI学习中的大部分控件及常用的基础都整理了一下,很长~~~~~~~~~很长!!!!!!!...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1022 词法分析程序总结
- 下一篇: 84岁济公演戏 济公扮演者游本昌近况个人