关于手势

// 手势 Gesture

// 1.轻拍Tap

UITapGestureRecognizer *tap = [[UITapGestureRecognizer allocinitWithTarget:self action:@selector(tapAction:)];


// 设置需要点击几次才会触发方法

tap.numberOfTapsRequired = 2;

tap.numberOfTouchesRequired = 2;


// 把手势加入到图片上

[imageView addGestureRecognizer:tap];

// 内存管理



- (void)tapAction:(id)sender {

    NSLog(@"taped an image");


}


// 2.长按 LongPress

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer allocinitWithTarget:self action:@selector(longPress:)];

// 设置一下长按需要的最短时间

longPress.minimumPressDuration = 3;

// 判断在长按过程中允许手指移动的距离

longPress.allowableMovement = 100;

[imageView addGestureRecognizer:longPress];


- (void)longPress:(UILongPressGestureRecognizer *)longPress {

    // 长按的方法在手势的各个状态中都会进行触发,所以需要进行判断

    if (longPress.state == UIGestureRecognizerStateBegan) {

        NSLog(@"开始长按了");

    }

    

    NSLog(@"long pressed");


}



// 3.旋转 rotation

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer allocinitWithTarget:self action:@selector(rotationAction:)];

[imageView addGestureRecognizer:rotation];



- (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture {

    // 获得添加手势的视图

    UIImageView *imageView = (UIImageView *)[rotationGesture view];

    // 调整视图的transform属性

    imageView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);


}


// 4.捏合 pinch

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer allocinitWithTarget:self action:@selector(pinchAction:)];

[imageView addGestureRecognizer:pinch];



- (void)pinchAction:(UIPinchGestureRecognizer *)pinchGesture {

    UIImageView *imageView = (UIImageView *)[pinchGesture view];

    

    imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);

    

//    imageView.transform = CGAffineTransformScale(imageView.transform, pinchGesture.scale, pinchGesture.scale);

//    pinchGesture.scale = 1;


}


// 5.拖拽 pan

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer allocinitWithTarget:self action:@selector(panAction:)];

[imageView addGestureRecognizer:pan];



- (void)panAction:(UIPanGestureRecognizer *)panGesture {

    // 获取拖拽手势添加的视图

    UIImageView *imageView = (UIImageView *)[panGesture view];

    // 获取手势经过的点

    CGPoint p = [panGesture translationInView:imageView];

    // 然后对视图的transform属性进行改变

    imageView.transform = CGAffineTransformMakeTranslation(p.x, p.y);

//    imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);

//    [panGesture setTranslation:CGPointZero inView:imageView];


}


// 6.轻扫

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer allocinitWithTarget:self action:@selector(swipeAction:)];

[imageView addGestureRecognizer:swipe];

[swipe release];


- (void)swipeAction:(UISwipeGestureRecognizer *)swipeGesture {

    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {

        NSLog(@"向左");

    } else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {

        NSLog(@"向右");

    } else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {

        NSLog(@"向上");

    } else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {

        NSLog(@"向下");

    }


}


转载于:https://my.oschina.net/u/437446/blog/397780

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值