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

谢谢!!!

你可能感兴趣的:(iOS UIGestureRecognizer(手势操作))