iOS学习之手势

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS学习之手势相关的知识,希望对你有一定的参考价值。

UIGestureRecognizer

  为了完成手势识别,必须借助于手势识别器-UIGestureRecognizer,利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势,要实现<UIGestureRecognizerDelegate>

手势状态:

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {

     UIGestureRecognizerStatePossible,   // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态

     UIGestureRecognizerStateBegan,      // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成

    UIGestureRecognizerStateChanged,    // 手势状态发生转变

     UIGestureRecognizerStateEnded,      // 手势识别操作完成(此时已经松开手指)

     UIGestureRecognizerStateCancelled,  // 手势被取消,恢复到默认状态

     UIGestureRecognizerStateFailed,     // 手势识别失败,恢复到默认状态

     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded };

 

常用的六种手势:

  1. 轻击手势(TapGestureRecognizer)
  2. 轻扫手势 SwipeGestureRecognizer)
  3. 长按手势(LongPressGestureRecognizer)
  4. 拖动手势(PanGestureRecognizer)
  5. 捏合手势(PinchGestureRecognizer)
  6. 旋转手势(RotationGestureRecognizer)

1. 轻击手势(TapGestureRecognizer

1) 初始化轻击手势识别器对象

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];

 2) 设置手势识别器对象的具体属性
// 连续敲击2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起敲击
tap.numberOfTouchesRequired = 2;
//添加手势识别器到对应的view上
[self.iconView addGestureRecognizer:tap];

3) 监听手势的触发
[tap addTarget:self action:@selector(tapIconView:)];
4) 代理
 tap.delegate = self;
//轻击手势触发方法
-(void) tapIconView:(UITapGestureRecognizer *)sender
{

    NSLog(@"我用两只手指敲击屏幕成功了!");
}
#pragma mark - 代理方法

/**

*  当点击view的时候,会先调用这个方法

 */

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{
    CGPoint pos = [touch locationInView:touch.view];

    if (pos.x <= self.iconView.frame.size.width * 0.5) {

        return YES;

    }
    return NO;

}

2. 轻扫手势 SwipeGestureRecognizer

  1. 初始化轻扫手势识别器对象
    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)
    
    {
    
    //向右轻扫
    
    }
    
    } 

3. 长按手势(LongPressGestureRecognizer

初始化轻扫手势识别器对象

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

//设置长按时间

longPressGesture.minimumPressDuration = 0.5;

[self.view addGestureRecognizer:longPressGesture];

//长按手势触发方法

-(void)longPressGesture:(id)sender

{

 UILongPressGestureRecognizer *longPress = sender;

 if (longPress.state == UIGestureRecognizerStateBegan)

 {

 //your code

 }

}

说明:长按手势的常用状态如下

开始:UIGestureRecognizerStateBegan

改变:UIGestureRecognizerStateChanged

结束:UIGestureRecognizerStateEnded

取消:UIGestureRecognizerStateCancelled

失败:UIGestureRecognizerStateFailed

4. 拖动手势(PanGestureRecognizer)

初始化轻扫手势识别器对象

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

[self.view addGestureRecognizer:panGesture];

//拖动手势触发方法

-(void) panGesture:(id)sender

{
    UIPanGestureRecognizer *panGesture = sender;

    CGPoint movePoint = [panGesture translationInView:self.view];

    //your code

}

5. 捏合手势(PinchGestureRecognizer)

初始化轻扫手势识别器对象

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

[self.view addGestureRecognizer:panGesture];
//拖动手势触发方法

-(void) panGesture:(id)sender

{

    UIPanGestureRecognizer *panGesture = sender;

    CGPoint movePoint = [panGesture translationInView:self.view];

    //your code
}

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;//取消形变

}];

}

}


以上是关于iOS学习之手势的主要内容,如果未能解决你的问题,请参考以下文章

Android使用学习之画图(Canvas,Paint)与手势感应及其应用(乒乓球小游戏)

IO流学习之字节流

GoLang学习之变量定义和初始化

iOS学习之HelloWorld工程

原iOS学习之Masonry第三方约束

iOS学习之C语言Day01