iOS 之手势识别器 UIGestureRecognizer

苹果为我们封装了7种手势识别

UIGestureRecognizer

  1. UITapGestureRecognizer
  2. UIPinchGestureRecognizer
  3. UIRotationGestureRecognizer
  4. UISwipeGestureRecognizer
  5. UIPanGestureRecognizer
  6. UIScreenEdgePanGestureRecognizer
  7. UILongPressGestureRecognizer

     UITapGestureRecognizer 轻拍动作

设置方法:

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 2;
        [imageView addGestureRecognizer:tap];
        [tap release];

效果为点击imageView两次执行tapAction时间.

属性

  1. numberOfTapsRequired 是点击的次数,可以用这个属性来分别单击和双击事件,(另一种用touch事件中得延迟事件,但是延迟比较严重,效果不好)
  2. numberOfTouchesRequired 是点击的手指的个数,可以用来检测多个手指点击

    UIPinchGestureRecognizer 捏合 ,用来放大和缩小

代码

        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
         [imageView addGestureRecognizer:pinch]
         [pinch release];

        - (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
            //改变图片的比例
            imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
            pinch.scale = 1.0;
        }

属性

1.scale 两个手指捏合的比例

    UIRotationGestureRecognizer  旋转

代码

        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
        [imageView addGestureRecognizer:rotation];
        [rotation release];

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
    rotation.rotation = 0;
}

属性

    UISwipeGestureRecognizer 清扫 向4个方向运动

代码

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
                //设置手势触发的方向,支持水平或垂直方向的清扫
                swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
                [imageView addGestureRecognizer:swipeLeft];
                [swipeLeft release];
            UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
            //设置手势触发的方向,支持水平或垂直方向的清扫
            swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
            [imageView addGestureRecognizer:swipeRight];
            [swipeRight release];
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{

    if (swipe.direction == 1) {
        imageIndex++;
        if (imageIndex == 8) {
            imageIndex = 1;
        }
        NSString *name = [NSString stringWithFormat:@"%d", imageIndex];
        imageView.image = [UIImage imageNamed:name];
        NSLog(@"right -- %@",name);
    }
    if (swipe.direction == 2) {
        imageIndex--;
        if (imageIndex == 0) {
            imageIndex = 7;
        }
        NSString *name = [NSString stringWithFormat:@"%d",imageIndex];
        imageView.image = [UIImage imageNamed:name];
        NSLog(@"left -- %@", name);
    }
    NSLog(@"%lu",(unsigned long)swipe.direction);
}

属性

    UIPanGestureRecognizer 拖拽

代码

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
                [imageView addGestureRecognizer:pan];
                [pan release];
    - (void)panAction:(UIPanGestureRecognizer *)pan{
    CGPoint point = [pan translationInView:imageView];
    //修改图片的坐标
    imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);
    [pan setTranslation:CGPointZero inView:imageView];
}

属性

    UIScreenEdgePanGestureRecognizer 边缘运动

代码

UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
            screen.edges = UIRectEdgeLeft;
            [imageView addGestureRecognizer:screen];
            [screen release];

属性

    UILongPressGestureRecognizer 长按 (进行截屏)

代码

     UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpressAction:)];
                [imageView addGestureRecognizer:longpress];
                [longpress release];
- (void)longpressAction:(UILongPressGestureRecognizer *)longpress{
    if (longpress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按");
        //屏幕截屏
        UIGraphicsBeginImageContext(self.view.frame.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        //viewImage就是获取的截图,如果要将图片存入相册.只需在后面调用
        UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
}

属性

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值