您好,登录后才能下订单哦!
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。