温故而知新,UI学习中的大部分控件及常用的基础都整理了一下,很长~~~~~~~~~很长!!!!!!!...
生命周期,生命周期,生命周期,重要的事要說三遍,戰(zhàn)五渣的我最常用的好像只有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是應(yīng)用程序的窗體,所有的界面都顯示在UIWindow上
??? //一般情況下一個應(yīng)用程序只有一個UIWindow,它類似與畫布,所有的界面都在上面描繪
??? //initWithFrame 傳給它一個顯示的區(qū)域大小,作為根都是屏幕大小
??? //使用UIScreen 獲取屏幕的大小,bounds就是屏幕的寬高
??? //CGRect描繪一個矩形,指定原點和寬高即可
?? ?
??? CGRect rect = [UIScreen mainScreen].bounds;
??? NSLog(@"screen bounds%@",NSStringFromCGRect(rect));
??? //iphone的屏幕坐標(biāo)原點在左上角,x軸向右增加,y軸向下增加
??? self.window = [[UIWindow alloc]initWithFrame:rect];
??? //設(shè)置window的背景色,backgroundColor
??? //在ios中使用UIColor代表顏色,也可以指定red green blue三元色來指定
??? self.window.backgroundColor = [UIColor orangeColor];
?? ?
??? //UIView :view 是代表視圖,一個可見的顯示區(qū)域
??? UIView *view1 = [[UIView? alloc]initWithFrame:CGRectMake(10, 20, 200, 200)];
??? view1.backgroundColor = [UIColor redColor];
?? ?
??? //所有要顯示的控件都必須最終添加到window上,才能顯示出來、
??? //addSubView:添加一個子視圖,這樣view1就和window發(fā)生了關(guān)聯(lián)
??? //window就是view1的父視圖,view1就是子視圖
??? [self.window addSubview:view1];
?? ?
??? //指定frame時,原點的坐標(biāo)系是父視圖的坐標(biāo)系
??? UIView *subView1 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
??? subView1.backgroundColor = [UIColor blueColor];
??? //把subView1添加到view1上
??? [view1 addSubview:subView1];
?? ?
??? //根據(jù)自身的寬高加中心點的方式指定一個顯示區(qū)域
??? UIView *view2 = [[UIView alloc]init];
??? //指定bounds時,x,y坐標(biāo)由于是參考自身的坐標(biāo)系所以永遠(yuǎn)為0,只需要指定寬高即可
??? //center 中心點
??? view2.bounds = CGRectMake(0, 0, 150, 150);
??? //center中心點的坐標(biāo)系是參考父視圖
??? 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都會對應(yīng)一個layer,layer幫我們做的事情是渲染view的內(nèi)容
??? //2:layer中有一些屬性是view沒有的,這樣我們可以對UIView做進(jìn)一步的設(shè)置
??? //3:所有發(fā)生在view上的動畫,都是layer完成的。
?? ?
??? //UIView的作用:1:顯現(xiàn)layer渲染出來的界面,2:與用戶進(jìn)行交互
?? ?
??? //UIViewAutoresizingFlexibleLeftMargin 子視圖的左邊根據(jù)父視圖的大小變化而變化
??? //UIViewAutoresizingFlexibleWidth 子視圖的寬度根據(jù)父視圖的大小變化而變化
??? //& | !
??? //autoresizingMask 設(shè)置子視圖怎么根據(jù)父視圖的大小變化而變化的屬性
??? blueView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
??? //subviews 把自己的子視圖放到數(shù)組中
??? NSArray *subViews = self.window.subviews;
??? NSLog(@"%@",subViews);
?? ?
??? //交換兩個視圖的次序,次序既為數(shù)組中的下表
??? [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
??? //----------------------------------------------------------------------
??? //UIview頁面翻轉(zhuǎn)動畫
?? ?
??? //??? 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];
?? ?
??? //動畫塊,注意必須成對的出現(xiàn)
??? //animationID 字符串表識一個annimation
??? //context:這里可以傳遞動畫結(jié)束時還需要使用的參數(shù)
??? [UIView beginAnimations:nil context:nil];
?? ?
??? //設(shè)置動畫行進(jìn)的速度曲線
??? //??? typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
??? //??????? UIViewAnimationCurveEaseInOut,???????? // slow at beginning and end
??? //??????? UIViewAnimationCurveEaseIn,??????????? // slow at beginning
??? //??????? UIViewAnimationCurveEaseOut,?????????? // slow at end
??? //??????? UIViewAnimationCurveLinear
??? //??? };
?? ?
??? [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
?? ?
??? //整個動畫的持續(xù)時間
??? [UIView setAnimationDuration:1];
?? ?
??? //UIViewAnimationTransitionCurlUp 向上翻頁
??? //UIViewAnimationTransitionCurlDown 向下
??? //UIViewAnimationTransitionFlipFromLeft? 從左邊翻轉(zhuǎn)
??? //UIViewAnimationTransitionFlipFromRight 從右邊翻轉(zhuǎn)
?? ?
??? //forView 是在哪一個view上做動畫
??? //catche 是否要緩存最初捕獲的頁面,一般寫為YES,提高效率
??? [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.window cache:YES];
?? ?
??? //設(shè)置動畫的重復(fù)次數(shù)
??? [UIView setAnimationRepeatCount:1];
?? ?
??? //是否自動翻轉(zhuǎn):YES 自動翻轉(zhuǎn)
??? //[UIView setAnimationRepeatAutoreverses:YES];
?? ?
??? //設(shè)置動畫的代理
??? [UIView setAnimationDelegate:self];
??? //設(shè)置動畫結(jié)束時調(diào)用的方法
??? //[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{
??? //通過系統(tǒng)提供的使用可以生成系統(tǒng)提供的按鈕
??? UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
??? //設(shè)置普通狀態(tài)下的按鈕的文本
??? [button setTitle:@"按鈕" forState:UIControlStateNormal];
??? //設(shè)置文本的顏色,狀態(tài)為Normal
??? [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
??? //高亮狀態(tài)下的文本的顏色
??? [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
??? //使用[UIImage imageNamed:@""] 通過指定圖片名稱的方式來生成一個UIImage
??? //@2x.png
??? //首先UIImage imageNamed如果沒指定圖片的后綴,默認(rèn)格式png
??? //@2x 意思是2倍像素
??? //imageNamed :文件名,首先檢查是否為高清屏(視網(wǎng)膜屏),如果是就找文件
??? //文件名@2x.png,如果沒有對應(yīng)的@2x的圖片,就使用指定的文件
??? UIImage *imageNormal = [UIImage imageNamed:@"bgCommon"];
??? UIImage *imageHightLight = [UIImage imageNamed:@"bgSelected"];
?? ?
??? //給button設(shè)置背景圖片,normal狀態(tài)下
??? //注意:button會對圖片根據(jù)button的區(qū)域大小進(jìn)行拉伸,如果二者的寬高比不一致圖片可能變形
??? [button setBackgroundImage:imageNormal forState:UIControlStateNormal];
?? ?
??? //指定button在highlight模式下的背景圖片
??? [button setBackgroundImage:imageHightLight forState:UIControlStateHighlighted];
??? //當(dāng)指定的事件(Event)發(fā)生時,調(diào)用target的selector
?? ?
??? //UIControlEventTouchDown 一旦按鈕被按下就觸發(fā)的buttonClickDown
??? [button addTarget:self action:@selector(buttonClickDown:) forControlEvents:UIControlEventTouchDown];
?? ?
??? //UIControlEventTouchUpInside :當(dāng)手指按下后開始起來,inside意思是手指在UIButton的區(qū)域范圍只你
??? //當(dāng)button的事件UIControlEventTouchUpInside發(fā)生時,調(diào)用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];
?? ?
??? //設(shè)置字體大小,默認(rèn)字體大小是17
??? label.font = [UIFont systemFontOfSize:30];
?? ?
??? //指定字體
??? //label.font = [UIFont fo];
??? //指定字體的左右對齊方式
??? //NSTextAlignmentLeft??? 靠左 默認(rèn)
??? //NSTextAlignmentCenter? 居中
??? //NSTextAlignmentRight?? 靠右
??? label.textAlignment = NSTextAlignmentCenter;
??? //設(shè)置為0,不限制 lable 行數(shù)
??? label.numberOfLines? = 0;
??? label.text = @"Application windows are expected to have a root view controller at the end of application launch";
?? ?
??? //設(shè)置adjustsFontSizeToFitWidth 的屬性為yes,可以自動調(diào)整字體的大小以適應(yīng)整個的區(qū)域
??? label.adjustsFontSizeToFitWidth = YES;
??? //自動計算要顯示文本需要的區(qū)域大小
??? [label sizeToFit];
??? //如果需要自己控制換行,可以手動添加\n
??? label.text = @"窗前明月光\n疑似地上霜\n舉頭望明月\n低頭思校長";
??? int index = 0;
??? //得到所有的字體家族(一個字體家族包含的字體可能由斜體,粗體,下劃線)
??? //換行的模式,意識根據(jù)什么來進(jìn)行換行
??? //NSLineBreakByWordWrapping 以單詞為單位進(jìn)行換行
??? //NSLineBreakByCharWrapping 以字符為單位進(jìn)行換行
??? //截斷的模式
??? //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:時間間隔,指定每隔多長時間定時器觸發(fā)一次,單位是秒
??? //target + selector? :當(dāng)定時器觸發(fā)時,調(diào)用的方法
??? //userInfo:是額外的參數(shù),通常設(shè)置為nil
??? //repeats:指定定時器是否一直重復(fù),YES 一直重復(fù) NO:不重復(fù)只觸發(fā)一次
?? ?
??? //timer會對傳入的targe的進(jìn)行保留,(retainCount+1),防止timer觸發(fā)時,target釋放掉,這里對self的retainCount加一
??? //等到timer停掉的時候,才把self的retainCount減一`
??? //定義timer每隔0.1秒觸發(fā)一次
??? _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
??? //由于timer對self有保留,dealloc不會被調(diào)用
??? //不要在dealloc中停掉timer
??? //讓定時器失效,真?zhèn)€timer停掉
??? [_timer invalidate];
??? _timer = nil;
??? [UIView animateWithDuration:5 animations:^{
??????? //這里寫動畫的最終的狀態(tài),也就是雪花的最終位置
??????? 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默認(rèn)的alpha值1
??????????? snowImageView.alpha = 0.0;
??????? } completion:^(BOOL finished) {
??????????? //這里正對alpha的動畫已經(jīng)結(jié)束,這是可以把視圖從父視圖上刪除
??????????? [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 :動畫的持續(xù)時間,單位秒
??????? //2:delay:動畫延遲多長時間開始 單位秒
??????? //3:Damping 阻尼,取值范圍0~1,一般 < 0.7 晃動比較厲害,為1不會有彈簧效果
??????? //4:Velocity:初始速度,單位 points/秒,一般設(shè)置為0就可以了
??????? //5:options:動畫的一些屬性,設(shè)置速度的曲線,或者翻轉(zhuǎn)的方式
??????? //6:animations :動畫內(nèi)容
??????? //7:complete:動畫結(jié)束時回調(diào)的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旋轉(zhuǎn) tx,ty位移,基礎(chǔ)的2D矩陣
??????????? //旋轉(zhuǎn),參數(shù)指定為弧度,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回到最原始的狀態(tài),沒有縮放,沒有旋轉(zhuǎn),沒有平移
??????????? ballView.transform = CGAffineTransformIdentity;
??????? } completion:^(BOOL finished) {
?????????? ?
??????? }];
??? }
}
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark Notification 通知中心
- (void)notification{
??? _boy1 = [[Boy alloc]init];
?? ?
??? //把boy添加或注冊到通知中心,關(guān)注的事件是name中標(biāo)示的字符串
??? //當(dāng)其他對象發(fā)布通知時,通知中心中關(guān)注次事件的對象都會得到通知
??? //自動調(diào)用selector方法,即[boy findFriend:]
?? ?
??? //注意:通知中心不會把你注冊對象保留,當(dāng)boy對象釋放的時候,一定要把自己從通知中心中移除
??? //通知中心:不會對你添加的對象進(jìn)行比較,可能存在一個對象多次添加到通知中心的情況,添加幾次就會調(diào)用幾次
?? ?
??? //最有一個參數(shù)object:nil? 不管誰發(fā)送的findFriendNotification 通知自己都會接受
??? //如果object,非nil,指定為一個對象,意思是只關(guān)注該對象發(fā)送的通知
?? ?
??? //為了避免多次注冊到通知中心,先把_boy1從通知中心中移除,然后再添加
??? [[NSNotificationCenter defaultCenter] removeObserver:_boy1];
??? [[NSNotificationCenter defaultCenter] addObserver:_boy1 selector:@selector(findFriend:) name:@"findFriendNotification" object:nil];
??? //通知傳值一般寫法
??? //發(fā)通知方:
??? //postNotificationName:通知的名字 object:發(fā)通知的人,一般為 self? userInfo:要發(fā)送的信息是個字典(也就是要傳的值)
??? [NSNotificationCenter defaultCenter]postNotificationName:<#(NSString *)#> object:<#(id)#> userInfo:<#(NSDictionary *)#>];
??? //接收方:
??? //addObserver: 注冊人一般為 self select:收到通知后調(diào)用的方法;name:為通知名,要與發(fā)通知的名字一樣 object:后面寫就收誰的通知(即發(fā)通知人的名字),寫 nil意思是,只要通知名一樣都接收
??? [NSNotificationCenter defaultCenter]addObserver:<#(id)#> selector:@selector(resiveNotification:) name:<#(NSString *)#> object:<#(id)#>];
}
??? //調(diào)用一下方法來得到傳過來的值用字典接收
- (void)resiveNotification:(NSNotification*)nitification{
??????? NSDictionary *dic =[nitification userInfo];
??? }
??? //要注意,必須先注冊過才能收到通知,對于平行頁面,如 tabBar 里的各個界面用通知傳值,后面的界面還沒有生成,也就沒有注冊,可以把注冊方法寫到 init 中,可以實現(xiàn),事先注冊,可以直接得到傳值
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark TextField 輸入框
- (void)TextField{
??? UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(85,20, 200, 50)];
??? //設(shè)置邊框類型
//??? UITextBorderStyleNone,
//??? UITextBorderStyleLine,
//??? UITextBorderStyleBezel,
//??? UITextBorderStyleRoundedRect
??? textField.borderStyle = UITextBorderStyleRoundedRect;
??? //暗文
??? textField.secureTextEntry = YES;
?? ?
??? //設(shè)置底部背景,通常和邊框類型為None的一起使用,對于圓角類型的沒有效果
??? textField.background = [UIImage imageNamed:@"bg.png"];
?? ?
??? //placeholder 占位符,開始編輯后消失,主要起到提示的作用,設(shè)置后灰色顯示提示
??? textField.placeholder = @"請輸入";
?? ?
??? //編輯后是否顯示編輯的刪除標(biāo)識,可以把文本一次性的刪除
??? //
??? //UITextFieldViewModeNever,?????????? 永不顯示
??? //UITextFieldViewModeWhileEditing,??? 當(dāng)編輯的時候顯示
??? //UITextFieldViewModeUnlessEditing,?? 當(dāng)不編輯的時候顯示
??? //UITextFieldViewModeAlways?????????? 一直顯示
??? textField.clearButtonMode = UITextFieldViewModeWhileEditing;
?? ?
??? //橫向居中顯示
??? textField.textAlignment = NSTextAlignmentCenter;
?? ?
??? //調(diào)整文本垂直方向靠上,居中,靠下
??? //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;
?? ?
??? //當(dāng)再次編輯時,是否把上次編輯的內(nèi)容清掉
??? textField.clearsOnBeginEditing = YES;
?? ?
??? //鍵盤類型
??? textField.keyboardType = UIKeyboardTypeDefault;
?? ?
??? //設(shè)置return的類型,
??? textField.returnKeyType = UIReturnKeyEmergencyCall;
?? ?
??? //設(shè)置textField的代理,這樣再開始編輯。
??? //結(jié)束編輯調(diào)用相應(yīng)的代理方法
??? textField.delegate = self;
?? ?
??? //是否自動糾錯
??? textField.autocorrectionType = NO;
?? ?
??? //讓輸入框成為第一響應(yīng)者,這樣鍵盤自動彈出
??? //所謂第一響應(yīng)者就是鍵盤的輸入和該控件結(jié)合再一起
??? [textField becomeFirstResponder];
?? ?
??? //要讓鍵盤消失,就是讓textField 不是第一相應(yīng)者,這樣鍵盤就會消失
??? [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是否應(yīng)該結(jié)束編輯,如果為NO,無法結(jié)束編輯
??????? return YES;
??? }
?? ?
??? - (void)textFieldDidEndEditing:(UITextField *)textField;???????????? {
??????? NSLog(@"已經(jīng)結(jié)束編輯");
??? }
?? ?
??? - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
??? {
??????? //是否應(yīng)該接受輸入的string,返回的時YES表示接受輸入的字符
??????? //NO 表示不接受輸入的字符
??????? //通常使用再輸入驗證上
??????? if([string isEqualToString:@"!"])
??????? {
??????????? //表示輸入0不接受
??????????? return NO;
??????? }
?????? ?
??????? //其他的字符接受
??????? return YES;
??? }
?? ?
??? - (BOOL)textFieldShouldReturn:(UITextField *)textField
??? {
??????? //當(dāng)return鍵按下時調(diào)用的的代理方法
??????? //讓鍵盤消失,調(diào)用resignFirstResponder
??????? //[_currentEditingTextField resignFirstResponder];
??????? [textField resignFirstResponder];
??????? return YES;
??? }
//---------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark 自定義textField鍵盤
- (void)自定義textField{
??? UITextField *textField = [[UITextField alloc]init];
??? //inputView 可以指定一個UIView,充當(dāng)我們的輸入,當(dāng)指定了view后,默認(rèn)鍵盤就不會彈出
??? //customKeyboardView自定義一個 View 作為鍵盤,坐標(biāo)設(shè)為0,0即可;
??? textField.inputView = [self customKeyboardView];
??? //inputAccessoryView設(shè)置自定義鍵盤上面的 bar,返回一個 View 就可以坐標(biāo)設(shè)為0 ,0 。
??? textField.inputAccessoryView = [self customAccessoryView];
??? //----------------------————————————————————————————
??? //也可以創(chuàng)建一個 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 上,用一個數(shù)組
??? toolBar.items = @[item,item1,item,item2,item,item3,item,item4,item];
??? //item 調(diào)用 select 可以進(jìn)行鍵盤類型切換
??? //先拿到當(dāng)前編輯的 textField,使其失去第一響應(yīng)者,再設(shè)置自定義鍵盤,然后再設(shè)為第一響應(yīng)者
??? UITextField *textField = [self findFirstResponder];
??? //先拿到當(dāng)前編輯的 textField,使其失去第一響應(yīng)者
??? [textField resignFirstResponder];
?? // 再設(shè)置自定義鍵盤
??? textField.inputView = [self createInputView];
?? // 然后再設(shè)為第一響應(yīng)者
??? [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,內(nèi)部存放的是UIViewController,內(nèi)部自己維護(hù)一個數(shù)組,顯示的內(nèi)容就是他容器中的UIViewController的內(nèi)容
??? //創(chuàng)建UINavigationController的時候,指定一個根
??? UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:firstVC];
??? //UINavigationController 作為容器controller的本質(zhì)是內(nèi)部維護(hù)了一個數(shù)組,數(shù)組中存放的都是UIViewController
??? //navController.viewControllers
??? //pushViewController 把一個UIViewController推入導(dǎo)航控制器
??? //self.navigationController 可以找到viewcontroller所在的容器
??? //根view通過superview找到自己父視圖類似
?? ?
??? //pushViewController:
??? //1:secondVC 放到了UINavigationController的容器內(nèi)
??? //2:把secondVC.navigationController = 容器
??? //3:把secondVC.view顯示在當(dāng)前的界面上
?? ?
??? [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:@"導(dǎo)航背景@2x.png"] forBarMetrics:UIBarMetricsDefault];
?? ?
??? //UIBarMetricsCompact 風(fēng)景模式,也就是橫屏,由于尺寸問題有可能造成圖片平鋪
?? ?
??? //得到原始的圖片
??? UIImage *image? = [UIImage imageNamed:@"navBg.png"];
??? //創(chuàng)建一個可以拉伸的圖片給他
?? ?
??? //UIImageResizingModeTile,???? 平鋪
??? //UIImageResizingModeStretch,? 拉伸
??? image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch];
?? ?
??? [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsCompact];
?? ?
??? //如果給導(dǎo)航欄設(shè)置設(shè)置背景,導(dǎo)航欄默認(rèn)為不透明,坐標(biāo)從導(dǎo)航欄下開始
??? //注意:對于在導(dǎo)航控制器的中UIViewController,默認(rèn)情況下(半透明)他的視圖的坐標(biāo)(0,0)點會被導(dǎo)航欄蓋掉,整個的高度 是狀態(tài)欄的20+導(dǎo)航欄的44 ,一共64個point
?? ?
??? //把導(dǎo)航欄改為不透明
??? self.navigationController.navigationBar.translucent = NO;
?? ?
??? //一旦導(dǎo)航欄變?yōu)椴煌该?#xff0c;視圖的(0,0)點坐標(biāo)是從導(dǎo)航欄先開始
?? ?
??? //修改navigationBar的式樣,會影響到狀態(tài)欄到顯示
??? //UIBarStyleBlack 代表黑色式樣
??? //self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
??? //把導(dǎo)航欄修改為其他顏色
??? self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];
?? ?
??? //這里的tintColor可以設(shè)置回退按鈕的顏色
??? //self.navigationController.navigationBar.tintColor = [UIColor redColor];
?? ?
??? //修改標(biāo)題的顏色,以及字體大小
??? //指定一個字典作為title的屬性,
??? //key NSFontAttributeName 指定自定
??? //key NSForegroundColorAttributeName?? 指定顯示的顏色
??? self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:30],NSForegroundColorAttributeName:[UIColor redColor]};
??? self.title = @"標(biāo)題";
//----------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark 定制NavigationBar
??? //這里不是設(shè)置自身的一個回退按鈕,他是設(shè)置下一個導(dǎo)航進(jìn)來的viewController的回退BarButtonItem
?? ?
??? self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(canleBarItemClick:)];
??? //由于圖片顯示渲染的問題,導(dǎo)致在navigationBar上設(shè)置的圖片顯示有問題
??? UIImage *image = [UIImage imageNamed:@"rightItem.png"];
??? //設(shè)置顯示image時的渲染模式,使用最初的圖片,不需要和背景混合
??? image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
??? //設(shè)置ViewContrller在導(dǎo)航欄上的展示內(nèi)容
??? //每一個UIViewController都有一個navigationItem,由他來設(shè)置自己在naviagitonBar上的展示內(nèi)容
??? //回退按鈕的顯現(xiàn)次序
??? //1:? 如果設(shè)置了leftBarButtonItem,那就顯示我設(shè)置的leftBarButonItem
??? //2:? 如果上一個UIViewContrller設(shè)置backBarButtonItem,那么顯示的就是back
??? //3:? 如果上面沒有設(shè)置,查看上一個UIViewController的title是否設(shè)置如果設(shè)置顯示的是上一個viewController的title
??? //? 3.1 如果上一個title文本比較多,顯示不下,直接就是用系統(tǒng)的Back來顯示
??? //4: 如果說以上都沒有設(shè)置,顯示的是Back,系統(tǒng)默認(rèn)的
?? ?
??? //不想讓狀態(tài)欄顯示,需要重寫該方法
??? - (BOOL)prefersStatusBarHidden
??? {
??????? return NO;
??? }
??? //[UIApplication sharedApplication] 得到當(dāng)前應(yīng)用程序的對象
?? ?
??? //全局設(shè)置狀態(tài)欄的式樣,同時在info.plist中設(shè)置View Controller-base status Bar appearence 設(shè)置為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;
?????? ?
??????? //讓導(dǎo)航欄隱藏
??????? //[self.navigationController setNavigationBarHidden:_isShow animated:NO];
?????? ?
??????? //手動讓狀態(tài)欄刷新
??????? [self setNeedsStatusBarAppearanceUpdate];
??? }
??? - (BOOL)prefersStatusBarHidden
??? {
??????? return _isShow;
??? }
事件響應(yīng)鏈,關(guān)鍵時候能幫很大的忙
//事件響應(yīng)鏈:
//1:自己處理不會再傳遞事件的響應(yīng),如果自己不處理交給自己的父視圖處理
//2:父視圖不處理-》交給父視圖-》視圖控制器的view-》視圖控制器->UIWindow->UIApplication->Appdelegate->丟棄
//只要觸摸屏幕就會調(diào)用下列方法
//這兩種方式都可以讓事件傳遞下去,優(yōu)先使用super的方式傳遞事件
//super 不僅可以讓事件進(jìn)行傳遞下去,還會做更多的操作
//直接 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{
??? //觸摸結(jié)束
??? NSLog(@"ViewController:touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
??? //觸摸取消,譬如來電時會結(jié)束當(dāng)前的一個觸摸
??? NSLog(@"ViewController:touchesCancelled");
}
//----------------------------------------------------------------------------------------------------
#pragma mark -------------------------------------
#pragma mark UITabBarController
- (void)UITabBarController{
//==============================================================
??? 使用系統(tǒng)的 tabBar 來生成
??? //UITabBarControler也是一個容器controller,本身的view不顯示,顯示自己孩子的viewController
??? //里面維護(hù)一個數(shù)組,viewControllers包含所有的子viewController
??? //通常TabBarController包含navigtionControoller
??? FirstViewController *firstViewController = [[FirstViewController alloc]init];
??? UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:firstViewController];
??? //使用自定義TabBarItem,title標(biāo)題
??? //在沒有指定選中圖片的時候,默認(rèn)使用藍(lán)色進(jìn)行選中時候的著色
??? //firstViewController.tabBarItem.title?? //標(biāo)題
??? //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,維護(hù)一個數(shù)組,把生成的 viewController,加進(jìn)去就可以了
??? //也可以 創(chuàng)建一個UINavigationController,用initWithRootViewController:firstViewController把firstViewController設(shè)為UINavigationController的跟頁面,把UINavigationController生成的對象作為,TabBar 的一個子頁面
??? tabBarController.viewControllers = @[firstViewController,secondViewController,thirdViewController,fourViewController,fiveViewController,sixViewController];
??? //可以制定tintColor給tabbarItem進(jìn)行選中時的著色
??? tabBarController.tabBar.tintColor = [UIColor redColor];
??? //設(shè)置 tabBar 的顏色
??? tabBarController.tabBar.barTintColor = [UIColor purpleColor];
??? //設(shè)置tabBar的背景圖片
????? [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_black_bg"]];
??? //如果子viewController個數(shù)超過5個,UITabBarController自動幫我們聲稱more的導(dǎo)航控制器,把剩余的tabBarItem放到more對應(yīng)的視圖控制器之內(nèi)
??? //設(shè)置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
{
??? //紀(jì)錄當(dāng)前選中的是哪一個viewController
??? //1:獲取viewController的次序
??? //2:保存到沙盒下
??? //3:從沙盒目錄中讀取
??? //4:啟動后重新設(shè)置選中的次序
??? NSLog(@"--->%ld",tabBarController.selectedIndex);
?? ?
??? //得到我的沙盒目錄 sandbox
?? ?
??? NSString *homePath = NSHomeDirectory();
??? NSLog(@"%@",homePath);
?? ?
??? //NSUserDefaults? 是使用本地化存儲的方式之一,
??? //實現(xiàn)的原理是在沙盒目錄下,保存的一個plist文件
??? //只適合保存比較少量數(shù)據(jù),譬如用戶名,密碼,開機歡迎界面是否顯示的標(biāo)志
??? [[NSUserDefaults standardUserDefaults] setInteger:tabBarController.selectedIndex forKey:@"currentSelectIndex"];
??? //注意要調(diào)用同步方法,由于NSUserDefault不會把數(shù)據(jù)馬上寫到本地文件
??? //會有延遲,調(diào)用synchronize 同步方法,會讓數(shù)據(jù)馬上同步到本地文件
??? [[NSUserDefaults standardUserDefaults] synchronize];
?? ?
??? //把key對應(yīng)的值讀取出來
??? [[NSUserDefaults standardUserDefaults] integerForKey:@"currentSelectIndex"];
}
?
#pragma mark -------------------------------------
#pragma mark 自定義 TabBar
?- (void)customTabBar
??? {
??????? //先把自己的tabBar欄隱藏
??????? [self.tabBar setHidden:YES];
?????? ?
??????? //使用UIImageView充當(dāng)我們的背景
??????? 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 = @[@"歷史",@"收藏",@"分享",@"設(shè)置"];
?????? ?
??????? 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];
?????????? ?
??????????? //設(shè)置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可以設(shè)置他image的內(nèi)邊距,也可以設(shè)置title的內(nèi)邊距,來調(diào)整button上文字和圖片的位置
??????????? //button.imageEdgeInsets 設(shè)置圖片的內(nèi)邊距
??????????? //button.titleEdgeInsets 設(shè)置title的內(nèi)邊距
??????????? 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];
?????????? ?
??????????? //設(shè)置初始顯示時選中第一個按鈕
??????????? if (index == 0) {
??????????????? button.selected = YES;
??????????? }
?????????? ?
??????????? [backgroundView addSubview:button];
??????? }
??? }
?
- (void)buttonClick:(UIButton*)button
{
??? //重置button的狀態(tài)
??? [self resetButtonStatus];
??? NSInteger index = button.tag - 100;
?? ?
??? //讓button處于select狀態(tài)
??? button.selected = YES;
?? ?
??? //修改UITabBarControler的selectedIndex ,會導(dǎo)致切換到對應(yīng)的頁面
??? 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];
?? ?
??? //設(shè)置textView的代理
??? textView.delegate = self;
??? //自適應(yīng)大小時設(shè)置為 no 不會跳動
??? //沒有自適應(yīng)大小時,設(shè)置為 yes 可以超過本身行數(shù)輸入,可以滑動瀏覽
??? textView.scrollEnabled = NO;
?? ?
??? //使用它定制鍵盤
??? //textView.inputView
}
#pragma mark UITextView代理方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
??? NSLog(@"是否開始編輯");
??? return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
??? NSLog(@"是否應(yīng)該");
??? return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
??? NSLog(@"應(yīng)開始編輯");
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
??? NSLog(@"已經(jīng)結(jié)束編輯");
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
??? //指示輸入的字符是否應(yīng)該接收,這里可以有自己的判斷邏輯
??? if ([text isEqualToString:@"\n"]) {
??????? //\n代表回車
??????? //第一響應(yīng)者的概念跟UITextField一樣
??????? [textView resignFirstResponder];
??????? return NO;
??? }
??? return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
??? //輸入框的文本發(fā)生了變化
??? 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:@"標(biāo)題" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"other" otherButtonTitles:@"A",@"B",@"C", nil];
??? [actionSheet showInView:self.view];
}
?
- (void)testUIAlertView
{
??? //警告框,通常使用在提示用戶,或者讓用戶做出選擇
??? //對于otherButtonTitles,如果不需要,直接寫為nil
??? UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"標(biāo)題" message:@"警告內(nèi)容" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"A",@"B",@"C", nil];
??? [alterView show];
?? ?
??? //??? UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"是否繼續(xù)操作" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
??? //??? [alterView show];
}
#pragma mark UIActionSheet代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{??? //按鈕序號從上到下0——>
??? //還是要根據(jù)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對應(yīng)的button,他的index 是0,其他的根據(jù)次序順序排列
??? //這樣我們根據(jù)buttonIndex來決定響應(yīng)了哪一個按鈕
??? 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能顯示的內(nèi)容的大小
??? //如果不設(shè)置contentSize的大小默認(rèn)contentSize的大小就是scrollView的view的frame的大小
??? scrollView.contentSize = CGSizeMake(imageView.bounds.size.width, imageView.bounds.size.height);
??????? //scrollView是否有回彈效果
??? scrollView.bounces = NO;
??? //是否顯示水平滾動條
??? scrollView.showsHorizontalScrollIndicator = NO;
??? //是否顯示垂直滾動條
??? scrollView.showsVerticalScrollIndicator;
??? //重點:
??? //設(shè)置scrollView的內(nèi)容偏移量
??? //cotentOffset的內(nèi)容視圖的左上角到scrollView左上角的一個偏移量
??? //contentOffset.x? x軸的偏移量
??? //contentOffset.y? y軸的偏移量
??? //計算時以內(nèi)容視圖的左上角為基準(zhǔn)
??? //contentOffset默認(rèn)從(0,0)開始
??? scrollView.contentOffset = CGPointMake(100, 0);
??? //設(shè)置scorllView的代理,當(dāng)滾動事件事件發(fā)生時,相應(yīng)的代理方法被調(diào)用
??? scrollView.delegate = self;
??? //設(shè)置scollView以分頁的效果顯示
??? scrollView.pagingEnabled = YES;
??? //minimumZoomScale 設(shè)置最小的放大系數(shù)
??? scrollView.minimumZoomScale = 1.0;
??? //設(shè)置最大的放大系數(shù)
??? scrollView.maximumZoomScale = 2.0;
??? //添加雙擊事件進(jìn)行縮放
??? 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滾動就會調(diào)用該方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
??? //由于該方法在視圖滾動中一直調(diào)用,所以不要在這里做耗時的計算
??? NSLog(@"視圖滾動");
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
??? NSLog(@"滾動視圖內(nèi)容即將被拖動");
}
//decelerate 指定是否有減速動作
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
??? NSLog(@"滾動視圖的內(nèi)容已經(jīng)結(jié)束拖動");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
??? NSLog(@"即將開始減速");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
??? NSLog(@"減速結(jié)束,內(nèi)容視圖停止");
}
//指定在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有多少頁,由他決定圓點的個數(shù)
??? _pageControl.numberOfPages = 6;
?? ?
??? //pageIndicatorTintColor 圓點的顏色
??? _pageControl.pageIndicatorTintColor = [UIColor blueColor];
?? ?
??? //currentPageIndicatorTintColor? 當(dāng)前頁的顏色
??? _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);
??? //需要使用代碼的方式滾動到相應(yīng)的位置
??? [_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];
??? }
?? ?
??? //這里設(shè)置以下contentSize的大小,指定scrollView顯示內(nèi)容的區(qū)域大小
??? _scrollView.contentSize = CGSizeMake(6*_scrollView.bounds.size.width, _scrollView.bounds.size.height);
}
//只要設(shè)置了scrollView的分頁顯示,當(dāng)手動(使用手指)滾動結(jié)束后,該代理方法會被調(diào)用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
??? NSLog(@"scrollViewDidEndDecelerating");
??? //得到當(dāng)前的偏移
??? 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);
?? ?
??? //設(shè)置pageController的當(dāng)前頁,對應(yīng)的原點高亮顯示
??? _pageControl.currentPage = pageNumber;
}
//該代理是使用代碼的方式設(shè)置contentOffset后的代理
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
??? NSLog(@"scrollViewDidEndScrollingAnimation");
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/Lvfengxian/p/4902666.html
總結(jié)
以上是生活随笔為你收集整理的温故而知新,UI学习中的大部分控件及常用的基础都整理了一下,很长~~~~~~~~~很长!!!!!!!...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1022 词法分析程序总结
- 下一篇: 数据量大和高并发解决方法