iOS手势操作

iOS手势有六种

手势类型:

/**
轻点手势( UITapGestureRecognizer);
长按手势(UILongPressGestureRecognizer);
轻扫手势(UISwipeGestureRecognizer);
平移手势(UIPanGestureRecognizer);
捏合手势(UIPinchGestureRecognizer);
旋转手势(UIRotationGestureRecognizer);
*/

手势状态:

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    UIGestureRecognizerStatePossible,   // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
    UIGestureRecognizerStateBegan,      // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
    UIGestureRecognizerStateChanged,    // 手势状态发生转变
    UIGestureRecognizerStateEnded,      // 手势识别操作完成(此时已经松开手指)
    UIGestureRecognizerStateCancelled,  // 手势被取消,恢复到默认状态
    UIGestureRecognizerStateFailed,     // 手势识别失败,恢复到默认状态
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded
};

创建View添加手势

 UIImage *image = [UIImage imageNamed:@"YellowSquare"];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);//尺寸和图片的尺寸一样大
    imageView.center = self.view.center;//居中显示
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];

1.轻点手势( UITapGestureRecognizer)

    //1.创建手势对象
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    //2.手势相关的属性
    //点击次数(默认1)
    tap.numberOfTapsRequired = 1;
    //手指的个数(默认1)
    tap.numberOfTouchesRequired = 1;
    //3.把手势与视图相关联
    [imageView addGestureRecognizer:tap];
关联轻点手势响应方法(tapClick:)
-(void)tapClick:(UITapGestureRecognizer *)tap{
    
    NSLog(@"轻点手势响应了!");
    
}

2.长按手势(UILongPressGestureRecognizer)

    //长按手势
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
    //用几个手指触屏,默认1
    longPress.numberOfTouchesRequired = 1;
    //设置最短长按时间,单位为秒(默认0.5)
    longPress.minimumPressDuration = 1;
    //设置手势识别期间所允许的手势可移动范围
    longPress.allowableMovement = 10;
    [imageView addGestureRecognizer:longPress];

关联手势响应方法(longPressGestureClick:)
-(void)longPressClick:(UILongPressGestureRecognizer *)press{
    //state属性是所有手势父类提供的方法,用于记录手势的状态
    if(press.state == UIGestureRecognizerStateBegan){
        NSLog(@"长按手势开始响应!");
    }else if (press.state == UIGestureRecognizerStateChanged){
        NSLog(@"长按手势状态发生改变!");
    }else{
        NSLog(@"长按手势结束!");
    }

}

3.轻扫手势(UISwipeGestureRecognizer)

//轻扫手势
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureClick:)];
    //设置轻扫的方向
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipe];
    
    
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureClick:)];
    //右扫
    rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipe];
关联手势响应方法(longPressGestureClick:)
-(void)swipeClick:(UISwipeGestureRecognizer *)swpie{

    //如果是左扫
    if (swpie.direction == UISwipeGestureRecognizerDirectionLeft ) {
        self.view.backgroundColor = [UIColor magentaColor];
        NSLog(@"左扫!");
    }else{
        self.view.backgroundColor = [UIColor greenColor];
        NSLog(@"右扫!");
    }
    
}

4.平移手势(UIPanGestureRecognizer);

 //平移手势
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureClick:)];
    [imageView addGestureRecognizer:pan];
关联手势响应方法(panGestureClick:)
-(void)panClick:(UIPanGestureRecognizer *)pan{
    
    NSLog(@"响应了。。。");
    //通过pan手势,能够获取到pan.view在self.view上的偏移量
    CGPoint point = [pan translationInView:self.view];
    NSLog(@"x=%.2lf y=%.2lf",point.x,point.y);
    //改变中心点坐标(原来的中心点+偏移量=当前的中心点)
    CGPoint newCenter = CGPointMake(pan.view.center.x + point.x, pan.view.center.y + point.y);
//    CGPointZero<==>CGPointMake(0,0)
    
    //限制拖动范围
    newCenter.y = MAX(pan.view.frame.size.height/2, newCenter.y);
    newCenter.y = MIN(self.view.frame.size.height - pan.view.frame.size.height/2,  newCenter.y);
    newCenter.x = MAX(pan.view.frame.size.width/2, newCenter.x);
    newCenter.x = MIN(self.view.frame.size.width - pan.view.frame.size.width/2, newCenter.x);
    pan.view.center = newCenter;
    
    //每次调用之后,需要重置手势的偏移量,否则偏移量会自动累加
    [pan setTranslation:CGPointZero inView:self.view];


}

5.捏合手势(UIPinchGestureRecognizer)

   //捏合手势
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichGestureClick:)];
    [imageView addGestureRecognizer:pinch];
关联手势响应方法(pichGestureClick:)
-(void)pichClick:(UIPinchGestureRecognizer *)pinch{
    //缩放的系数
    NSLog(@"%.2lf",pinch.scale);
    //固定写法
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    //重置缩放系数(否则系数会累加)
    pinch.scale = 1.0;
}

6.旋转手势(UIRotationGestureRecognizer)

  //旋转手势
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureClick:)];
    [imageView addGestureRecognizer:rotation];
关联手势响应方法(rotationGestureClick:)
-(void)rotationClick:(UIRotationGestureRecognizer *)rotation{
    //rotation.rotation 手势旋转的角度
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    //重置角度
    rotation.rotation = 0;
}

还有自定义手势就不写了,以上6中手势就是平时开发常见的

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容