您的位置:首页 > 移动开发 > IOS开发

iOS 手势添加(代码和storyboard)

2015-08-06 15:07 821 查看
IOS手势一共六种:

轻击手势(TapGestureRecognizer)
轻扫手势(SwipeGestureRecognizer)
长按手势(LongPressGestureRecognizer)
拖动手势(PanGestureRecognizer)
捏合手势(PinchGestureRecognizer)
旋转手势(RotationGestureRecognizer)
我们可以根据自己需要添加手势

一:storyboard添加手势
1:拖拽手势到需要添加的view



添加完成后会生成一个手势:



然后我们就可以通过拖线到controller中来实现业务逻辑,和拖button的点击事件一样

二:代码创建手势:

1.轻击手势(TapGestureRecognizer)的添加
初始化代码TapGestureRecongnizer的代码如下:

//新建tap手势
     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    //设置点击次数和点击手指数
    tapGesture.numberOfTapsRequired = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
    [self.view addGestureRecognizer:tapGesture];


回调方法处理逻辑:

//轻击手势触发方法
    -(void)tapGesture:(id)sender
     {
         //轻击后要做的事情
     }


2.长按手势(LongPressGestureRecognizer)
初始化代码:

//添加长摁手势
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
         //设置长按时间
         longPressGesture.minimumPressDuration = 0.5; //(2秒)
         [self.view addGestureRecognizer:longPressGesture];


回调方法处理逻辑:
常用状态:

开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed

//常摁手势触发方法
     -(void)longPressGesture:(id)sender
     {
         UILongPressGestureRecognizer *longPress = sender;
         if (longPress.state == UIGestureRecognizerStateBegan)
         {
             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"长按事件" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
             [alter show];
         }
     }


3.轻扫手势(SwipeGestureRecognizer)
在初始化轻扫手势的时候得指定轻扫的方向,上下左右。 如果要要添加多个轻扫方向,就得添加多个轻扫手势,不过回调的是同一个方法。
添加轻扫手势,一个向左一个向右,代码如下
//添加轻扫手势
         UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
         //设置轻扫的方向
         swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默认向右
         [self.view addGestureRecognizer:swipeGesture];
    
         //添加轻扫手势
         UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
         //设置轻扫的方向
         swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默认向右
         [self.view addGestureRecognizer:swipeGestureLeft];
回调:

//轻扫手势触发方法
     -(void)swipeGesture:(id)sender
     {
         UISwipeGestureRecognizer *swipe = sender;
         if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
         {
             //向左轻扫做的事情
         }
         if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
         {
             //向右轻扫做的事情
         }
     }


4.捏合手势(PinchGestureRecognizer)
捏合手势初始化
//添加捏合手势
         UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
         [self.view addGestureRecognizer:pinchGesture];
回调(放大或缩小)

//捏合手势触发方法
     -(void) pinchGesture:(id)sender
     {
          UIPinchGestureRecognizer *gesture = sender;
    
         //手势改变时
         if (gesture.state == UIGestureRecognizerStateChanged)
         {
             //捏合手势中scale属性记录的缩放比例
             _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
         }
    
         //结束后恢复
         if(gesture.state==UIGestureRecognizerStateEnded)
         {
             [UIView animateWithDuration:0.5 animations:^{
                 _imageView.transform = CGAffineTransformIdentity;//取消一切形变
             }];
         }
     }


5.拖动手势(PanGestureRecognizer)
拖动手势的初始化
//添加拖动手势
         UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
         [self.view addGestureRecognizer:panGesture];
回调(通过translationInView获取移动的点,和TouchesMoved方法类似)

//拖动手势
     -(void) panGesture:(id)sender
     {
         UIPanGestureRecognizer *panGesture = sender;
    
         CGPoint movePoint = [panGesture translationInView:self.view];
    
         //做你想做的事儿
     }


6.旋转手势(RotationGestureRecognizer)
旋转手势的初始化
//添加旋转手势
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
         [self.view addGestureRecognizer:rotationGesture];
回调:

//旋转手势
     -(void)rotationGesture:(id)sender
     {
    
         UIRotationGestureRecognizer *gesture = sender;
    
         if (gesture.state==UIGestureRecognizerStateChanged)
         {
             _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
         }
    
         if(gesture.state==UIGestureRecognizerStateEnded)
         {
    
             [UIView animateWithDuration:1 animations:^{
                 _imageView.transform=CGAffineTransformIdentity;//取消形变
             }];
         }
         
     }


本文链接:http://mobile.51cto.com/iphone-453679.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: