#import "ViewController.h"
@interface ViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageV.userInteractionEnabled = YES;
// 1.創(chuàng)建手勢(shì)
// 輕掃手勢(shì)
//一個(gè)手勢(shì)對(duì)應(yīng)一個(gè)方向
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 默認(rèn)軌掃的方向向右輕掃
swipe.direction = UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight;
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//默認(rèn)軌掃的方向向右輕掃
swipe2.direction = UISwipeGestureRecognizerDirectionUp |UISwipeGestureRecognizerDirectionDown ;
//2.添加手勢(shì)
[self.imageV addGestureRecognizer:swipe];
[self.imageV addGestureRecognizer:swipe2];
// [self longPges];
// [self tapGes];
}
// 輕掃手勢(shì)發(fā)生時(shí)調(diào)用
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
// NSLog(@"loveu");
}
//長(zhǎng)按手勢(shì)
- (void)longPges {
//1.創(chuàng)建手勢(shì)
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longP:)];
//2.添加手勢(shì)
[self.imageV addGestureRecognizer:longP];
}
//當(dāng)長(zhǎng)按時(shí)調(diào)用,長(zhǎng)按方法,如果 移動(dòng)時(shí)會(huì)持續(xù)調(diào)用
- (void)longP: (UILongPressGestureRecognizer *)longP {
if (longP.state == UIGestureRecognizerStateBegan) {
NSLog(@"開(kāi)始長(zhǎng)按");
} else if (longP.state == UIGestureRecognizerStateChanged) {
NSLog(@"長(zhǎng)按時(shí)移動(dòng)");
} else if (longP.state == UIGestureRecognizerStateEnded) {
NSLog(@"手指離開(kāi)");
}
}
// 點(diǎn)按手勢(shì)
- (void)tapGes {
//1.創(chuàng)建手勢(shì)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
tap.delegate = self;
//2.添加手勢(shì)
[self.imageV addGestureRecognizer:tap];
}
// 當(dāng)點(diǎn)擊ImageV時(shí)調(diào)用
- (void)tap {
NSLog(@"%s",__func__);
}
// 是否允許接收手指.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//獲取當(dāng)前手指的點(diǎn)
CGPoint curP = [touch locationInView:self.imageV];
if (curP.x > self.imageV.bounds.size.width * 0.5) {
//在右側(cè)
return YES;
}else {
//在左側(cè)
return NO;
}
}
@end
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageV.userInteractionEnabled = YES;
// [self pincGes];
// [self rotationGes];
[self panGes];
}
// 是否允許支持多個(gè)手勢(shì)
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
// 捏合手勢(shì)
- (void)pincGes {
// 1.創(chuàng)建手勢(shì)
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
// 2.設(shè)置代理
pinch.delegate = self;
// 3.添加手勢(shì)
[self.imageV addGestureRecognizer:pinch];
}
// 當(dāng)縮放時(shí)(捏合)會(huì)調(diào)用
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pinch.scale, pinch.scale);
//復(fù)位
[pinch setScale:1];
}
//旋轉(zhuǎn)手勢(shì)
- (void)rotationGes {
//1.創(chuàng)建手勢(shì)
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
//2.添加手勢(shì)
[self.imageV addGestureRecognizer:rotation];
}
// 當(dāng)旋轉(zhuǎn)時(shí)調(diào)用
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotation.rotation);
//復(fù)位
[rotation setRotation:0];
}
// 拖動(dòng)手勢(shì)
- (void)panGes {
//1.創(chuàng)建手勢(shì)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
//2.添加手勢(shì)
[self.imageV addGestureRecognizer:pan];
}
// 當(dāng)拖動(dòng)時(shí)調(diào)用
- (void)pan:(UIPanGestureRecognizer *)pan {
//NSLog(@"%s",__func__);
//獲取偏移量(相對(duì)于最原始的值)
CGPoint transP = [pan translationInView:pan.view];
NSLog(@"===%@", [pan.view class]);
NSLog(@"transP=%@",NSStringFromCGPoint(transP));
self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);
// 復(fù)位(相對(duì)于上一次的操作)
[pan setTranslation:CGPointZero inView:pan.view];
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。