手势
/**
手势识别,是单独添加到某一个视图上的
如果要同时支持多个手势识别,需要设置手势识别的代理!
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
UIPanGestureRecognizer 拖动手势
UIPinchGestureRecognizer 缩放手势
UIRotationGestureRecognizer 旋转手势
都是通过transform设置视图的形变
一定记住设置完transform之后,需要将对应的形变参数复位!!!
因为轻扫手势要求用户比较放松的扫动,因此最好不要将此手势添加到某一个视图上,会局限用户的操作
1> 一般要添加到self.view上
2> 如果要监听多个轻扫方向,需要添加多个轻扫手势
长按手势一定要判断状态,否则方法会在手势开始和结束时分别调用!方法会被调用两次!
*/
#import "XZViewController.h"
@interface XZViewController () <UIGestureRecognizerDelegate]]
>
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation XZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//加载图片视图
[self imageView];
//长按手势
[self addLongPressGesture];
//轻扫手势
[self addSwipeGesture];
//缩放(捏合)手势
[self addPinchGesture];
//旋转手势
[self addRotateGesture];
//点按(单击,点击)
[self addTapGesture];
//拖动手势
[self addPanGesture];
}
//懒加载创建并添加imageView到view 上
- (UIImageView *)imageView
{
if (_imageView == nil) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 200, 200)];
_imageView.image = [UIImage imageNamed:@"头像1"];
_imageView.userInteractionEnabled = YES;
[self.view addSubview:_imageView];
}
return _imageView;
}
#pragma mark - 长按手势
- (void)addLongPressGesture
{
//创建长按手势识别并添加监听事件
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 给图片添加长按手势
[self.imageView addGestureRecognizer:longPress];
}
//识别到长按手势后回调的方法
- (void)longPress:(UILongPressGestureRecognizer *)recognizer
{
// 判断手势的状态,长按手势一定要判断状态,否则方法会在手势开始和结束时分别调用!方法会被调用两次!
if (recognizer.state == UIGestureRecognizerStateBegan) {
//动画 改变图片的透明度
[UIView animateWithDuration:0.5 animations:^{
self.imageView.alpha = 0.5;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
self.imageView.alpha = 1.0;
}];
}];
}
}
#pragma mark - 轻扫手势
- (void)addSwipeGesture
{
// 如果要监听多个轻扫方向,需要添加多个轻扫手势
// 轻扫手势默认支持向右的扫动方向
//创建轻扫手势识别并添加监听事件(默认是向右扫动)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//创建轻扫手势识别并添加监听事件
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//direction 方向 向左轻扫
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
/**
UISwipeGestureRecognizerDirectionRight 向右轻扫(默认)不设轻扫方向 就是向右
UISwipeGestureRecognizerDirectionLeft 向左轻扫
UISwipeGestureRecognizerDirectionUp 向上轻扫
UISwipeGestureRecognizerDirectionDown 向下轻扫
*/
// 因为轻扫手势要求用户比较放松的扫动,因此最好不要将此手势添加到某一个视图上,会局限用户的操作
// 添加手势
[self.view addGestureRecognizer:swipe];
[self.view addGestureRecognizer:swipeLeft];
}
//识别到轻扫手势后回调的方法
- (void)swipe:(UISwipeGestureRecognizer *)recognizer
{
//当前获取中心位置
CGPoint from = self.imageView.center;
//目标
CGPoint to;
//向左轻扫
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
to = CGPointMake(-from.x, from.y);
} else {//向右轻扫
to = CGPointMake(3 * from.x, from.y);
}
//动画移动图片
[UIView animateWithDuration:0.5 animations:^{
self.imageView.center = to;
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
self.imageView.center = from;
}];
}];
}
#pragma mark - 拖动手势
- (void)addPanGesture
{
//创建拖动手势 并添加手势的监听事件
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
//添加手势
[self.imageView addGestureRecognizer:pan];
}
//识别到拖动手势后回调的方法
- (void)pan:(UIPanGestureRecognizer *)recognizer
{
//获取手指按在图片上的位置 以图片左上角为原点
CGPoint translation = [recognizer translationInView:self.imageView];
// 移动图片
recognizer.view.transform = CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y);
//给平移复位 因为他是在原有基础上当前递增平移 如果不复位 或清空他会越变越大
[recognizer setTranslation:CGPointZero inView:self.imageView];
}
#warning mark - 手势识别代理方法
//Simultaneous同时识别两个手势识别
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
#pragma mark - 缩放(捏合)
- (void)addPinchGesture
{
//创建缩放(捏合)手势 并添加手势的监听事件
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
//设置控制器为缩放手势的代理 可以实现同时识别两个手势
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
}
//识别到 缩放(捏合)手势后回调的方法
- (void)pinch:(UIPinchGestureRecognizer *)recognizer
{
//绽放图片
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
//将缩放比例复位
recognizer.scale = 1.0;
}
#pragma mark - 旋转
- (void)addRotateGesture
{
//创建缩放 旋转并添加手势的监听事件
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
//设置控制器为缩放手势的代理 可以实现同时识别两个手势
rotate.delegate = self;
// 添加手势
[self.imageView addGestureRecognizer:rotate];
}
//识别到旋转手势后的回调方法
- (void)rotate:(UIRotationGestureRecognizer *)recognizer
{
//图片旋转
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
//将手势识别的旋转角度复位
recognizer.rotation = 0.0; //非常重要 角度也会叠加
}
#pragma mark - 点按(单击,点击)
- (void)addTapGesture
{
//创建缩放点按(单击,点击)并添加手势的监听事件
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
// 添加手势
[self.imageView addGestureRecognizer:tapGesture];
}
//识别到手势后的回调方法
- (void)tap
{
NSLog(@"点了");
}
@end
浙公网安备 33010602011771号