美文网首页
iOS手势事件

iOS手势事件

作者: 那笑容不是为我 | 来源:发表于2016-02-02 22:25 被阅读134次

    什么是手势?

    用过苹果手机的朋友都知道,我们的屏幕上有个"小圆点","小圆点"当你对它做出不同动作的时候会有不同的响应事件,拖拽平移来移动它的位置,点击实现各种按键功能的实现,这样大大为用户提供了便利,并且可以有效地保护按键.

    那么类似这种手势事件在我们的开发中是怎么实现的?

    UIGestureRecognizer

    UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势.UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,我们在开发中使用的是UIGestureRecognizer的子类:
    UITapGestureRecognizer

        UIPinchGestureRecognizer
    
        UIRotationGestureRecognizer
    
        UISwipeGestureRecognizer
    
        UIPanGestureRecognizer
    
        UILongPressGestureRecognizer
    

    见名知意,Tap(轻拍),Pinch(捏合),Rotation(旋转),Swipe(滑动,快速移动,是用于监测滑动的方向的),Pan(拖移,慢速移动,是用于监测偏移的量的),以及LongPress(长按)

    下面来介绍一下这些手势最基本的使用方法:

    @interface RootViewController ()
    @property(nonatomic,strong)UIView *greenView;
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    self.view.backgroundColor =[ UIColor whiteColor];
        
        self.greenView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        _greenView.backgroundColor = [UIColor greenColor];
        [self.view addSubview:_greenView];
        
    #pragma mark ---tap手势
        
        //创建手势,并初始化,使用initWithTarget: action:方法创建
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
        
        //设置属性
        tap.numberOfTapsRequired = 2;//次数
        tap.numberOfTouchesRequired = 2;//手指数
        
        //添加到视图上
    //    [_greenView addGestureRecognizer:tap];
        
    #pragma mark ----swipe手势
        
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
        //属性
        swipe.direction = 0;
        
        
    //    [_greenView addGestureRecognizer:swipe];
        
    #pragma mark ----longPress手势
        
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressView:)];
        
        longPress.minimumPressDuration = 2;
        
    //    [_greenView addGestureRecognizer:longPress];
        
    #pragma mark ----- pan平移手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    //    [_greenView addGestureRecognizer:pan];
        
        #pragma mark  ----- pinch捏合手势
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    //    [_greenView addGestureRecognizer:pinch];
        
    #pragma mark ----- Rotation旋转手势
        
        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
    //    [_greenView addGestureRecognizer:rotation];
    
    }
    
    //旋转手势事件
    -(void)rotationView:(UIRotationGestureRecognizer *)sender{
        
        //rotation 旋转角度
        
        sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
        
        //清除增量
        sender.rotation = 0.0;
        
    }
    
    //平移手势事件
    -(void)panView:(UIPanGestureRecognizer *)sender{
        
        CGPoint point = [sender translationInView:_greenView];
    //    sender.view.transform = CGAffineTransformMake(1, 0, 0, 1, point.x, point.y);
        
        //每次移动都是从原来的位置移动
    //    sender.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
        
        //以上次的位置为标准
        sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);
        //增量置为0
        [sender setTranslation:CGPointZero inView:_greenView];
    }
    
    //长按手势事件
    -(void)longPressView:(UILongPressGestureRecognizer *)sender{
        
        
        if (sender.state == UIGestureRecognizerStateBegan) {
            NSLog(@"长按状态!");
        }    
        
    }
    
    //捏合手势事件
    -(void)pinchView:(UIPinchGestureRecognizer *)sender{
        
        //以上一次所方位标准
        sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
        
        //重新设置缩放比例,1是正常缩放,小于1是缩小,大于1是放大
        sender.scale = 1;
    }
    
    
    -(void)swipeView:(UISwipeGestureRecognizer *)sender{
        //randomColor是自己写的一个UIColor的类目方法(随机色)
        _greenView.backgroundColor = [UIColor randomColor];
    }
    
    -(void)tapView:(UITapGestureRecognizer *)sender{
        
        _greenView.backgroundColor = [UIColor randomColor];
    }
    

    这就是开发中最基本的手势用法,大家都会了吗?

    相关文章

      网友评论

          本文标题:iOS手势事件

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