//聯(lián)系人:石虎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 {
//創(chuàng)建點擊事件
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 {
//注意:長安手勢可以觸發(fā)兩次事件娜睛,我們需要判斷手勢狀態(tài)唤崭,如下
if(sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"%s",__func__);
}
}
/*------------輕掃手勢----------------*/
- (void)setUpSwipe {
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipe:)];
swipe.delegate =self;
//注意:如果以后想要一個控件支持多個方向的清掃忙灼,必須創(chuàng)建多個清掃手勢惰蜜,一個清掃手勢只能支持一個方向
//默認清掃手勢的方向是向右
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
謝謝!!!