一:手势的类型
- 拍击UITapGestureRecognizer (任意次数的拍击)
- 长按UILongPressGestureRecognizer
- 向里或向外捏UIPinchGestureRecognizer (用于缩放)
- 旋转UIRotationGestureRecognizer(手指朝相反方向移动)
- 划动UISwipeGestureRecognizer (以任意方向)
- 摇动或者拖拽UIPanGestureRecognizer
// 手势识别是具有互斥的原则的,比如单击和双击,如果它识别出一种手势,其后的手势将不被识别
// 如果手势加在uiimageview上 记住 设置userInteractionEnabled = yes;
// 添加单击的手势
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]init];
tapGestureRecognizer.numberOfTapsRequired = 1; // 设置单击几次才触发方法
[tapGestureRecognizer addTarget:self action:@selector(tapGestureAction:)]; // 添加点击手势的方法
[self.view addGestureRecognizer:tapGestureRecognizer]; // 添加到当前的View上
[tapGestureRecognizer release], tapGestureRecognizer = nil; // 释放内存
//手势传值
img.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushWeiboInfo:)];
[img addGestureRecognizer:singleTap];
[singleTap setValue:la forKey:@"la"];
[cell.contentView addSubview:img];
}
}
-(void)pushWeiboInfo:(id)sender
{
UITapGestureRecognizer *tap = (UITapGestureRecognizer*)sender;
loadData *la = [tap valueForKey:@"la"];
}
// 添加长按的手势 注意:会调用两次方法,开始长按调用一次 松开后再调用一次 当长按并且滑动的时候,会多次调用长按的方法
UILongPressGestureRecognizer *pressLongGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
[pressLongGestureRecognizer addTarget:selfaction:@selector(pressLongGestureAction:)]; // 给长按手势添加方法
pressLongGestureRecognizer.minimumPressDuration =4.0;//长按时间限制
[self.view addGestureRecognizer:pressLongGestureRecognizer]; // 添加到当前的View上
[pressLongGestureRecognizer release], pressLongGestureRecognizer = nil; // 释放内存
// 添加捏合的手势 注意:捏合手势不是捏合一次调用一次方法,而是在捏合的过程中不停的调用方法
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizeralloc] init];
[pinchGestureRecognizer addTarget:self action:@selector(pinchGestureAction:)]; //添加捏合手势的方法
[self.view addGestureRecognizer:pinchGestureRecognizer]; // 添加到当前的View上
[pinchGestureRecognizer release], pinchGestureRecognizer = nil; // 释放内存
// 添加旋转的手势 注意:旋转手势是两指同时进行旋转
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] init];
[rotationGestureRecognizer addTarget:selfaction:@selector(rotationGestureAction:)]; // 给旋转手势添加方法
[self.view addGestureRecognizer:rotationGestureRecognizer]; // 添加到当前的View上
[rotationGestureRecognizer release], rotationGestureRecognizer = nil;
// 添加滑动的手势 注意: 快速移动,是用于监测滑动的方向的
UISwipeGestureRecognizer *swipGestureRecognizer = [[UISwipeGestureRecognizeralloc] init];
swipGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp; // 添加手势的方法
// 以下是设置滑动的方向
// typedef NS_OPTIONS(NSUInteger,UISwipeGestureRecognizerDirection) {
// UISwipeGestureRecognizerDirectionRight= 1 << 0, // 从左向右滑动
// UISwipeGestureRecognizerDirectionLeft =1 << 1, // 从右向左滑动
// UISwipeGestureRecognizerDirectionUp =1 << 2, // 从下向上滑动
// UISwipeGestureRecognizerDirectionDown =1 << 3 // 从上向下滑动
// };
[swipGestureRecognizer addTarget:self action:@selector(swipGestureAction:)]; // 给滑动手势添加方法
[self.view addGestureRecognizer:swipGestureRecognizer]; // 添加到当前的View上
[swipGestureRecognizer release], swipGestureRecognizer = nil; // 释放内存
// 添加拖移手势 注意:慢速移动,是用于监测偏移的量的
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]init];
[panGestureRecognizer addTarget:self action:@selector(panGestureAction:)]; // 添加托移收拾的方法
[self.view addGestureRecognizer:panGestureRecognizer]; // 添加到当前的View
[panGestureRecognizer release], panGestureRecognizer = nil; // 释放内存
#pragmamark - 实现单击手势的方法
- (void)tapGestureAction:(UITapGestureRecognizer *) sender {
NSLog(@"您 轻拍 了屏幕");
}
#pragmamark - 实现长按手势的方法
- (void)pressLongGestureAction:(UILongPressGestureRecognizer *) sender {
NSLog(@"您 长按 了屏幕");
// 为了让代码执行一次
if ([senderstate] == UIGestureRecognizerStateBegan) {
NSLog(@"长按事件");
}
}
#pragmamark - 实现了捏合手势的方法
- (void)pinchGestureAction:(UIPinchGestureRecognizer *) sender {
NSLog(@"您 捏合 了屏幕");
}
#pragmamark - 实现旋转手势的方法
- (void)rotationGestureAction:(UIRotationGestureRecognizer *) sender {
NSLog(@"您使用了 旋转 手势");
}
#pragmamark - 实现滑动手势的方法
- (void)swipGestureAction:(UISwipeGestureRecognizer *) sender {
NSLog(@"您 滑动 了屏幕");
}
#pragmamark - 实现了托移手势的方法
- (void)panGestureAction:(UIPanGestureRecognizer *) sender {
NSLog(@"您 托移 了。。。。");
}