手势

 //给UIImageView添加交互不许打开UIImageView的交互(只有UIImageView才需要这步)
    self.imageView.userInteractionEnabled = YES;
#pragma mark 手势(重点)
    //单击手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action_gesture:)];
    
    //拖拽手势
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(action_gesture:)];
    
    //缩放手势
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(action_gesture:)];
 
    //添加长按手势

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

     [self addGestureRecognizer:longPress];



    //给视图添加手势
    [self.imageView addGestureRecognizer:tapGesture];
    [self.imageView addGestureRecognizer:panGesture];
    [self.imageView addGestureRecognizer:pinchGesture];
- (void)action_gesture:(UIGestureRecognizer *)gesture{
    if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
//        NSLog(@"单击手势");
        [UIView animateWithDuration:2 animations:^{/**< 每一个动画都是一个线程 */
            //动画
            //只有animatable修饰的属性才会有动画效果
            //多个不同的属性时,会同时执行
            //同一属性的不同值,会执行后一个
            gesture.view.alpha = 0.3;
            gesture.view.center = CGPointMake(200, 500);
//            gesture.view.center = CGPointMake(0, 0);
        } completion:^(BOOL finished) {
            // 没有动画
//            [UIView animateWithDuration:2 animations:^{
//                //这样就可以做动画了
//            }];
            
        }];
       [UIView animateWithDuration:2 delay:2 options:0 animations:^{
           self.imageView.alpha = 1.0;
           self.imageView.center = self.view.center;
       } completion:^(BOOL finished) {
           //完成过后做什么操作
           [UIView animateWithDuration:2 animations:^{
               //形变:
               //缩放
               CGAffineTransform transform = CGAffineTransformMakeScale(0.7, 0.7);
               //旋转
               self.imageView.transform = CGAffineTransformRotate(transform, M_PI);
           }completion:^(BOOL finished) {
               // 还原所有形变(回到初始值)
               [UIView animateWithDuration:2 animations:^{
                   self.imageView.transform = CGAffineTransformIdentity;
               }];
               
           }];
       }];
      
    }
    
    if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]) {
        //拖拽手势
        UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer *)gesture;/**< 强制转换 */
        if (panGesture .state == UIGestureRecognizerStateChanged) {
            //改变视图的位置:
            //1、拿到手势在视图上的移动位置
            CGPoint transitionPiont = [panGesture translationInView:self.view];
            //2、改变视图的位置
            panGesture.view.center = CGPointMake(panGesture.view.center.x + transitionPiont.x, panGesture.view.center.y + transitionPiont.y);
            //3、重置手势的移动位置(CGPointZero表示(0,0))
            [panGesture setTranslation:CGPointZero inView:self.view];
        }else if (panGesture.state == UIGestureRecognizerStateEnded){
        //结束手势后的操作
            NSLog(@"手势结束");
        }
    }
    
    if ([gesture isKindOfClass:[UIPinchGestureRecognizer class]]) {
         UIPinchGestureRecognizer *pinchGesture = (UIPinchGestureRecognizer *)gesture;
        if (pinchGesture.state == UIGestureRecognizerStateChanged) {
            //缩放手势
            //改变视图的形变属性
            //        pinchGesture.view.transform = CGAffineTransformMakeScale(2, 2);
            pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale); //在某个的基础上再做形变
            //还原缩放系数
            pinchGesture.scale = 1;
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容