注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

遗忘之地

回忆真实的世界 忏悔曾经的过往

 
 
 

日志

 
 

关于几种手势简单编写  

2013-04-03 10:42:55|  分类: ios开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#import "ViewController.h"

-(void)viewDidLoad

{

    [super viewDidLoad];

    

    UIImage* image=[UIImage imageNamed:@"5.png"];

    _imageView=[[UIImageView alloc]initWithFrame:CGRectMake(120180100100)];

    _imageView.image=image;

    

    _imageView.userInteractionEnabled=YES;

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

    _size=CGSizeMake(100100);

    [_imageView addGestureRecognizer:tap];

    [tap release];

    

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

    [_imageView addGestureRecognizer:pan];

    [pan release];

    

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

    [_imageView addGestureRecognizer:pinch];

    [pinch release];

    

    UIRotationGestureRecognizer* rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:selfaction:@selector(rotation:)];

    [_imageView addGestureRecognizer:rotation];

    [rotation release];

    

    [self.view addSubview:_imageView];

    [_imageView release];

    

}

-(void)tap:(UITapGestureRecognizer*)tap

{

    NSLog(@"tap");

}

-(void)pan:(UIPanGestureRecognizer*)pan

{

    CGPoint point=[pan translationInView:self.view];

    

//    _imageView.center=CGPointMake(point.x+_imageView.center.x, point.y+_imageView.center.y);

    _imageView.frame=CGRectMake(_imageView.frame.origin.x+point.x, _imageView.frame.origin.y+point.y, _imageView.frame.size.width, _imageView.frame.size.height);

    [pan setTranslation:CGPointMake(00) inView:self.view];

}

-(void)pinch:(UIPinchGestureRecognizer*)pinch

{

    _imageView.bounds=CGRectMake(00, _size.width*pinch.scale, _size.height*pinch.scale);

    if (pinch.state==UIGestureRecognizerStateEnded) {

      _size=_imageView.bounds.size;

    }

    

}

-(void)rotation:(UIRotationGestureRecognizer*)rotation

{

    _imageView.transform=CGAffineTransformMakeRotation(_rotation+rotation.rotation);

    if (rotation.state==UIGestureRecognizerStateEnded) {

        _rotation+=rotation.rotation;

    }

}

@end


那么这个问题有解吗?答案是肯定的,UIGestureRecognizer 有个方法叫做requireGestureRecognizerToFail,他可以指定某一个 recognizer,即便自己已经滿足條件了,也不會立刻触发,会等到该指定的 recognizer 确定失败之后才触发。以同时支持单点与双点的手势为例,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (void)viewDidLoad {
    // 单击的 Recognizer
    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSingleTapFrom)];
    singleTapRecognizer.numberOfTapsRequired = 1; // 单击
    [self.view addGestureRecognizer:singleRecognizer];
     
    // 双击的 Recognizer
    UITapGestureRecognizer* double;
    doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleDoubleTapFrom)];
    doubleTapRecognizer.numberOfTapsRequired = 2; // 双击
    [self.view addGestureRecognizer:doubleRecognizer];
     
    // 关键在这一行,如果双击确定偵測失败才會触发单击
    [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];
    [singleRecognizer release];
    [doubleRecognizer release];
}
  评论这张
 
阅读(43)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018