iOS中GestureRecognizer的6大手勢與代理方法詳細使用


#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.\

[self setUpPinch];

[self setUpRotation];

[self setUpPan];

}

#pragma mark - 手勢代理方法

// 是否允許開始觸發手勢

//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

//{

//    return NO;

//}

// 是否允許同時支持多個手勢,默認是不支持多個手勢

// 返回yes表示支持多個手勢

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

{

return YES;

}

// 是否允許接收手指的觸摸點

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

//    // 獲取當前的觸摸點

//    CGPoint curP = [touch locationInView:self.imageView];

//

//    if (curP.x < self.imageView.bounds.size.width * 0.5) {

//        return NO;

//    }else{

//        return YES;

//    }

//}

1.#pragma mark - 點按手勢

- (void)setUpTap

{

// 創建點按手勢

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

tap.delegate = self;

[_imageView addGestureRecognizer:tap];

}

- (void)tap:(UITapGestureRecognizer *)tap

{

NSLog(@"%s",__func__);

}

2.#pragma mark - 長按手勢

// 默認會觸發兩次

- (void)setUpLongPress

{

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

[self.imageView addGestureRecognizer:longPress];

}

- (void)longPress:(UILongPressGestureRecognizer *)longPress

{

if (longPress.state == UIGestureRecognizerStateBegan) {

NSLog(@"%s",__func__);

}

}

3.#pragma mark - 清掃

- (void)setUpSwipe

{

// 默認輕掃的方向是往右

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

swipe.direction = UISwipeGestureRecognizerDirectionUp;

[self.imageView addGestureRecognizer:swipe];

// 如果以后想要一個控件支持多個方向的輕掃,必須創建多個輕掃手勢,一個輕掃手勢只支持一個方向

// 默認輕掃的方向是往右

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

swipeDown.direction = UISwipeGestureRecognizerDirectionDown;

[self.imageView addGestureRecognizer:swipeDown];

}

- (void)swipe

{

NSLog(@"%s",__func__);

}

4.#pragma mark - 旋轉手勢

- (void)setUpRotation

{

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];

rotation.delegate = self;

[self.imageView addGestureRecognizer:rotation];

}

// 默認傳遞的旋轉的角度都是相對於最開始的位置

- (void)rotation:(UIRotationGestureRecognizer *)rotation

{

self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);

// 復位

rotation.rotation = 0;

// 獲取手勢旋轉的角度

NSLog(@"%f",rotation.rotation);

}

5.#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;

}

6.#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));

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM