UIGestureRecognizer
10年積累的做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有回民免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
利用手勢(shì)識(shí)別器,能夠輕松識(shí)別用戶在某個(gè)view上面做一些常見的手勢(shì)
UIGestureRecognizer是一個(gè)抽象類,定義了所有手勢(shì)的基本行為,使用它的子類才能處理具體的手勢(shì)
UITapGestureRecognizer 敲擊
UIPinchGestureRecognizer 捏合手勢(shì)
UIPanGestureRecognizer 拖拽
UISwipeGestureRecognizer 輕掃
UIRotationGestureRecognizer 旋轉(zhuǎn)
UILongPressGestureRecognizer 長(zhǎng)按
手勢(shì)識(shí)別器使用的一般步驟 (以敲擊手勢(shì)為例)
創(chuàng)建手勢(shì)識(shí)別器對(duì)象
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init];
設(shè)置手勢(shì)識(shí)別器對(duì)象的具體屬性 :
//連續(xù)敲擊2次且兩根手指敲擊時(shí)響應(yīng) tap.numberOfTapsRequired = 2; tap.numberOfTouchesRequired = 2;
添加手勢(shì)識(shí)別器到view上 :
[self.iconView addGestureRecognizer:tap];
監(jiān)聽手勢(shì)的觸發(fā) :
[tap addTarger:self action:@selector(tapIconView:)];
實(shí)現(xiàn)觸發(fā)的方法
- (void) tapIconView:(UITapGestureRecognizer*) tapGR { NSLog(@"tap響應(yīng)"); }
手勢(shì)識(shí)別器的狀態(tài)
手勢(shì)識(shí)別器的代理
代理協(xié)議
//是否接受這個(gè)touch對(duì)象(默認(rèn)返回YES),這個(gè)代理方法在一個(gè)觸摸事件產(chǎn)生時(shí)先調(diào)用 - (BOOL) gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldReceiveTouch:(UITouch*) touch; //確定兩個(gè)類似的手勢(shì)識(shí)別器是否同時(shí)處理 - (BOOL) gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer;
示例:UITabBarController的滑動(dòng)手勢(shì)控制
1)UITabBarController子類,保存所有子控制器的截圖
添加UIImage對(duì)象數(shù)組
@property (nonatomic, strong) NSMutableArray* picArray;
數(shù)組初始化為空對(duì)象
NSMutableArray * arr1 = [NSMutableArray array]; for ( int i =0; i重寫selectedIndex的setter方法:切換子控制器前,截圖保存
- (void)setSelectedIndex:(NSUInteger)selectedIndex { //截圖操作 float width = [UIScreen mainScreen].bounds.size.width; float height = [UIScreen mainScreen].bounds.size.height; UIGraphicsBeginImageContext(CGSizeMake(width, height)); CGContextRef context = UIGraphicsGetCurrentContext(); [[UIApplication sharedApplication].keyWindow.layer renderInContext:context]; UIImage *p_w_picpath = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //截圖保存到截圖數(shù)組 NSLog(@"selectedIndex=%ld", self.selectedIndex); self.picArray[self.selectedIndex] = p_w_picpath; [super setSelectedIndex:selectedIndex]; }提供方法,返回當(dāng)前子控制器的前后兩個(gè)子控制器的截圖
- (UIImage *) p_w_picpathForCurrentViewControllerLeft { if ( self.selectedIndex == 0 ) { return nil; } return self.picArray[self.selectedIndex-1]; } - (UIImage *) p_w_picpathForCurrentViewControllerRight { if ( self.selectedIndex == self.viewControllers.count - 1 ) { return nil; } return self.picArray[self.selectedIndex+1]; }2)添加子控制器的共用父類
UITabBarController的所有子控制器的類型就是繼承自一個(gè)共用父類
父類中添加滑動(dòng)手勢(shì)識(shí)別器
UIPanGestureRecognizer * gr1 = [[UIPanGestureRecognizer alloc] init]; [gr1 addTarget:self action:@selector(panGR:)]; [self.view addGestureRecognizer:gr1];父類中添加UIImageView屬性,用于存儲(chǔ)當(dāng)前頁(yè)面左右兩個(gè)截圖p_w_picpathView
@property (nonatomic, weak) UIImageView * ivLeft; @property (nonatomic, weak) UIImageView * ivRight;手勢(shì)識(shí)別器:手勢(shì)開始時(shí),當(dāng)前view左右放置截圖
UIImage * imgLeft = [((ViewController*)self.tabBarController) p_w_picpathForCurrentViewControllerLeft]; if ( imgLeft == nil ) { self.ivLeft = nil; } else { if ( [imgLeft isKindOfClass:[NSNull class]] ) { //一張白***片 imgLeft = [UIImage p_w_picpathNamed:@"white_bg"]; } UIImageView * ivLeft = [[UIImageView alloc] initWithImage:imgLeft]; self.ivLeft = ivLeft; [self.view addSubview:ivLeft]; self.ivLeft.frame = CGRectMake(-self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height); } UIImage * imgRight = [((ViewController*)self.tabBarController) p_w_picpathForCurrentViewControllerRight]; if ( imgRight == nil ) { self.ivRight = nil; } else { if ( [imgRight isKindOfClass:[NSNull class]] ) { imgRight = [UIImage p_w_picpathNamed:@"white_bg"]; } UIImageView * ivRight= [[UIImageView alloc] initWithImage:imgRight]; self.ivRight = ivRight; [self.view addSubview:self.ivRight]; self.ivRight.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height); }手勢(shì)識(shí)別器:手勢(shì)滑動(dòng)式時(shí),當(dāng)前view隨之滑動(dòng)
if ( p.x > 0 && self.ivLeft == nil ) { return ; } if ( p.x < 0 && self.ivRight == nil ) { return ; } self.view.transform = CGAffineTransformMakeTranslation(p.x, 0);手勢(shì)識(shí)別器,手勢(shì)結(jié)束時(shí),根據(jù)位置確定是否切換子控制器
if ( self.view.transform.tx > self.view.frame.size.width/2 && self.ivLeft != nil ) { self.view.transform = CGAffineTransformIdentity; self.tabBarController.selectedIndex--; if ( self.ivLeft != nil ) { [self.ivLeft removeFromSuperview]; self.ivLeft = nil; } if ( self.ivRight != nil ) { [self.ivRight removeFromSuperview]; self.ivRight = nil; } return ; } if ( self.view.transform.tx < -self.view.frame.size.width/2 && self.ivRight != nil ) { self.view.transform = CGAffineTransformIdentity; self.tabBarController.selectedIndex++; if ( self.ivLeft != nil ) { [self.ivLeft removeFromSuperview]; self.ivLeft = nil; } if ( self.ivRight != nil ) { [self.ivRight removeFromSuperview]; self.ivRight = nil; } return ; } [UIView animateWithDuration:0.5 animations:^{ self.view.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { if ( self.ivLeft != nil ) { [self.ivLeft removeFromSuperview]; self.ivLeft = nil; } if ( self.ivRight != nil ) { [self.ivRight removeFromSuperview]; self.ivRight = nil; } }];
本文題目:UIKit框架(16)手勢(shì)識(shí)別器
分享路徑:http://weahome.cn/article/gcehpj.html