码迷,mamicode.com
首页 > 其他好文 > 详细

基础手势识别

时间:2016-04-04 01:19:24      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

一、UIGestureRecognizer介绍

 
 
手势在ios经常用到,可以说是非常重要的存在,在之前,大部分都是通过touch方法来进行手势的判定,但是在ios系统3.2之后,添加了UIGestureRecongnizer抽象类,抽象出了几个子类
 
 
这些手势都很简单,基本上明白一个的用法,其他的也就都明白了,所以我们今天就以 拖动为例,给大家做一个侧滑展开菜单栏
 
我们先来了解一下这些手势的基本用法
 

1、缩放手势

UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self  action:@selector(handlePinch:)];  
[imageView addGestureRecognizer:pinchGestureRecognizer];  

回调方法

- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer  
{  
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);  
    recognizer.scale = 1;  
}  

2.拖动手势

UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];  
 //添加手势到 tabBarController.view  
 [tabBarController.view addGestureRecognizer:panRecognizer];  

回调方法

- (void) handlePan:(UIPanGestureRecognizer*) recognizer  
{  
    CGPoint translation = [recognizer translationInView:self.view];  
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,recognizer.view.center.y + translation.y);  
    [recognizer setTranslation:CGPointZero inView:self.view];  
      
}  

3.旋转手势

/** 
 *  处理旋转手势 
 * 
 *  @param recognizer 旋转手势识别器对象实例 
 */  
- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer 
{ recognizer.view.transform
= CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); recognizer.rotation = 0.0; }

 

大家需要注意的是,一个手势只能对应一个View, 但是一个View可以有多个手势

 

 

 

 

 

 

 

 

基础手势识别

标签:

原文地址:http://www.cnblogs.com/CLiOS/p/5351165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!