ios手势识别(敲击,捏合,拖拽,轻扫,旋转,长按)
2015-12-16 16:20
429 查看
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
UITapGrestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecongnizer(旋转)
UILongPressGestureRecognizer(长按)
#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
#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.创建一个 "捏合手势" 对象
UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinView:)];
// 属性
// 2.把手势添加到 view 上
[self.imageView addGestureRecognizer:pinchGest];
}
- (void)pinView:(UIPinchGestureRecognizer *)pinGest{
// 缩放的比例是一个 "累加" 的过程
NSLog(@"%s 缩放的比例 %f",__func__, pinGest.scale);
// 设置图片的缩放
// 10x10 / 1.0
// 10*2.01 x 10*2.01 /1.01 + 1.0 = 2.01
// 10*2.01 * 3.03 x 10*2.01*3.03 / 1.03 + 2.01 = 3.03
#warning 放大图片后, 再次缩放的时候,马上回到原先的大小
//self.imageView.transform = CGAffineTransformMakeScale(pinGest.scale, pinGest.scale);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinGest.scale, pinGest.scale);
// 让比例还原,不要累加
// 解决办法,重新设置scale
pinGest.scale = 1;
}
@end
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//给图片添加 "拖拽手势"
// 1.创建一个 "拖拽手势" 对象
UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
// 添加手势
[self.imageView addGestureRecognizer:panGest];
}
-(void)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];
}
UITapGrestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecongnizer(旋转)
UILongPressGestureRecognizer(长按)
#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
#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.创建一个 "长按手势" 对象 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
#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.创建一个 "轻扫手势" 对象 UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)]; // 属性 //UISwipeGestureRecognizerDirectionRight 向右轻扫 //UISwipeGestureRecognizerDirectionLeft 向左轻扫 //UISwipeGestureRecognizerDirectionUp 向上 //UISwipeGestureRecognizerDirectionDown 向下 swipeGest.direction = UISwipeGestureRecognizerDirectionUp; // 2.把手势添加到 view 上 [self.imageView addGestureRecognizer:swipeGest]; } - (void)swipeView:(UISwipeGestureRecognizer *)swipeGest{ // 怎么判断 "长按" 开始和结束 NSLog(@"%s 手势状态 %ld",__func__, swipeGest.state); } @end
#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.创建一个 "捏合手势" 对象
UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinView:)];
// 属性
// 2.把手势添加到 view 上
[self.imageView addGestureRecognizer:pinchGest];
}
- (void)pinView:(UIPinchGestureRecognizer *)pinGest{
// 缩放的比例是一个 "累加" 的过程
NSLog(@"%s 缩放的比例 %f",__func__, pinGest.scale);
// 设置图片的缩放
// 10x10 / 1.0
// 10*2.01 x 10*2.01 /1.01 + 1.0 = 2.01
// 10*2.01 * 3.03 x 10*2.01*3.03 / 1.03 + 2.01 = 3.03
#warning 放大图片后, 再次缩放的时候,马上回到原先的大小
//self.imageView.transform = CGAffineTransformMakeScale(pinGest.scale, pinGest.scale);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinGest.scale, pinGest.scale);
// 让比例还原,不要累加
// 解决办法,重新设置scale
pinGest.scale = 1;
}
@end
#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:)]; // 属性 // 2.把手势添加到 view 上 [self.imageView addGestureRecognizer:rotationGest]; } -(void)rotationView:(UIRotationGestureRecognizer *)rotationGest{ //旋转角度 //旋转的角度也一个累加的过程 NSLog(@"旋转角度 %f",rotationGest.rotation); // 设置图片的旋转 self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGest.rotation); // 清除 "旋转角度" 的累 rotationGest.rotation = 0; } @end
//旋转捏合手势 #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; } @end
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//给图片添加 "拖拽手势"
// 1.创建一个 "拖拽手势" 对象
UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
// 添加手势
[self.imageView addGestureRecognizer:panGest];
}
-(void)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];
}
相关文章推荐
- 官网 webrtc_IOS 源码下载和编译
- 常用iOS第三方库以及XCode插件介绍
- iOS开发-静态库.a的制作
- iOS9 3DTouch(ShortcutItem、Peek/Pop)等简单使用
- iOS中使用autolayout写轮播图片的问题
- html输入框在IOS下只能输入数字
- iOS 极光推送问题
- iOS 语言本地化(中文显示)
- iOS Xcode修改工程名字
- IOS StoryBoard下的视图跳转
- IOS 引导页/欢迎页面 仅一句代码调用
- iOS copy, mutableCopy, strong
- iOS小demo之image的大小和压缩
- iOS17 加密
- iOS实现麦克风捕获和AAC编码
- iOS WebP转换工具
- 深入剖析Auto Layout,分析iOS各版本新增特性
- iOS中Cookie介绍
- iOS Block简介
- 【iOS】网页中调用JS与JS注入