iOS疯狂讲解之手势识别器

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    // 创建一个imageView
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    imageView.image = [UIImage imageNamed:@"u=2473889012,1045156706&fm=21&gp=0.jpg"];
    //  UIImageView 默认交互是关闭的 ,所以需要打开
    imageView.userInteractionEnabled = YES;
    
    [self.view addSubview:imageView];
    [imageView release];
    
    // 手机识别器这个类 , 是一个抽象类
    // 自己本身不实现什么具体功能
    //具体功能都是由其子类来实现
 
    // 第一个:轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap:)];
    // 用 几个手指 轻拍
    tap.numberOfTouchesRequired = 2; // 默认是1
    // 轻拍的次数
    tap.numberOfTapsRequired = 1; // 默认是1
    // 把手势添加到要点击轻拍的视图上
    [imageView addGestureRecognizer:tap];
    [tap release];
  
    /*
    //第二个: 长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionLongPress:)];
    // 最短长按时间
    longPress.minimumPressDuration = 1;
    // 添加到视图上
    [imageView addGestureRecognizer:longPress];
    [longPress release];
    
    */
    /*
    // 第三个: 旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(actionRotation:)];
    // 添加到视图上
    [imageView addGestureRecognizer:rotation];
    [rotation release];
    */
    /*
    // 第四个: 捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(actionPinch:)];
    // 直接添加到视图上
    [imageView addGestureRecognizer:pinch];
    [pinch release];
    */
    /*
    // 第五个 平移手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(actionPan:)];
    // 添加到视图上
    [imageView addGestureRecognizer:pan];
    [pan release];
    */
    
//    // 第六个 清扫手势
//    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(actionSwipe:)];
//    // 设置左右扫
//    swipe.direction = UISwipeGestureRecognizerDirectionLeft; // 默认是右扫
//    
//    // 添加到视图上
//    [imageView addGestureRecognizer:swipe];
//    [swipe release];
    // 第七个: 边缘扫 是默认的 不设置也有边缘扫
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(actionScreenEdgePan:)];
    //  设置从屏幕边缘 那边去扫
    screenEdgePan.edges = UIRectEdgeLeft;
    [imageView addGestureRecognizer:screenEdgePan];
    [screenEdgePan release];
    
    
    
    
    
    
}

//1, 轻拍触发方法
- (void)actionTap:(UITapGestureRecognizer *)tap
{
    NSLog(@"轻拍了");
}
// 2,长按触发方法
- (void)actionLongPress:(UILongPressGestureRecognizer *)longPress
{
    // 长按 换图片
    // 获取到长按的view
    UIImageView *imageView = (UIImageView *)longPress.view;
    imageView.image = [UIImage imageNamed:@"u=3484715933,459413563&fm=23&gp=0.jpg"];
    NSLog(@"长按手势实现");
}
// 3,旋转触发方法
- (void)actionRotation:(UIRotationGestureRecognizer *)rotation
{
    // transform 形变属性
    // 描述一下这个方法
    // 第一个参数 传入要创建那个视图的形变属性
    // 第二个参数 传入 手势的弧度
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    // 把弧度重置
    rotation.rotation = 0;
    NSLog(@"旋转了");
}

//4, 捏合触发方法
- (void)actionPinch:(UIPinchGestureRecognizer *)pinch
{
    // 形变属性控制 捏合
    // 第二个参数 按捏合的刻度比例 形变
    pinch.view.transform  = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    // 重置刻度比例
    pinch.scale = 1;
    NSLog(@"捏合手势");
}

// 5. 平移触发方法
- (void)actionPan:(UIPanGestureRecognizer *)pan
{
    // 这个点 以手指触摸到屏幕那一刻 即为0,0点
    CGPoint p = [pan translationInView:pan
                 .view];
    NSLog(@"%@", NSStringFromCGPoint(p));
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
    // 重置 点 让他以为每次都是刚开始触发
    [pan setTranslation:CGPointMake(0, 0) inView:pan.view];
    
    
    //[pan translationInView:self.view];
}

// 6. 左右扫触发方法
- (void)actionSwipe:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"左扫了");
    
    
}

// 7. 边缘扫触发方法
- (void)actionScreenEdgePan:(UIScreenEdgePanGestureRecognizer *)screenEdgePan
{
    // 刚一扫就触发
    if (screenEdgePan.state == UIGestureRecognizerStateBegan) {
         NSLog(@"边缘扫 左扫");
    }
   
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值