代码改变世界

五大手势的使用

2015-09-29 23:34  真实16  阅读(302)  评论(0编辑  收藏  举报

tap(点击),loogPress(长按),Rotation(旋转),Pinch(捏合),swipe(轻扫),pan(拖拽)

*** 如果不响应手势,有以下几种原因

1、看是否添加到了ImageView 上,如果是把UserInteractionEnabled 设置成YES

2、查看是否设置 背景颜色,如果没有设置,设置背景颜色

3、手势的冲突,这时就需要设置手势的优先级,(截获触发事件,手势)设置优先级

**五大手势继承的父类:UIGestureRecognizer

 

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

//    设置点击手指的个数

    tap.numberOfTouchesRequired = 1;

    

//   numberOfTapsRequired 设置手指点击的次数

    tap.numberOfTapsRequired = 1;

 

//   addGestureRecognizer: 添加手势

        [self.view addGestureRecognizer:tap];

    

#pragma mark--------------长按--------------------------

//    初始化

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

//    设置按下的最小持续时间 默认0.5s

    longPress.minimumPressDuration = 1;

 

    

//    longPress.numberOfTouchesRequired = 2;

    

   //   addGestureRecognizer: 把手势添加到某个视图上

    [self.view addGestureRecognizer:longPress];

 

#pragma mark ---------拖拽-----------------

//    初始化

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

//    minimumNumberOfTouches 设置最少的手指个数

    pan.minimumNumberOfTouches = 2;

//    maximumNumberOfTouches

    [self.view addGestureRecognizer:pan];

 

#pragma mark-------------轻扫------------------------

//    初始化

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];

//    direction 设置轻扫的方向

    swipe.direction = UISwipeGestureRecognizerDirectionLeft;

//    处理手势冲突的思想 :谁的手势执行时间短就先执行谁  方法:requireGestureRecognizerToFail

//   需要轻扫手势 执行完之后  在去执行拖拽手势

    [pan requireGestureRecognizerToFail/*处理拖拽和轻扫 两个手势冲突的方法*/:swipe/*先执行的*/];

    [self.view addGestureRecognizer:swipe];

    

#pragma mark--------------捏合--------------------------

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

    

    [self.view addGestureRecognizer:pinch];

#pragma mark---------------旋转-------------------------

    

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

    

    

    [self.view addGestureRecognizer:rotation];

}

 

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

    

//    通过旋转手势 让图片进行相同的变形

    imageView.transform = CGAffineTransformMakeRotation(sender.rotation);

    

    

}

 

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

//    transform该变原来的形态 (如果不恢复它就一直以这个形态下去)与手势无关

    

//    通过手势 得到的 变化比例 让imageView的形态发生改变

    imageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);

//    imageView.transform = CGAffineTransformScale(imageView.transform, sender.scale, sender.scale);

    imageView.center = self.view.center;

    

}

 

- (void)swipe:(UISwipeGestureRecognizer *)sender{

    

    self.view.frame = CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds), 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));

    [UIView animateWithDuration:2 animations:^{

        

        self.view.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));

    }];

    

    NSLog(@"dsdb");

}

 

 

 

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

    //    locationInView: 获取点击的那个触摸点

    CGPoint tapLoc = [sender locationInView:self.view];

    NSLog(@"%f %f",tapLoc.x,tapLoc.y);

    

           //    点击的位置设置成图片视图的这些点

        imageView.center = tapLoc;

   

 

}

 

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

    

    //    locationInView: 获取点击的那个触摸点

    CGPoint tapLoc = [sender locationInView:self.view];

    NSLog(@"%f %f",tapLoc.x,tapLoc.y);

    //    动画

    [UIView animateWithDuration:0.2 animations:^{

        //    点击的位置设置成图片视图的这些点

        imageView.center = tapLoc;

    }];

    

 

    

}

 

 

//单击手势的方法

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

 

//    让视图恢复原来的形态

    imageView.transform = CGAffineTransformIdentity;

    

//    self.view.backgroundColor = [UIColor redColor];

//    locationInView: 获取点击的那个点

    CGPoint tapLoc = [sender locationInView:self.view];

    NSLog(@"%f %f",tapLoc.x,tapLoc.y);

//    动画

    [UIView animateWithDuration:1 animations:^{

        //    点击的位置设置成图片视图的这些点

        imageView.center = tapLoc;

    }];

 

    

}