十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
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响应"); }
手势识别器的状态
手势识别器的代理
代理协议
//是否接受这个touch对象(默认返回YES),这个代理方法在一个触摸事件产生时先调用 - (BOOL) gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldReceiveTouch:(UITouch*) touch; //确定两个类似的手势识别器是否同时处理 - (BOOL) gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer;
示例:UITabBarController的滑动手势控制
1)UITabBarController子类,保存所有子控制器的截图
添加UIImage对象数组
@property (nonatomic, strong) NSMutableArray* picArray;
数组初始化为空对象
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(); //截图保存到截图数组 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; } }];另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前名称:UIKit框架(16)手势识别器-创新互联
标题网址:http://6mz.cn/article/dedech.html