17.⼿势识别器

⼿势识别器有7个⼦类
分别识别轻拍⼿势、平移⼿势、轻扫⼿势、缩放⼿势、旋转⼿势、
⻓按⼿势以及屏幕边界平移⼿势
⼀旦指定的⼿势被识别,我们可以执⾏我们⾃⼰定义好的操作。

创建ImageView 用来添加手势用
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:imageView];
[imageView release];

打开交互
imageView.userInteractionEnabled = YES;

UIImage *image = [UIImage imageNamed:@”646B48AE-7711-4DDE-BFA3-97A6E51AA4B7”];
imageView.image = image;

手势类 UIGestureRecognizer
这个类是个抽象类 其具体功能 交给子类去实现
添加手势步骤
1.初始化手势 添加手势触发调用的方法
2.把手势添加到视图上
3.释放手势

轻拍

1.初始化
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

2.添加手势到视图上
[imageView addGestureRecognizer:tap];

3.释放
[tap release];

4.实现轻拍方法
-(void)tapAction:(UITapGestureRecognizer *)tap
{
取出被添加的视图
UIImageView imageView = (UIImageView )tap.view;

换图片
imageView.image = [UIImage imageNamed:@”u=194240101,2532182839&fm=21&gp=0.jpg”];

NSLog(@”你轻拍我了”);
}

长按手势

1.初始化
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

2.设置长按时间
longPress.minimumPressDuration = 2.0;

3.添加到视图上
[imageView addGestureRecognizer:longPress];
4.释放
[longPress release];

5.实现长按的方法

-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
判断一下状态 长按 只需要触发一次
if (longPress.state == UIGestureRecognizerStateBegan) {

NSLog(@”长按触发多少次”);

}

}

旋转手势

1.初始化
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];

2.添加到视图上
[imageView addGestureRecognizer:rotation];

3.释放
[rotation release];

4.实现旋转方法
-(void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
电脑上测试按Alt键
形变属性 transform
参数一 要改变形变属性的视图
参数二 根据弧度去创建
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);

每次转需要把旋转的角度 重置为0
因为要接替上一次角度 开始旋转
rotation.rotation = 0;

NSLog(@”旋转。。。跳跃”);
}

捏合手势

1.初始化
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

2.添加到视图上
[imageView addGestureRecognizer:pinch];

3.释放
[pinch release];

4.实现捏合手势
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch
{

根据缩放的刻度(比例)改变形变属性

根据手势捏合的比例 去改变形变属性

pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);

重置捏合的比例
pinch.scale = 1;

NSLog(@”捏合”);
}

平移手势

1.初始化
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

2.添加到视图上
[imageView addGestureRecognizer:pan];

3.释放
[pan release];

4.实现平移方法
-(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(@”平移”);
}

轻扫

1.初始化
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];

2.设置左右扫的方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;

3.添加到视图上
[imageView addGestureRecognizer:swipe];

4.释放
[swipe release];

5.实现左扫
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@”左扫”);
}

边缘扫

1.初始化
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];

2.设置一下 从屏幕哪个边缘扫
screenEdgePan.edges = UIRectEdgeRight;

3.添加到视图上
[imageView addGestureRecognizer:screenEdgePan];

4.释放
[screenEdgePan release];

5.实现边缘扫
-(void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan
{
NSLog(@”一定是 靠近屏幕边缘开始扫 才能触发”);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值