0128——手势Gesture

UIGestureRecognizer:

1.locationinView 获取手势在某个视图里面的坐标位置

2.delegate监听手势的行为

3.state状态

  开始:UIGestureRecognizerStateBegan  手势达到要求

  识别:UIGestureRecognizerStateRecognized  手势识别的最后一刻

  改变:UIGestureRecognizerStateChanged  手势过程当中

  结束:UIGestureRecognizerStateEnded  手势结束

  取消:UIGestureRecognizerStateCancelled

  失败:UIGestureRecognizerStateFailed

1.单击

UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(handleSingleFingerEvent:)];

singleFingerOne.numberOfTouchesRequired = 1; 手指数

singleFingerOne.numberOfTapsRequired = 1; tap次数

2.长按

UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];

longpressGesutre.minimumPressDuration=1;  最少长按时间

longpressGesutre.allowableMovement=15; 最大15像素的运动是手势识别所允许的

3.滑动

UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];

swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft; 不设置是右  多个方向用|或

4.捏

UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(PinchGesture:)];

-(void) pinchGesture:(id)sender{
     UIPinchGestureRecognizer *gesture = sender;     
    //手势改变时
    if (gesture.state == UIGestureRecognizerStateChanged){
        //捏合手势中scale属性记录的缩放比例
        _imageView.transform = CGAffineTransformScale(_imageView.transform,gesture.scale, gesture.scale);
    }    
    //结束后恢复
    if(gesture.state==UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:0.5 animations:^{
            _imageView.transform = CGAffineTransformIdentity;//取消一切形变
        }];
    }
}
5.旋转
UIRotateGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)]; 
-(void)rotationGesture:(id)sender{    
  UIRotateGestureRecognizer *gesture = sender;      
  if (gesture.state==UIGestureRecognizerStateChanged){        
    _imageView.transform=CGAffineTransform=Rotation(_imageView.transform,gesture.rotation);    
  }       
  if(gesture.state==UIGestureRecognizerStateEnded){        
    [UIView animateWithDuration:1 animations:^{            
      _imageView.transform=CGAffineTransformIdentity;//取消形变   
      }];   
   }   
 }

6.多手势

ios开发中,默认是在同一时间只能有一个手势被执行,要实现多个手势同时进行,须实现UIGestureRecognizerDelegate,并重写函数

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

return YES;

}

上一篇:jquery ajax 报交请求返回 HTTP 400 错误


下一篇:原生 js 模拟 alert 弹窗