码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 手势

时间:2016-03-18 10:45:56      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

  手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。iOS 中手势包括:UITapGestureRecognizer(点击),UIPinchGestureRecognizer(捏合),UIRotationGestureRecognizer(旋转),UISwipeGestureRecognizer(轻扫),UIPanGestureRecognizer(移动),UILongPressGestureRecognizer(长按)。上面的类属性和方法都比较简单,有几个方法个人感觉不容易理解的,在这里说明一下。

  

- (void)viewDidLoad {
    [super viewDidLoad];
    
    image1 = [[UIImageView alloc]initWithFrame:CGRectMake(80, 20, 200, 200)];
    image1.image = [UIImage imageNamed:@"0.jpg"];
    image1.userInteractionEnabled = YES;
    [self.view addSubview:image1];
    //点击
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [image1 addGestureRecognizer:tap];
    //捏合
    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pin:)];
    [image1 addGestureRecognizer:pin];
    //旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    [image1 addGestureRecognizer:rotation];
    //轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    [image1 addGestureRecognizer:swipe];
    //移动
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [image1 addGestureRecognizer:pan];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void) tap:(UITapGestureRecognizer *)gestureRecognizer
{
    CGPoint point1 = [gestureRecognizer locationInView:image1];

    NSLog(@"point1 = %@",NSStringFromCGPoint(point1));
}
- (void)pin:(UIPinchGestureRecognizer *)gestureRecognizer
{
    NSLog(@"scale = %f;velocity = %f",gestureRecognizer.scale,gestureRecognizer.velocity);
    image1.transform = CGAffineTransformMakeScale(gestureRecognizer.scale, gestureRecognizer.scale);
}
- (void)rotation:(UIRotationGestureRecognizer *)gestureRecognizer
{
    image1.transform = CGAffineTransformMakeRotation(gestureRecognizer.rotation);
}

- (void)swipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
    image1.image = [UIImage imageNamed:@"2.jpg"];
}
- (void)pan:(UIPanGestureRecognizer *)gestureRecognizer
{
    //相对于手指开始的位置
    CGPoint point1 = [gestureRecognizer translationInView:image1];
    //相对于View左上角的位置
    CGPoint point2 = [gestureRecognizer locationInView:image1];
    
    CGFloat centerX  = image1.center.x;
    CGFloat centerY = image1.center.y;
    
    image1.center = CGPointMake(centerX + point1.x, centerY + point1.y);
    //注意这里
    [gestureRecognizer setTranslation:CGPointMake(0, 0) inView:image1];
    
    NSLog(@"point1 = %@ ; point2 = %@",NSStringFromCGPoint(point1),NSStringFromCGPoint(point2));
}

手势的使用很简单,demo就不上传了。

自定义手势

自定义手势继承:UIGestureRecognizer,实现下面的方法:

– touchesBegan:withEvent:  
– touchesMoved:withEvent:  
– touchesEnded:withEvent:  
- touchesCancelled:withEvent:
 

手势冲突处理

- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;

如果otherGestureRecognizer执行失败,则执行调用者的手势。

  

iOS 手势

标签:

原文地址:http://www.cnblogs.com/yzvictory/p/5291111.html

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