IOS | MAC

手势的使用(UIGestureRecognizer)

2016-09-09  本文已影响320人  CoderZNB

注意点 : 手势的优先级比较高,如果给tableView添加手势,那么会造成tableView无法滚动,是不是很慌,不用怕,只需要给手势设置代理并且让代理实现代理方法就行

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

一.轻扫手势

typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
};


####手势的创建

```objc
// 默认方向是往右
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
   swipe.direction = UISwipeGestureRecognizerDirectionUp;

   swipe.delegate = self;

   [self.tableView addGestureRecognizer:swipe];

   UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

   swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
   swipeDown.delegate = self;
   [self.tableView addGestureRecognizer:swipeDown];
// 是否允许支持多个手势,默认是不支持:NO
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"向下滚动");
        [UIView animateWithDuration:0.25 animations:^{
            self.navigationController.navigationBar.znb_y = 20;
        }];
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"上滚");

        [UIView animateWithDuration:0.25 animations:^{

            self.navigationController.navigationBar.znb_y = - 64;
        }];
    }
}

二.长按手势

注意点:长按手势是有状态的 默认会触发两次

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible,
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};

手势的创建

#pragma mark - 添加长按手势

- (void)setupLongPress {

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

    // 设置代理,实现代理方法,让tableView可以就收手势实现手势的方法
    longPress.delegate = self;

    [self.tableView addGestureRecognizer:longPress];
}
// 实现长按手势的方法
- (void)longPress:(UILongPressGestureRecognizer *)longPress {

    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"UIGestureRecognizerStateBegan");
    }else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"UIGestureRecognizerStateEnded");

//        self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;
        [UIView animateWithDuration:0.25 animations:^{

            self.navigationController.navigationBar.znb_y = self.navigationController.navigationBar.znb_y == 0 ? - 64 : 20;
        }];
    }
}```

##三.旋转手势
>注意点:旋转手势中,旋转角度都是相对于最开始的位置的,结合例子,加深理解

####创建手势 and 实现手势方法
```objc
#pragma mark - 旋转手势
- (void)setUpRotation
{
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    [self.imageView addGestureRecognizer:rotation];
}

// 默认传递的旋转的角度都是相对于最开始的位置
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
    // 相对于上次的角度 CGAffineTransformRotate
    // 相对于最开始的位置的旋转角度 CGAffineTransformMakeRotation
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);

    // 复位
    rotation.rotation = 0;

    // 获取手势旋转的角度
    NSLog(@"%f",rotation.rotation);
}

四.捏合手势

创建手势 and 实现手势方法

#pragma mark - 捏合
- (void)setUpPinch
{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinch.delegate = self;
    [self.imageView addGestureRecognizer:pinch];
}

- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);

    // 复位

    pinch.scale = 1;
}

五.拖拽手势

创建手势 and 实现手势方法


#pragma mark - 拖拽
- (void)setUpPan
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];


    [self.imageView addGestureRecognizer:pan];
}

- (void)pan:(UIPanGestureRecognizer *)pan
{
    // 获取手势的触摸点
   // CGPoint curP = [pan locationInView:self.imageView];

    // 移动视图
    // 获取手势的移动,也是相对于最开始的位置
    CGPoint transP = [pan translationInView:self.imageView];

    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);

    // 复位
    [pan setTranslation:CGPointZero inView:self.imageView];

  //  NSLog(@"%@",NSStringFromCGPoint(curP));
}

上一篇 下一篇

猜你喜欢

热点阅读