iOS 六种手势详解 UIGestureRecognizer

  • UITapGestureRecognizer   轻拍

  • UIPinchGestureRecognizer  捏合

  • UIRotationGestureRecognizer  旋转

  • UISwipeGestureRecognizer   轻扫

  • UIPanGestureRecognizer   拖拽

  • UILongPressGestureRecognizer  长按


具体实现代码如下(需要哪种就把注释部分粘贴下就可以了): 

- (void)viewDidLoad 

    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor grayColor]; 

// Do any additional setup after loading the view. 

    UIImage *image = [UIImage imageNamed:@"iphone.jpg"]; 

    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

     

    //设置UIImage可接受交互 

    imageView.userInteractionEnabled = YES; 

     

    [imageView setFrame:CGRectMake(20, 20, 280, 440)]; 

    [self.view addSubview:imageView]; 

    [imageView release]; 

     

/*   

 *  轻拍手势 

  

//  轻拍 单击 

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; 

    

//  单击 

    tap.numberOfTapsRequired = 1; 

//  试图添加一个手势,UIView的方法 

    [imageView addGestureRecognizer:tap]; 

    [tap release]; 

     

//  轻拍 双击 

    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)]; 

     

//  双击 

    tap1.numberOfTapsRequired = 2; 

//  试图添加一个手势,UIView的方法 

    [imageView addGestureRecognizer:tap1]; 

    [tap1 release]; 

*/ 

     

     

/*   

 *  拖拽手势   

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; 

    [imageView addGestureRecognizer:pan]; 

    [pan release]; 

*/    

  

     

/* 

 *  捏合手势 

  

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; 

    [imageView addGestureRecognizer:pinch]; 

    [pinch release]; 

  

*/ 

     

/* 

 *  旋转手势 

  

     

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; 

    [imageView addGestureRecognizer:rotation]; 

    [rotation release]; 

*/ 

     

/* 

 *  轻扫手势 

  

  

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; 

    //设置轻扫方向 

    swipe.direction = UISwipeGestureRecognizerDirectionDown;  

    [imageView addGestureRecognizer:swipe]; 

    [swipe release]; 

*/ 

     

/* 

 *  长按手势 

 */ 

  

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 

    [imageView addGestureRecognizer:longPress]; 

    longPress.minimumPressDuration = 1.0; 

    [longPress release]; 

     

  

//   轻拍手势响应单击方法 

-(void) tap:(UITapGestureRecognizer *) tap 

    NSLog(@"是否相应 Tap !"); 

  

//  轻拍手势响应双击方法 

-(void) tap1:(UITapGestureRecognizer *) tap 

//  随机颜色 

    UIColor *randomColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha: 1.0]; 

     

    self.view.backgroundColor = randomColor; 

  

//  拖拽手势响应事件 

-(void) pan:(UIPanGestureRecognizer *) pan 

    CGPoint point = [pan translationInView:pan.view]; 

    NSLog(@"point = %@", NSStringFromCGPoint(point)); 

    NSLog(@"center = %@", NSStringFromCGPoint(pan.view.center)); 

    pan.view.transform = CGAffineTransformMakeTranslation(point.x, point.y); 

  

//  捏合手势响应事件 

-(void) pinch:(UIPinchGestureRecognizer *)pinch 

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

  

//  旋转响应事件 

-(void) rotation:(UIRotationGestureRecognizer *) rotation 

//  旋转是多少弧度 

    rotation.view.transform = CGAffineTransformMakeRotation(rotation.rotation); 

  

//  轻扫手势响应事件 

-(void) swipe:(UISwipeGestureRecognizer*) swipe 

    //  随机颜色 

    UIColor *randomColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha: 1.0]; 

     

    self.view.backgroundColor = randomColor; 

  

//  长按手势响应事件 

-(void) longPress:(UILongPressGestureRecognizer*) longPress 

    if(longPress.state == UIGestureRecognizerStateEnded) 

    { 

        //  随机颜色 

        UIColor *randomColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha: 1.0]; 

         

        self.view.backgroundColor = randomColor; 

    } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值