手势

作者: 张家杰仔 | 来源:发表于2017-04-06 10:42 被阅读16次
 //给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;
        }
    }
}

相关文章

  • 手势

    点击手势 捏合手势 旋转手势 轻扫手势 拖拽手势 边缘平移手势 长按手势

  • iOS-手势详细参数说明

    敲击手势 长按手势 滑动手势 拖动手势 旋转手势 捏合手势 两种手势作用在同一个视图

  • 【iOS学习】——手势识别

    iOS 手势 手势需要开启用户交互 点击手势 单击手势 双击手势 添加 numberOfTapsRequired...

  • Swift - UIGestureRecognizer 各种手势

    1、点击手势2、拖动手势3、长按手势4、滑动手势5、捏合手势6、旋转手势 完整代码

  • iOS七种手势详解

    1、轻拍手势 2、捏合手势 3、旋转手势 4、平移手势 5、边缘轻扫手势 6、长按手势 7、轻扫手势 给image...

  • iOS手势总结

    1.轻拍手势 2.长按手势 3.轻扫手势 4.平移手势 5.捏合手势 6.旋转手势 7.边缘手势

  • iOS 手势

    修改时间: 2016-12-19修改次数: 0 手势传递 点击手势 捏合手势 轻扫手势 拖动手势 长按手势

  • iOS手势操作

    iOS手势有六种 手势类型: 手势状态: 创建View添加手势 1.轻点手势( UITapGestureRecog...

  • Vue手势

    点击手势 滑动手势 手势的方法

  • UI梳理——手势

    手势分类: 手势的创建: 方法的实现: 轻扫手势:UISwipeGestureRecognizer 长按手势: 以...

网友评论

      本文标题:手势

      本文链接:https://www.haomeiwen.com/subject/fbutattx.html