#import "ViewController.h"
@interface ViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageV.userInteractionEnabled = YES;
// 1.创建手势
// 轻扫手势
//一个手势对应一个方向
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 默认轨扫的方向向右轻扫
swipe.direction = UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight;
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//默认轨扫的方向向右轻扫
swipe2.direction = UISwipeGestureRecognizerDirectionUp |UISwipeGestureRecognizerDirectionDown ;
//2.添加手势
[self.imageV addGestureRecognizer:swipe];
[self.imageV addGestureRecognizer:swipe2];
// [self longPges];
// [self tapGes];
}
// 轻扫手势发生时调用
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
// NSLog(@"loveu");
}
//长按手势
- (void)longPges {
//1.创建手势
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longP:)];
//2.添加手势
[self.imageV addGestureRecognizer:longP];
}
//当长按时调用,长按方法,如果 移动时会持续调用
- (void)longP: (UILongPressGestureRecognizer *)longP {
if (longP.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始长按");
} else if (longP.state == UIGestureRecognizerStateChanged) {
NSLog(@"长按时移动");
} else if (longP.state == UIGestureRecognizerStateEnded) {
NSLog(@"手指离开");
}
}
// 点按手势
- (void)tapGes {
//1.创建手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
tap.delegate = self;
//2.添加手势
[self.imageV addGestureRecognizer:tap];
}
// 当点击ImageV时调用
- (void)tap {
NSLog(@"%s",__func__);
}
// 是否允许接收手指.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//获取当前手指的点
CGPoint curP = [touch locationInView:self.imageV];
if (curP.x > self.imageV.bounds.size.width * 0.5) {
//在右侧
return YES;
}else {
//在左侧
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];
}
// 是否允许支持多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
// 捏合手势
- (void)pincGes {
// 1.创建手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
// 2.设置代理
pinch.delegate = self;
// 3.添加手势
[self.imageV addGestureRecognizer:pinch];
}
// 当缩放时(捏合)会调用
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pinch.scale, pinch.scale);
//复位
[pinch setScale:1];
}
//旋转手势
- (void)rotationGes {
//1.创建手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
//2.添加手势
[self.imageV addGestureRecognizer:rotation];
}
// 当旋转时调用
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotation.rotation);
//复位
[rotation setRotation:0];
}
// 拖动手势
- (void)panGes {
//1.创建手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
//2.添加手势
[self.imageV addGestureRecognizer:pan];
}
// 当拖动时调用
- (void)pan:(UIPanGestureRecognizer *)pan {
//NSLog(@"%s",__func__);
//获取偏移量(相对于最原始的值)
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);
// 复位(相对于上一次的操作)
[pan setTranslation:CGPointZero inView:pan.view];
}
@end