手势识别器
抽象类:这个类是抽象类 其具体功能 交给子类去实现 (省代码)
⼿势识别器是对触摸事件做了封装,我们⽆需⾃⼰去判断某个⼿势
是否触发,⼿势识别器本⾝起到了识别作⽤,我们把重⼼放在识别之
后要做什么操作上⾯
根视图准备满屏的ImageView
// 创建imageView 添加手势用
UIImageView *imageV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageV.image = [UIImage imageNamed:@"car.jpg"];
[self.view addSubview:imageV];
// 打开imageV的交互 **********************
imageV.userInteractionEnabled = YES;
[imageV release];
轻拍
// 初始化部分
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 添加手势到视图上
[imageV addGestureRecognizer:tap];
// 释放
[tap release];
轻拍的方法实现
// 轻拍的触发方法
- (void)tapAction:(UITapGestureRecognizer *)tap{
NSLog(@"轻拍完成");
// 取出被添加的视图
UIImageView *imageView = (UIImageView *)tap.view;
imageView.image = [UIImage imageNamed:@"car.jpg"];
}
长按
初始化部分
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 设置长按时间
longPress.minimumPressDuration = 2.0;
// 添加到视图上
[self.view addGestureRecognizer:longPress];
[longPress release];
长按的方法实现
// 长按的触发方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{
// 判断手势状态 长按 只需要触发一次
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按Zzz");
}
}
旋转
初始化
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[self.view addGestureRecognizer:rotation];
[rotation release];
旋转方法实现
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
NSLog(@"旋转跳跃");
// 形变属性 transform
// 参数一 要改变形变属性的视图
// 参数二 根据弧度去创建
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
// 每次转的时候需要把旋转的角度重置为0
// 因为要接替上一次的角度 开始旋转
rotation.rotation = 0;
}
捏合触发的方法
初始化
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pinch];
[pinch release];
实现方法
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
NSLog(@"捏合完成");
// 根据缩放的刻度(比例)改变形变属性
// 根据手势捏合的比较 去改变形变属性
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
// 重置比例
pinch.scale = 1;
}
平移
初始化
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageV addGestureRecognizer:pan];
[pan release];
平移方法的实现
- (void)panAction:(UIPanGestureRecognizer *)pan{
// 获取平移的点(相对于要平移的视图)
CGPoint p = [pan translationInView:pan.view];
// 根据这个点改变形变属性
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
// 重置这个点
[pan setTranslation:CGPointMake(0, 0) inView:pan.view];
NSLog(@"平移~~~~~~~~");
}
轻扫(左右扫)
初始化(右扫)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// 设置左右扫
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipe];
[swipe release];
轻扫的方法实现
// 轻扫(实现左扫方法)
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
NSLog(@"扫啊扫");
}
边缘扫
初始化
// 边缘扫
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];
// 设置从哪个边缘扫
screenEdgePan.edges = UIRectEdgeRight;
[imageV addGestureRecognizer:screenEdgePan];
[screenEdgePan release];
边缘扫的方法实现
- (void)screenEdgePanAction
:(UIScreenEdgePanGestureRecognizer *)screenEdgePan{
NSLog(@"边缘扫~~~");
}
总结
添加手势步骤
1. 初始化手势 添加手势出发调用的方法
2. 把手势添加到视图上
3. 释放手势
4. 实现方法