//1.創(chuàng)建點按手勢
- (void)tapGes {
//1.創(chuàng)建點按手勢
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
tapGes.delegate = self;
//2.添加手勢
[self.imageV addGestureRecognizer:tapGes];
}
//是否允許接收手指
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//獲取當(dāng)前的點.
CGPoint curP = [touch locationInView:self.imageV];
//判斷點在圖片的左邊還是右邊;
if (curP.x > self.imageV.bounds.size.width * 0.5) {
//如果在右邊,返回YES;
return YES;
}else {
//否則返回NO;
return NO;
}
}
長按手勢有3種狀態(tài)調(diào)用時機(jī):1炫乓,第一次響應(yīng)的時候夜赵,2.手指不松開移動的時候设褐,3.手指松開的時候
//1.創(chuàng)建長按
- (void)longPressGes {
//1.創(chuàng)建長按
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGes:)];
//2.添加手勢
[self.imageV addGestureRecognizer:longP];
}
//長按手勢分狀態(tài),長按移動時,會持續(xù)調(diào)用
- (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.創(chuàng)建輕掃
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGes:)];
//默認(rèn)是向右輕掃
//設(shè)置輕掃的方向
//一個輕掃手勢只能對應(yīng)一個方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
//2.添加手勢
[self.imageV addGestureRecognizer:swipe];
//1.創(chuàng)建輕掃
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGes:)];
//默認(rèn)是向右輕掃
//設(shè)置輕掃的方向
//一個輕掃手勢只能對應(yīng)一個方向
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");
}
}
通過拖轉(zhuǎn)手勢獲取的偏移量是相對最原始的點讹俊,也就是最開始拖動的點(松開手指第二次去拖轉(zhuǎn)盾戴,同樣相對的是第一次拖動的那個最原始的點,所以第二次拖動的時候寄锐,它回自動回去)
//1.創(chuàng)建平移
- (void)panGes {
//添加手勢
//1.創(chuàng)建平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
//2.添加手勢
[self.imageV addGestureRecognizer:pan];
}
//當(dāng)拖動時持續(xù)調(diào)用
- (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操作(不讓偏移量進(jìn)行累加,獲取的是相對于上一次的值,每一次走的值.)
[pan setTranslation:CGPointMake(0, 0) inView:self.imageV];
}
- (void)rotationGes {
//1.創(chuàng)建旋轉(zhuǎn)手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
//2.添加手勢
[self.imageV addGestureRecognizer:rotation];
}
- (void)rotation:(UIRotationGestureRecognizer *)rotationGes {
//獲取旋轉(zhuǎn)角度(已經(jīng)是弧度)
//相對于最原始的弧度
CGFloat rotatinon = rotationGes.rotation;
self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotatinon);
//清0
[rotationGes setRotation:0];
}
//1.創(chuàng)建捏合手勢
- (void)pinchGes {
//添加手勢
//1.創(chuàng)建捏合手勢
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];
}
//一個手勢默認(rèn)是不能夠同時支持多個手勢
//可以通過代理進(jìn)行設(shè)置.
//是否允許同時支持多個手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}