6种手势

1.手势UIGestureRecognizer

UIGestureRecognizer是一个抽象类我们不能直接使用,他有6个子类(tap,pan,swip,long,scale,raition)。他的父类NSObject。

2.常用属性和函数

//函数
创建
 -(instancetype)initWithTarget:target action(SEL)action
 单独添加target方法 可以添加多个手势
 -(void)addTarget:target action:(SEL)action
 删除手势
 -(void)removeTarget:(id)target action:(SEL)action


- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer; //手势依赖(手势互斥)方法  设置手势优先级 A requireRecognizerToFail B 手势条件都满足时B触发,A不会
 
 
 
 //获取在传入view的点击位置的信息方法
 - (CGPoint)locationInView:(nullable UIView*)view;
 
 
 //(touchIndex 是第几个触摸点)用来获取多触摸点在view上位置信息的方法
 - (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;




常用属性
UIGestureRecognizerState state; 手势状态 只读
 //手势状态枚举值
 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
 UIGestureRecognizerStatePossible,   // 默认的状态,这个时候的手势并没有具体的情形状态
 UIGestureRecognizerStateBegan,      // 手势开始被识别的状态
 UIGestureRecognizerStateChanged,    // 手势识别发生改变的状态
 UIGestureRecognizerStateEnded,      // 手势识别结束,将会执行触发的方法
 UIGestureRecognizerStateCancelled,  // 手势识别取消
 UIGestureRecognizerStateFailed,     // 识别失败,方法将不会被调用
 UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded


 
id <UIGestureRecognizerDelegate> delegate;代理
 
BOOL enabled 手势是否有效 默认YES
 
UIview *view 获取手势所在的view 只读
 
 BOOL cancelsTouchesInView 取消view上面的touch事件响应 默认YES 就是拦截了,手势触发了就不会发送touchesCancelled:withEvent:消息
 
BOOL delaysTouchsBegan    延迟touch事件开始 默认NO
BOOL delaysTouchsEnded    延迟touch事件结束 默认YES
NSArray<NSNumber *> *allowedTouchTypes 允许touch类型的数组
NSArray<NSNumber *> *allowedPressTypes 允许按压press的类型数组
BOOL requiresExclusiveTouchType  是否只允许一种touchType 类型,是否同时只接受一种触摸类型 默认YES
NSUInteger numberOfTouches;  获取触摸点数



代理方法
 开始进行手势识别时调用,返回NO,手势识别失败
 -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
 
 手势触发屏幕后回调方法,返回NO手势识别失败
 -(BOOL)getureRecognizer:(uiGestureRecognizer*)gestureRecognizer;

2.子类类型

UITapGestureRecognizer 点击

  UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapclack)];
    
    //点击次数 默认单击
    tapGesture.numberOfTapsRequired = 2;
    //同时点击的手指数
  //  tapGesture.numberOfTouchesRequired = 2;
    
   [self.imageView addGestureRecognizer:tapGesture];


-(void)tapclack{
    
    NSLog(@"点击了");
    
    
}

UIPinchGestureRecognizer 捏合
 

 //属性 CGFloat scale 缩放比例
 // CGFloat velocty  设置捏合速度 只读
    
    UIPinchGestureRecognizer *pingGest = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinClack:)];
  
    [self.imageView addGestureRecognizer:pingGest];
    





-(void)pinClack:(UIPinchGestureRecognizer *)pin{
    
    
    NSLog(@"%f,%f",pin.scale,pin.velocity);
   //固定写法
   pin.view.transform = CGAffineTransformScale(pin.view.transform, pin.scale, pin.scale);
    //重置缩放系数(否则系数会累加)
    pin.scale = 1.0;
}

 

 

UIRotationGestureRecognizer 旋转

-(void)rotation{
//属性 CGFloat rotation 旋转角度
//旋转速度 CGFloat velocity 只读
    
    UIRotationGestureRecognizer *rotationGest = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationClack:)];
    [self.imageView addGestureRecognizer:rotationGest];
    
    
    
    
}



-(void)rotationClack:(UIRotationGestureRecognizer *)rotationGest{
    
    
    rotationGest.view.transform = CGAffineTransformRotate(rotationGest.view.transform,rotationGest.rotation);
    NSLog(@"旋转角度%f",rotationGest.rotation);
    
    
}

UISwipeGestureRecognizer 轻扫

-(void)swipe{
    //常用属性 NSInteger numberOfTouchesRequired 触发轻扫手势所需触摸点数量
    //UISwipeGestureRecognizerDirection direction 轻扫手势方向
    
    //左
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];
    //轻扫方向
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
       [self.imageView addGestureRecognizer:swipeLeft];
    //右
    UISwipeGestureRecognizer *swipRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];
    swipRight.direction = UISwipeGestureRecognizerDirectionRight;
       [self.imageView addGestureRecognizer:swipRight];
    
    //上
    UISwipeGestureRecognizer *swipUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];
    swipUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self.imageView addGestureRecognizer:swipUp];
    //下
    UISwipeGestureRecognizer *swipDrown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];
    swipDrown.direction = UISwipeGestureRecognizerDirectionDown;
    [self.imageView addGestureRecognizer:swipDrown];
}


-(void)swipClack:(UISwipeGestureRecognizer *)swipe{
    
    switch (swipe.direction) {
        case UISwipeGestureRecognizerDirectionLeft:
            NSLog(@"左");
            break;
        case UISwipeGestureRecognizerDirectionRight:
            NSLog(@"右");
            break;
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"上");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"下");
            break;
        default:
            break;
    }
    
    
    
    
    
}

 

UIPanGestureRecognizer 滑动

-(void)pan{
   /*常用属性
    NSUInteger minimumNumberOfTouchches 设置触发移动拖拽最少触发点数量 默认1
    NSUInteger maximumNumberOfTouches   设置触发拖拽最多触发点
    -(void)setTranslation:(CGPoint)translation inView(UIView *)view 设置当前位置
    -(CGPoint)translationInView:(UIView)view 获取当前位置
    -(void)setVelocityInView:(UIView *)view 设置平移速度
    -(CGPoint)velocityInView:(UIView *)view 获取平移速度
    
    */
    
    
    UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panClack:)];
    
    
    
    [self.imageView addGestureRecognizer:panGest];
    
    
    
    
    
    
}


-(void)panClack:(UIPanGestureRecognizer *)pan{
    
   //获取imageview在self.view上的便宜量
    CGPoint point = [pan translationInView:self.view];
    NSLog(@"%f--%f",point.x,point.y);
    
    
    //移动到的中心点 本来中心点+便宜量
    CGPoint newCenter = CGPointMake(pan.view.center.x + point.x, pan.view.center.y+point.y);
  
    
    //设置拖动范围
    newCenter.x = MAX(pan.view.frame.size.width,newCenter.x);
    
    //最小范围
    //newCenter.x = MIN();
    
    
    
    //没错调用后,重之手势便宜量
    [pan setTranslation:CGPointZero inView:self.imageView];
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始响应");
    }else if (pan.state == UIGestureRecognizerStateChanged){
        
        NSLog(@"手势状态发生改变");
        
    }else{
        
        NSLog(@"手势结束");
    }
    
    
    
    
}



 

UILongPressGestureRecognizer 长按

-(void)LongPress{
    
    /*属性
     NSUInteger numberOfTapsRequired。    设置触发前的点击次数
     NSUInteger numberOfTouchesReauired  设置触发的触摸点数
     CGTimeInterval minimumPressDuration 设置最短长按时间
     CGFloat allowableMovement 设置在按触时允许移动的最大距离,默认10像素
     */
    
    
    UILongPressGestureRecognizer *longPressGest = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClack:)];
    
    //触摸点 默认1
    longPressGest.numberOfTapsRequired = 1;
    
    //设置最短长按时间 秒 默认 0.5
    longPressGest.minimumPressDuration = 1;
    
    //设置手势识别期间所允许的手势可移动范围
    //longPressGest.allowableMovement = 10;
    
    
    
    
    [self.imageView addGestureRecognizer:longPressGest];

}
-(void)longPressClack:(UILongPressGestureRecognizer *)longpress{
    
    if (longpress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按手势开始响应");
    }else if (longpress.state == UIGestureRecognizerStateChanged){
        
        
        NSLog(@"长按手势状态发生改变");
        
    }else{
        
        NSLog(@"长按手势结束");
    }
    
    
    
    
    
    
}

3.解决手势冲突

 - (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer; //手势依赖(手势互斥)方法  设置手势优先级 A requireRecognizerToFail B 手势条件都满足时B触发,A不会

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值