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

手势识别器

时间:2015-08-06 09:30:11      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:手势识别   手势识别器   手势   

在实现手势识别器之前 需要建立 一个UIImageView来存放图片
原代码:

UIImage *image =[UIImage imageNamed:@"图片名.后缀"];

    self.imageView =[[UIImageView alloc ] initWithImage:image];
    self.imageView.frame =CGRectMake(30, 100, 300, 400);
    [self.view addSubview:self.imageView];
    [self.imageView release];

手势的使用

1.点击

原代码:
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
设置点击几次才能触发方法(默认是1)
tap.numberOfTapsRequired =2;
设置几根手指进行点击(默认是1)
tap.numberOfTouchesRequired =1;
将手势添加到图片上
[self.imageView addGestureRecognizer:tap];   
[tap release];
#pragma mark 点击的方法
-(void)tapClick:(UITapGestureRecognizer *)tap{
    NSLog(@"测试一下手势");
    self.imageView.image =[UIImage imageNamed:@"7af40ad162d9f2d3efbd7dd2abec8a136327cc5b.jpg"];
}

2.长按

UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
设置长按触发的最小时间(默认是0.5)
longPress.minimumPressDuration =0.5;
用户手指在长按过程中允许移动的距离(默认是10)
longPress.allowableMovement =200;
把手势添加到图片中
[self.imageView addGestureRecognizer:longPress];
[longPress release];
#pragma mark 长按对应的响应方法
-(void)longPressClick:(UILongPressGestureRecognizer *)longPress{
NSLog(@”测试长按手势”);
// 长按的状态
// longPress.state;
长按之后弹出一个UIAlertView
if(self.alertView == nil){
self.alertView =[[UIAlertView alloc] initWithTitle:@”长按” message:@”进入” delegate:self cancelButtonTitle:@”确定” otherButtonTitles:@”取消”, nil];
}
[self.alertView show];
[self.alertView release];
self.alertView =nil;
}

3.旋转

创建一个旋转的手势
UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationClick:)];
 把手势放到对应的图片上
[self.imageView addGestureRecognizer:rotation];
释放
[rotation release];

#pragma mark 旋转对应的响应方法(通过图片的手势,让图片发生旋转)
-(void)rotationClick:(UIRotationGestureRecognizer *)rotation{

    // 可以通过手势获取手势添加的是图示是哪一个
    UIImageView *imageView = (UIImageView *)rotation.view;

    // 进行旋转的操作
    // 通过视图的transfrom属性,让视图进行旋转
//    imageView.transform =CGAffineTransformMakeRotation(rotation.rotation);
    imageView.transform =CGAffineTransformRotate(imageView.transform, rotation.rotation);
    // 防止转的太快,斗;
    rotation.rotation =0;
}

4.捏合

创建
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc ] initWithTarget:self action:@selector(pinchClick:)];
 添加到图片上
 [self.imageView addGestureRecognizer:pinch];
  释放
[pinch release];
#pragma mark 捏合对应的响应方法 (通过捏合手势,缩放图片)

-(void)pinchClick:(UIPinchGestureRecognizer *)pinch{
    // 通过手势找到对应的视图
    UIImageView *imageView =(UIImageView *)pinch.view;

    // 进行捏合的操作
    // 通过transform改变图片的尺寸
    imageView.transform =CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
    // 为了防止手势的变化让图片直接消失(停止动作)
    pinch.scale =1;  
}

5.拖拽

 UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];
 [self.imageView addGestureRecognizer:pan];
 [pan release];
 #pragma mark 拖拽对应的响应方法(通过拖拽手势,让视图随着手势移动)

-(void)panClick:(UIPanGestureRecognizer *)pan{
    // 通过手势找视图
    UIImageView *imageView =(UIImageView *)pan.view;
    // 通过手势获得经过的点
//    imageView.transform =CGAffineTransformMakeScale(pan., <#CGFloat sy#>)
    CGPoint p=[pan translationInView:imageView];
    //设置移动的位置
    imageView.transform =CGAffineTransformTranslate(imageView.transform, p.x, p.y);
    // 为了防止手势在操作的时候视图消失
    [pan setTranslation:CGPointZero inView:imageView];
}

6.轻扫

UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];
[self.imageView addGestureRecognizer:swipe];
[swipe release];
轻扫的方向
向右
swipe.direction =UISwipeGestureRecognizerDirectionRight;
# pragma mark 轻扫对应的响应
-(void)swipeClick:(UISwipeGestureRecognizer *)swipe{
    if (swipe.direction ==UISwipeGestureRecognizerDirectionRight ) {
        NSLog(@"向右");
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

手势识别器

标签:手势识别   手势识别器   手势   

原文地址:http://blog.csdn.net/mltianya/article/details/47299889

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