iOS疯狂讲解之手势识别器

来源:互联网 发布:zip linux 解压命令 编辑:程序博客网 时间:2024/05/16 08:42
#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