Main.storyboard
1.敲击手势
ViewController.m
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1.创建一个“敲击手势”对象
UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] init];
//2.创建“敲击手势”对象的属性
//点击的次数
tapGest.numberOfTapsRequired = 1;
//手指的数量
// tapGest.numberOfTouchesRequired = 2;
//设置代理
tapGest.delegate = self;
//3.把手势添加到view上
[self.imageView addGestureRecognizer:tapGest];
//4.设置手势的监听方法
[tapGest addTarget:self action:@selector(tapView:)];
}
-(void)tapView:(UITapGestureRecognizer *)tapGest
{
NSLog(@"%s", __func__);
}
#pragma mark 手势的代理
//告诉view能否接收“触摸事件”
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//左边可以 点击/右边不可以
CGPoint location = [touch locationInView:touch.view];
if (location.x <= touch.view.bounds.size.width * 0.5) {
return YES;
}
return NO;
}
@end
2.长按手势
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//给图片添加 “长按手势” 对象
//1.创建一个"长按手势"对象
UILongPressGestureRecognizer *longGest = [[UILongPressGestureRecognizer alloc] init];
//属性
//长按的时间
longGest.minimumPressDuration = 3;
//长按是,距离“触摸点”可移动的距离
longGest.allowableMovement = 30;
//2.把手势添加到view上
[self.imageView addGestureRecognizer:longGest];
//3.设置手势的监听方法
[longGest addTarget:self action:@selector(longPressView:)];
}
-(void)longPressView:(UILongPressGestureRecognizer *)longPressGest{
//怎么判断“长按” 开始和结束
NSLog(@"%s 手势状态 %ld", __func__, longPressGest.state);
if (longPressGest.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按手势开始");
}else{
NSLog(@"长按手势结束");
}
}
@end
3.轻扫手势
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//给图片添加 “轻扫手势” 对象
//1.创建一个"轻扫手势"对象
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc] init];
//属性
swipeGest.direction = UISwipeGestureRecognizerDirectionRight;//默认方向
// UISwipeGestureRecognizerDirectionRight = 1 << 0,向右轻扫
// UISwipeGestureRecognizerDirectionLeft = 1 << 1,向左轻扫
// UISwipeGestureRecognizerDirectionUp = 1 << 2,向上轻扫
// UISwipeGestureRecognizerDirectionDown = 1 << 3,向下
//2.把手势添加到view上
[self.imageView addGestureRecognizer:swipeGest];
//3.设置手势的监听方法
[swipeGest addTarget:self action:@selector(swipeGestView:)];
}
-(void)swipeGestView:(UISwipeGestureRecognizer *)swipeGest{
//“轻扫” 的状态不需要判断,因为只有结束状态。 只有“长按”的状态去判断。
NSLog(@"%s 手势状态 %ld", __func__, swipeGest.state);
// if (swipeGest.state == UIGestureRecognizerStateBegan) {
// NSLog(@"轻扫手势开始");
// }else{
// NSLog(@"轻扫手势结束");
// }
}
4.捏合手势
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/**
* 默认情况下,控制只能 监听到一种手势
* 如果要监听到多个手势,设置一个代理的方法,告知它允许“多个手势”并存
*/
//给图片添加 “捏合手势” 对象
//1.创建一个"捏合手势"对象
UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] init];
//属性
//2.把手势添加到view上
[self.imageView addGestureRecognizer:pinchGest];
//3.设置手势的监听方法
[pinchGest addTarget:self action:@selector(pinchGestView:)];
}
-(void)pinchGestView:(UIPinchGestureRecognizer *)pinchGest{
//缩放的比例是一个“累加”的过程
NSLog(@"%s 缩放的比例 %f", __func__,pinchGest.scale);
//设置图片的缩放
//10*10 / 1.0
//10*2.01*10*2.01/1.01+1.0 = 2.01
//10*2.01*3.03 *10*2.01*3.03 /1.02 +2.01 = 3.03
#warning 放大图片后,再次缩放的时候,马上回到原先的大小
// self.imageView.transform = CGAffineTransformMakeScale(pinchGest.scale, pinchGest.scale);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinchGest.scale, pinchGest.scale);
//让比例还原,不要累加
//解决办法,重新设置scale 图片就不会消失
pinchGest.scale = 1;
}
5.旋转手势
ViewController.m
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//给图片添加“旋转手势”对象
//1.创建一个“旋转手势”对象
UIRotationGestureRecognizer *rotationGest = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
//属性
//设置代理
rotationGest.delegate = self;
//2.把手势添加到view上
[self.imageView addGestureRecognizer:rotationGest];
//给图片添加“捏合手势”
UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[self.imageView addGestureRecognizer:pinchGest];
}
-(void)rotationView:(UIRotationGestureRecognizer *)rotationGest{
//旋转角度
//旋转的角度也是一个累加的过程
NSLog(@"旋转角度%f", rotationGest.rotation);
//设置图片的旋转
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGest.rotation);
// //清除“旋转角度”的累加
rotationGest.rotation = 0;
}
-(void)pinchView:(UIPinchGestureRecognizer *)pinchGest{
//设置图片
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinchGest.scale, pinchGest.scale);
//还原
pinchGest.scale = 1;
}
#pragma mark 手势代理
//Simultaneous 同时发生
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
6.拖曳手势
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//给图片添加“拖拽手势”
//1.创建一个"拖拽手势"对象
// UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
// //添加手势
// [self.imageView addGestureRecognizer:panGest];
}
-(IBAction)panView:(UIPanGestureRecognizer *)panGest{
//方法
//panGest.view触摸的view
//拖拽的距离 累加的过程 获取的是距离
CGPoint trans =[panGest translationInView:panGest.view];
NSLog(@"%@", NSStringFromCGPoint(trans));
//设置图片移动
CGPoint center = self.imageView.center;
center.x += trans.x;
center.y += trans.y;
self.imageView.center = center;
//清除添加
[panGest setTranslation:CGPointZero inView:panGest.view];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
上面几种方法都是用代码写,现在用Main.storyboard
就是在图片上添加手势,前提是,图片是可交互的