//1.创建点按手势
- (void)tapGes {
//1.创建点按手势
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
tapGes.delegate = self;
//2.添加手势
[self.imageV addGestureRecognizer:tapGes];
}
//是否允许接收手指
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//获取当前的点.
CGPoint curP = [touch locationInView:self.imageV];
//判断点在图片的左边还是右边;
if (curP.x > self.imageV.bounds.size.width * 0.5) {
//如果在右边,返回YES;
return YES;
}else {
//否则返回NO;
return NO;
}
}
长按手势有3种状态调用时机:1,第一次响应的时候,2.手指不松开移动的时候,3.手指松开的时候
//1.创建长按
- (void)longPressGes {
//1.创建长按
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGes:)];
//2.添加手势
[self.imageV addGestureRecognizer:longP];
}
//长按手势分状态,长按移动时,会持续调用
- (void)longGes:(UILongPressGestureRecognizer *)longP {
if (longP.state == UIGestureRecognizerStateBegan) {
NSLog(@"UIGestureRecognizerStateBegan");
}else if (longP.state == UIGestureRecognizerStateChanged) {
NSLog(@"UIGestureRecognizerStateChanged");
}else if (longP.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.imageV.userInteractionEnabled = YES;
//添加手势
//1.创建轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGes:)];
//默认是向右轻扫
//设置轻扫的方向
//一个轻扫手势只能对应一个方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
//2.添加手势
[self.imageV addGestureRecognizer:swipe];
//1.创建轻扫
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGes:)];
//默认是向右轻扫
//设置轻扫的方向
//一个轻扫手势只能对应一个方向
swipe2.direction = UISwipeGestureRecognizerDirectionRight;
//2.添加手势
[self.imageV addGestureRecognizer:swipe2];
}
- (void)swipeGes:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"UISwipeGestureRecognizerDirectionLeft");
}else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"UISwipeGestureRecognizerDirectionRight");
}
}
通过拖转手势获取的偏移量是相对最原始的点,也就是最开始拖动的点(松开手指第二次去拖转,同样相对的是第一次拖动的那个最原始的点,所以第二次拖动的时候,它回自动回去)
//1.创建平移
- (void)panGes {
//添加手势
//1.创建平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
//2.添加手势
[self.imageV addGestureRecognizer:pan];
}
//当拖动时持续调用
- (void)pan:(UIPanGestureRecognizer *)pan {
//获取偏移量
//获取的偏移量是相对于最原始的点
CGPoint transP = [pan translationInView:self.imageV];
NSLog(@"%@",NSStringFromCGPoint(transP));
// self.imageV.transform = CGAffineTransformMakeTranslation(transP.x, transP.y);
self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);
//清0操作(不让偏移量进行累加,获取的是相对于上一次的值,每一次走的值.)
[pan setTranslation:CGPointMake(0, 0) inView:self.imageV];
}
- (void)rotationGes {
//1.创建旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
//2.添加手势
[self.imageV addGestureRecognizer:rotation];
}
- (void)rotation:(UIRotationGestureRecognizer *)rotationGes {
//获取旋转角度(已经是弧度)
//相对于最原始的弧度
CGFloat rotatinon = rotationGes.rotation;
self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotatinon);
//清0
[rotationGes setRotation:0];
}
//1.创建捏合手势
- (void)pinchGes {
//添加手势
//1.创建捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
//2.添加手势
[self.imageV addGestureRecognizer:pinch];
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
//放大, 缩小
//获取缩放比例(相对于最原始的比例)
CGFloat scale = pinch.scale;
self.imageV.transform = CGAffineTransformScale(self.imageV.transform, scale, scale);
[pinch setScale:1];
}
//一个手势默认是不能够同时支持多个手势
//可以通过代理进行设置.
//是否允许同时支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
网友评论