码迷,mamicode.com
首页 > 其他好文 > 详细

手势识别器

时间:2015-10-12 20:57:25      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

UIImageView

    这个类是iOS专门用来显示图片的类,几乎所有的图片,都是用这个类显示的。

初始化方法:initWithImage:

 

 UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.png"]];

    img.frame = CGRectMake(100, 100, 200, 200);

 

通过图片名来初始化一个UIImageView类。

注意:    

ImageView的默认是NO,是关闭交互。要将userInteractionEnabled(响应者链是否交互)设置为YES

self.img.userInteractionEnabled = YES;

 

手势

 

手势是加在某个视图上的,否则手势无效。

轻拍手势:

 // 轻拍手势

    // 创建轻拍手势识别器

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

 

 

// 轻拍手势响应方法

- (void)tapAction:(UITapGestureRecognizer *)sender{

    NSLog(@"我是轻拍手势..");

    // 换图片

   // 获取手势图片

    UIImageView *temp = (UIImageView *)sender.view;

    temp.image = [UIImage imageNamed:@"3.png"];

}

 

长按手势:

 

 // 长按手势   

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];

    longPress.minimumPressDuration = 0.5;

[self.img addGestureRecognizer:longPress];

 

 

// 长按响应方法

- (void)longPressAction:(UILongPressGestureRecognizer *)sender{

    NSLog(@"长按手势");

   

    if (sender.state == UIGestureRecognizerStateBegan) {

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

        [UIView animateWithDuration:1 animations:^{

            // 修改view的frame

            CGRect temp1 = sender.view.frame;

            temp1.size.width +=50;

            temp1.size.height +=50;

            // 修改完成附回

            sender.view.frame = temp1;

           

            sender.view.alpha = 0.5;

        }];

    }else if (sender.state == UIGestureRecognizerStateEnded){

        NSLog(@"长按结束..");

        [UIView animateWithDuration:1 animations:^{

            // 修改view的frame

            CGRect temp1 = sender.view.frame;

            temp1.size.width -=50;

            temp1.size.height -=50;

            // 修改完成附回

            sender.view.frame = temp1;

           

            sender.view.alpha = 1;

        }];

    }

}

 

 

旋转手势:

  // 旋转   

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

[self.img addGestureRecognizer:rotation];

// 旋转手势响应方法

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

    NSLog(@"我是旋转手势..");

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

    sender.rotation = 0;

}

 

捏合手势:

 // 捏合

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

[self.img addGestureRecognizer:pinch];

 

// 捏合手势响应方法

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

    NSLog(@"我是捏合手势");

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

    sender.scale = 1;

}

 

平移手势:

 

 // 平移

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

[self.img addGestureRecognizer:pan];

 

// 平移手势响应方法

 

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

    NSLog(@"我是平移手势..");

    CGPoint point = [sender translationInView:sender.view];

    sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);

    // 将偏移量归零

    [sender setTranslation:CGPointZero inView:sender.view];

}

 

边缘触发:

 

 

手势识别器

标签:

原文地址:http://www.cnblogs.com/Coder-GT/p/4872557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!