您好,登錄后才能下訂單哦!
UIGestureRecognizer
利用手勢識別器,能夠輕松識別用戶在某個view上面做一些常見的手勢
UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,使用它的子類才能處理具體的手勢
UITapGestureRecognizer 敲擊
UIPinchGestureRecognizer 捏合手勢
UIPanGestureRecognizer 拖拽
UISwipeGestureRecognizer 輕掃
UIRotationGestureRecognizer 旋轉
UILongPressGestureRecognizer 長按
手勢識別器使用的一般步驟 (以敲擊手勢為例)
創建手勢識別器對象
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init];
設置手勢識別器對象的具體屬性 :
//連續敲擊2次且兩根手指敲擊時響應 tap.numberOfTapsRequired = 2; tap.numberOfTouchesRequired = 2;
添加手勢識別器到view上 :
[self.iconView addGestureRecognizer:tap];
監聽手勢的觸發 :
[tap addTarger:self action:@selector(tapIconView:)];
實現觸發的方法
- (void) tapIconView:(UITapGestureRecognizer*) tapGR { NSLog(@"tap響應"); }
手勢識別器的狀態
手勢識別器的代理
代理協議<UIGestureRecognizerDelegate>
//是否接受這個touch對象(默認返回YES),這個代理方法在一個觸摸事件產生時先調用 - (BOOL) gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldReceiveTouch:(UITouch*) touch; //確定兩個類似的手勢識別器是否同時處理 - (BOOL) gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer;
示例:UITabBarController的滑動手勢控制
1)UITabBarController子類,保存所有子控制器的截圖
添加UIImage對象數組
@property (nonatomic, strong) NSMutableArray<UIImage*> * picArray;
數組初始化為空對象
NSMutableArray * arr1 = [NSMutableArray array]; for ( int i =0; i<self.viewControllers.count; i++) { [arr1 addObject:[NSNull null]]; } self.picArray = [arr1 mutableCopy];
重寫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(); //截圖保存到截圖數組 NSLog(@"selectedIndex=%ld", self.selectedIndex); self.picArray[self.selectedIndex] = p_w_picpath; [super setSelectedIndex:selectedIndex]; }
提供方法,返回當前子控制器的前后兩個子控制器的截圖
- (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的所有子控制器的類型就是繼承自一個共用父類
父類中添加滑動手勢識別器
UIPanGestureRecognizer * gr1 = [[UIPanGestureRecognizer alloc] init]; [gr1 addTarget:self action:@selector(panGR:)]; [self.view addGestureRecognizer:gr1];
父類中添加UIImageView屬性,用于存儲當前頁面左右兩個截圖p_w_picpathView
@property (nonatomic, weak) UIImageView * ivLeft; @property (nonatomic, weak) UIImageView * ivRight;
手勢識別器:手勢開始時,當前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); }
手勢識別器:手勢滑動式時,當前view隨之滑動
if ( p.x > 0 && self.ivLeft == nil ) { return ; } if ( p.x < 0 && self.ivRight == nil ) { return ; } self.view.transform = CGAffineTransformMakeTranslation(p.x, 0);
手勢識別器,手勢結束時,根據位置確定是否切換子控制器
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; } }];
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。