#import “ViewController.h”
@interface ViewController ()
// 拖线拖过来的,点下右上角的俩圈 摁ctrl把图片拖根线过来.
@property (weak, nonatomic) IBOutlet UIImageView *imgV;
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];//设置图片框与用户交互
self.imgV.userInteractionEnabled=YES;// 1.======= 点击手势(单点) ===========
[self tapGes];// 2.======= 长按手势(单点) ===========
[self longGes];// 3.======= 拖动手势(单点) ===========
[self swipeGes];/** 怕两指手势冲突注掉了一个.
(将手势初始化封装到方法里)
*//**
注意两点:代表俩手指 摁住option键 点住触摸板滑动,
两个点必须在图片里
*///4.======= 捏合(两点) 放大缩小 ===========
//[self pinchGes];// 5.======= 拖动手势(单点) ===========
[self rotationGes];
}
#pragma mark - 初始化方法
// 1.点击
-(void)tapGes{
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
//给图片框添加点按手势
[self.imgV addGestureRecognizer:tap];
}
// 2.长摁
-(void)longGes{
UILongPressGestureRecognizer *longP=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longP:)];
//给图片框添加手势
[self.imgV addGestureRecognizer:longP];
}
// 3.拖动(扫)
-(void)swipeGes{
// ===== 清扫手势左 ========
UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)]; // 俩个横扫用的一个手势
//设置清扫的方向
swipe.direction=UISwipeGestureRecognizerDirectionLeft;
//给图片框添加手势
[self.imgV addGestureRecognizer:swipe];
// ===== 清扫手势右 ========
UISwipeGestureRecognizer *swipe1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
//设置清扫的方向
swipe1.direction=UISwipeGestureRecognizerDirectionRight;
//给图片框添加手势
[self.imgV addGestureRecognizer:swipe1];
}
// 4.捏合 ( 缩放 )
-(void)pinchGes
{
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGes:)];
//设置代理
pinch.delegate=self;
//给图片框添加手势
[self.imgV addGestureRecognizer:pinch];
}
// 5.旋转
-(void)rotationGes
{
UIRotationGestureRecognizer *rota=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[self.imgV addGestureRecognizer:rota];
}
#pragma mark - 手势触发的方法
// 1.点按
-(void)tap:(UITapGestureRecognizer *)tap
{
NSLog(@“我点”);
}
//2.长按
-(void)longP:(UILongPressGestureRecognizer *)longP
{
//判断手势的状态
if(longP.stateUIGestureRecognizerStateBegan)
{
NSLog(@“开始长按”);
}
else if(longP.stateUIGestureRecognizerStateChanged)
{
NSLog(@“长按时移动”);
}
else if(longP.state==UIGestureRecognizerStateEnded)
{
NSLog(@“手指移开”);
}
}
// 3.拖动(扫)
-(void)swipe:(UISwipeGestureRecognizer *)swipe
{
//判断清扫的方法
if(swipe.directionUISwipeGestureRecognizerDirectionLeft)
{
NSLog(@“向左”);
}
else if(swipe.directionUISwipeGestureRecognizerDirectionRight)
{
NSLog(@“向右”);
}
}
// 4.捏合 (缩放)
-(void)pinchGes:(UIPinchGestureRecognizer )pinchGes
{
//使图片放大缩小
/*
CGAffineTransformScale: 缩放方法
pinchGes 就是我们上边初始化点手势(形参)
scale 触点 后俩参数 是x y出点的改变
*/
self.imgV.transform=CGAffineTransformScale(self.imgV.transform, pinchGes.scale, pinchGes.scale);
//复位
// 缩放比例1 ,恢复原来大小
pinchGes.scale=1;
}
// 5.旋转
-(void)rotation:(UIRotationGestureRecognizer *)rotation
{
// 摁住cmd进入 CGAffineTransformRotate下边这个方法看下,里边有参数
// angle : 角度
// rotation 下边有俩个
// 第一个是手势的名字(形参)
// 第二个 rotation 旋转的弧度
self.imgV.transform=CGAffineTransformRotate(self.imgV.transform, rotation.rotation);
NSLog(@“1”);
//复位
rotation.rotation=0;
}
#pragma mark - 设置多种手势可共用
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
// return YES;
//}
@end