iOS UIGestureRecognizer(手勢操作)

//聯系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄

#import"ViewController.h"

@interfaceViewController ()

/**圖片*/

@property(nonatomic,weak)IBOutletUIImageView *imageView;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.imageView.userInteractionEnabled =YES;

[selfsetUpTap];

[selfsetUpLongPress];

[selfsetUpSwipe];

[selfsetUpRotation];

[selfsetPinch];

[selfsetPan];

}

/*------------長按手勢----------------*/

- (void)setUpTap {

//創建點擊事件

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

tap.delegate =self;

[self.imageView addGestureRecognizer:tap];

}

//點擊事件

- (void)tap {

NSLog(@"%s",__func__);

}

/*------------長按手勢----------------*/

- (void)setUpLongPress {

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

longPress.delegate =self;

[self.imageView addGestureRecognizer:longPress];

}

//長按手勢事件

- (void)longPress:(UILongPressGestureRecognizer *)sender {

//注意:長安手勢可以觸發兩次事件,我們需要判斷手勢狀態,如下

if(sender.state == UIGestureRecognizerStateEnded) {

NSLog(@"%s",__func__);

}

}

/*------------輕掃手勢----------------*/

- (void)setUpSwipe {

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

swipe.delegate =self;

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

//默認清掃手勢的方向是向右

swipe.direction = UISwipeGestureRecognizerDirectionRight;

[self.imageView addGestureRecognizer:swipe];

}

//輕掃手勢事件

- (void)swipe:(UISwipeGestureRecognizer *)sender {

NSLog(@"%s",__func__);

}

/*------------旋轉手勢----------------*/

- (void)setUpRotation {

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

rotation.delegate =self;

[self.imageView addGestureRecognizer:rotation];

}

//旋轉手勢事件

- (void)rotation:(UIRotationGestureRecognizer *)sender {

//注意:手勢傳遞的旋轉角度都是相對于最開始的位置

CGFloat rotation = sender.rotation;

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

//復位

sender.rotation =0;

NSLog(@"%s",__func__);

}

/*------------捏合手勢----------------*/

- (void)setPinch {

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:selfaction:@selector(pinch:)];

pinch.delegate =self;

[self.imageView addGestureRecognizer:pinch];

}

//捏合手勢事件

- (void)pinch:(UIPinchGestureRecognizer *)sender {

CGFloat scale = sender.scale;

self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale);

//復位

sender.scale =1.0;

NSLog(@"%s",__func__);

}

/*------------拖拽手勢----------------*/

- (void)setPan {

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:selfaction:@selector(pan:)];

pan.delegate =self;

[self.imageView addGestureRecognizer:pan];

}

//拖拽手勢事件

- (void)pan:(UIPanGestureRecognizer *)sender {

//獲取手勢的移動,也是相對于最開始的位置

CGPoint transP = [sender translationInView:self.imageView];

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

//復位

[sender setTranslation:CGPointZero inView:self.imageView];

NSLog(@"%s",__func__);

}

#pragma mark -

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

returnYES;

}

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

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

returnYES;

}

//是否可以接收手指的觸摸點(touch)

//實例:該方法可以控制讓一個視圖,左邊能點擊,右邊不能點擊

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

CGPoint currentPoint = [touch locationInView:self.imageView];

if(currentPoint.x >self.imageView.bounds.size.width *0.5) {

returnYES;

}

returnNO;

}

@end

謝謝!!!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容