UIGestureRecognizer手势识别
来源:互联网 发布:雪纺衫淘宝好评 编辑:程序博客网 时间:2024/05/17 21:07
UIGestureRecognizer手势识别
UIGestureRecognizer 继承于 NSObject 共有七种手势
即:
1.UIPinchGestureRecognizer (捏合手势)2.UITapGestureRecognizer (轻拍手势)
3.UIRotationGestureRecognizer (旋转手势)
4.UISwipeGestureRecognizer (轻扫手势)
5.UIPanGestureRecognizer (拖拽手势)
6.UIScreenEdgePanGestureRecognizer (屏幕边缘轻扫手势)
7.UILongPressGestureRecognizer (长按手势)
实现手势步骤
1创建手势对象
UIGestureRecognizer 的 七个子类并没有自身的初始化方法, 所以需要使用父类的 初始化方法,如下
- (instancetype)initWithTarget:(id)target action:(SEL)action2将手势加在视图上, 注意视图的用户交互属性是否开启
- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer/* 创建对象 */@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled/* 用户交互属性*/3内存管理
- (void)release4设置相关属性
5手势方法实现
实现创建对象 - (instancetype)initWithTarget:(id)target action:(SEL)action中的 action:(SEL)action例如
1.UIPinchGestureRecognizer (捏合手势)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view./* 创建一个UIImageView对象 */ UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; imageView.frame = CGRectMake(0, 0, 300, 300); imageView.center = self.view.center; imageView.layer.cornerRadius = 50; imageView.layer.backgroundColor = [UIColor blackColor].CGColor; imageView.layer.masksToBounds = YES; [self.view addSubview:imageView]; [imageView release];/* 创建UIPinchGestureRecognizer对象*/ UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */ [imageView addGestureRecognizer:pinch]; imageView.userInteractionEnabled = YES;/* 默认NO */ [pinch release];}/* 实现action:@selector(pinchAction:)*/ - (void)pinchAction:(UIPinchGestureRecognizer *)pinch{ NSLog(@"捏合"); /* 设置imageView的transform属性 *使用CGAffineTransformScale函数改变比例实现缩放效果 */ pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); pinch.scale = 1;}2.UITapGestureRecognizer (轻拍手势)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view./* 创建一个UIImageView对象 */ UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; imageView.frame = CGRectMake(0, 0, 300, 300); imageView.center = self.view.center; imageView.layer.cornerRadius = 50; imageView.layer.backgroundColor = [UIColor blackColor].CGColor; imageView.layer.masksToBounds = YES; [self.view addSubview:imageView]; [imageView release];/* 创建UITapGestureRecognizer手势对象 */ UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];/* 设置相关属性 */ tap.numberOfTapsRequired = 1; tap.numberOfTouchesRequired = 1;/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */ [imageView addGestureRecognizer:tap]; imageView.userInteractionEnabled = YES;/* 默认NO */ [tap release];}/* 实现action:@selector(pinchAction:)*/ - (void)tapAction:(UITapGestureRecognizer *)tap{ NSLog(@"轻拍");}3.UIRotationGestureRecognizer (旋转手势)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view./* 创建一个UIImageView对象 */ UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; imageView.frame = CGRectMake(0, 0, 300, 300); imageView.center = self.view.center; imageView.layer.cornerRadius = 50; imageView.layer.backgroundColor = [UIColor blackColor].CGColor; imageView.layer.masksToBounds = YES; [self.view addSubview:imageView]; [imageView release];/* 创建UIRotationGestureRecognizer手势对象 */ UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */ [imageView addGestureRecognizer:rotation]; imageView.userInteractionEnabled = YES;/* 默认NO */ [rotation release];}/* 实现action:@selector(pinchAction:)*/- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{ NSLog(@"旋转"); NSLog(@"%@", rotation); /* 设置imageView的transform属性 * 使用 CGAffineTransformRotate 函数 */ rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation); rotation.rotation = 0;}4.UISwipeGestureRecognizer (轻扫手势)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view./* 创建UISwipeGestureRecognizer手势对象 */ UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];/* 设置相关属性 */ swipe.direction = UISwipeGestureRecognizerDirectionUp;/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */ [self.view addGestureRecognizer:swipe]; [swipe release];}/* 实现action:@selector(pinchAction:)*/- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{ NSLog(@"轻扫");}5.UIPanGestureRecognizer (拖拽手势)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view./* 创建一个UIImageView对象 */ UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; imageView.frame = CGRectMake(0, 0, 300, 300); imageView.center = self.view.center; imageView.layer.cornerRadius = 50; imageView.layer.backgroundColor = [UIColor blackColor].CGColor; imageView.layer.masksToBounds = YES; [self.view addSubview:imageView]; [imageView release];/* 创建UIPanGestureRecognizer手势对象 */ UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */ [imageView addGestureRecognizer:pan]; imageView.userInteractionEnabled = YES;/* 默认NO */ [pan release];}/* 实现action:@selector(pinchAction:)*/- (void)panAction:(UIPanGestureRecognizer *)pan{ NSLog(@"拖拽"); /* 设置imageView的transform属性 * 使用 CGAffineTransformTranslate 函数 */ CGPoint point = [pan translationInView:pan.view]; pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y); [pan setTranslation:CGPointZero inView:pan.view];}6.UIScreenEdgePanGestureRecognizer (屏幕边缘轻扫手势)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view./* 创建UIScreenEdgePanGestureRecognizer手势对象 */ UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];/* 设置相关属性 */ screenEdge.edges = 2;/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */ [self.view addGestureRecognizer:screenEdge]; [screenEdge release];}/* 实现action:@selector(pinchAction:)*/-(void)screenAction:(UIScreenEdgePanGestureRecognizer *)screen{ NSLog(@"屏幕边缘轻扫");}7.UILongPressGestureRecognizer (长按手势)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view./* 创建一个UIImageView对象 */ UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; imageView.frame = CGRectMake(0, 0, 300, 300); imageView.center = self.view.center; imageView.layer.cornerRadius = 50; imageView.layer.backgroundColor = [UIColor blackColor].CGColor; imageView.layer.masksToBounds = YES; [self.view addSubview:imageView]; [imageView release];/* 创建UILongPressGestureRecognizer手势对象 */ UILongPressGestureRecognizer *long1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */ [imageView addGestureRecognizer:long1]; imageView.userInteractionEnabled = YES;/* 默认NO */ [long1 release];}/* 实现action:@selector(pinchAction:)*/- (void)longAction:(UILongPressGestureRecognizer *)long1{ NSLog(@"长按");}
/* 注: 摘自http://blog.csdn.net/yuling962464/article/details/47320243 */
0 0
- ios 手势识别UIGestureRecognizer
- UIGestureRecognizer手势识别小结
- UIGestureRecognizer(手势识别器)
- UIKit 手势识别 UIGestureRecognizer
- UIGestureRecognizer手势识别
- UIGestureRecognizer手势识别
- UIGestureRecognizer 手势识别器
- 手势识别(UIGestureRecognizer)
- UIGestureRecognizer手势识别器
- UIGestureRecognizer手势识别器
- UIGestureRecognizer手势识别详解
- UIGestureRecognizer-手势识别器
- UIGestureRecognizer 手势识别
- UIGestureRecognizer(手势识别器)
- iOS--手势识别UIGestureRecognizer
- UIGestureRecognizer手势识别详解
- UIGestureRecognizer 手势识别
- UIGestureRecognizer同时识别两个手势
- 什么是工具栏,什么是菜单栏、什么是标题栏,如何隐藏标题栏
- 基于DFS求无向图的割点及桥(割边)算法总结 POJ_1144题解
- Android分享功能的一点总结
- hdu5387(模拟)
- Skia overview
- UIGestureRecognizer手势识别
- android获取/更改gps和WIFI状态
- 数据结构实验之栈二:一般算术表达式转换成后缀式
- HDU 1012 u Calculate e (水题)
- Core Data Programming Guide 之 Core Data Basics 翻译
- 更改数据库字符集
- android textview html font标签不好用
- Merge讲解与实例
- hdoj-5099-Comparison of Android versions