iOS手势

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    _rotateImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

    _rotateImageview.image = [UIImage imageNamed:@"1.png"];

    _rotateImageview.userInteractionEnabled = YES;

    [self.view addSubview:_rotateImageview];

    

    // Tap 单击手势

    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];

    [_rotateImageview addGestureRecognizer:singleTapGestureRecognizer];

    

    // Tap 双击手势

    UITapGestureRecognizer *doubleGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];

    doubleGestureRecognizer.numberOfTapsRequired = 2;

//    [doubleGestureRecognizer setNumberOfTouchesRequired:2]; //两根手指点击

    [_rotateImageview addGestureRecognizer:doubleGestureRecognizer];

    

    /*单击手势和双击手势共存问题*/

    // 没有检测到doubleTapGestureRecognizer或者检测doubleTapGestureRecognizer失败singleTapGestureRecognizer才有效

    [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleGestureRecognizer];

    

    // LongPress 长按手势

    UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];

    [_rotateImageview addGestureRecognizer:longPressGestureRecognizer];

    

    // Pan 拖动手势

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]

                                                    initWithTarget:self

                                                    action:@selector(handlePan:)];

    [_rotateImageview addGestureRecognizer:panGestureRecognizer];

    

    // Pinch 缩放手势

    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]

                                                        initWithTarget:self action:@selector(handlePinch:)];

    [_rotateImageview addGestureRecognizer:pinchGestureRecognizer];

    

    // Rotation 旋转手势

    UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]

                                                     initWithTarget:self

                                                     action:@selector(handleRotate:)];

    [_rotateImageview addGestureRecognizer:rotateRecognizer];

    

}


#pragma mark 单击

- (void)handleTap:(UITapGestureRecognizer *)recognizer {

    NSLog(@"图片被单击");

}


#pragma mark 双击

- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer {

    NSLog(@"图片被双击");

}



// 其实时因为响应不同的状态。所以,要在你的方法里加上状态的判断。可以看到,开始响应,结束响应,如果你不判断的话,都会调用你的方法。

#pragma mark 长按手势

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {

    if (recognizer.state == UIGestureRecognizerStateBegan) {

        NSLog(@"图片被长按");

    }

}


#pragma mark 拖动手势

- (void)handlePan:(UIPanGestureRecognizer*) recognizer

{

    CGPoint translation = [recognizer translationInView:self.view];

    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,

                                         recognizer.view.center.y + translation.y);

    [recognizer setTranslation:CGPointZero inView:self.view];

    

}


#pragma mark 缩放手势

- (void)handlePinch:(UIPinchGestureRecognizer*) recognizer

{

    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);

    recognizer.scale = 1;

}


#pragma mark 旋转手势

- (void)handleRotate:(UIRotationGestureRecognizer*) recognizer

{

    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);

    recognizer.rotation = 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值