分享

手势

 叹落花 2014-12-20

手势是指你用一个或多个手指接触屏幕开始,直到你的手指全部离开屏幕为止所发生的所有事件。手势识别器(UIGestureRecognizer)是一个对象,知道如何观察用户生成的事件流,并识别用户何时以与预定义的手势相匹配的方式进行拉触摸和拖动。UIGestureRecognizer类封装了查找手势的工作。在模拟器中,按“option”键,可模拟两个手指的手势。

在.h文件里创建一个视图对象,此对象是模拟拖动手势时的目标:

  1. @interface LinViewController : UIViewController  
  2. //创建视图对象  
  3. @property (retain, nonatomic) UIView * mView;  
  4.   
  5. @end  
在.m文件里,创建各个手势的对象,编写各个手势的实现方法:

  1. @implementation LinViewController  
  2.   
  3. - (void)viewDidLoad  
  4. {  
  5.     [super viewDidLoad];  
  6.       
  7.     //---点击手势识别器---//  
  8.     //创建点击手势的对象  
  9.     UITapGestureRecognizer * pSingleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];  
  10.     //将手势添加到当前视图中  
  11.     [self.view addGestureRecognizer:pSingleTap];  
  12.       
  13.     //创建点击手势的对象  
  14.     UITapGestureRecognizer * pDoubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];  
  15.     //设置点击次数为2次,默认为1次,属于单击操作  
  16.     pDoubleTap.numberOfTapsRequired = 2;  
  17.     //将手势添加到当前视图中  
  18.     [self.view addGestureRecognizer:pDoubleTap];  
  19.     //双击的时候让单击放弃响应  
  20.     [pSingleTap requireGestureRecognizerToFail:pDoubleTap];  
  21.     //释放创建的对象  
  22.     [pDoubleTap release];  
  23.     [pSingleTap release];  
  24.       
  25.     //---滑动手势识别器---//  
  26.     //创建滑动手势的对象  
  27.     UISwipeGestureRecognizer * pSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)];  
  28.     //设置滑动的方向,此处为向右,共有4个方向  
  29.     pSwip.direction = UISwipeGestureRecognizerDirectionRight;  
  30.     //将手势添加到当前视图中  
  31.     [self.view addGestureRecognizer:pSwip];  
  32.     //释放创建的对象  
  33.     [pSwip release];  
  34.       
  35.     //---拖动手势识别器--//  
  36.     //创建一个拖动手势动作的目标,假定为一个视图块  
  37.     self.mView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];  
  38.     self.mView.backgroundColor = [UIColor redColor];  
  39.     [self.view addSubview:self.mView];  
  40.       
  41.     //创建拖动手势对象  
  42.     UIPanGestureRecognizer * pPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];  
  43.     //将手势添加到当前视图中  
  44.     [self.view addGestureRecognizer:pPan];  
  45.     //释放创建的对象  
  46.     [pPan release];  
  47.       
  48.     //---长按手势识别器--//  
  49.     //创建长按手势的对象  
  50.     UILongPressGestureRecognizer * pLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];  
  51.     //设置长按的时间,2秒以上判定为长按  
  52.     pLongPress.minimumPressDuration = 2;  
  53.     //将手势添加到当前视图中  
  54.     [self.view addGestureRecognizer:pLongPress];  
  55.     //释放创建的对象  
  56.     [pLongPress release];  
  57.       
  58.     //---旋转手势识别器---//  
  59.     //创建旋转手势对象  
  60.     UIRotationGestureRecognizer * pRotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];  
  61.     //将手势添加到当前视图中  
  62.     [self.view addGestureRecognizer:pRotation];  
  63.     //释放创建的对象  
  64.     [pRotation release];  
  65. }  
  66. #pragma mark--------tapGesture>>>single  单击  
  67. - (void)singleGesture:(UITapGestureRecognizer *)tap  
  68. {  
  69.     //单击手势对应的操作  
  70.     NSLog(@"单击操作");  
  71. }  
  72. #pragma mark--------tapGesture>>>double  双击  
  73. - (void)doubleGesture:(UITapGestureRecognizer *)tap  
  74. {  
  75.     //双击手势对应的操作  
  76.     NSLog(@"双击操作");  
  77. }  
  78. #pragma mark--------tapGesture>>>Swip    滑动  
  79. - (void)swipGesture:(UISwipeGestureRecognizer *)swip  
  80. {  
  81.     //滑动,划屏手势对应的操作  
  82.     NSLog(@"滑动操作,分方向,此处向右滑动");  
  83. }  
  84. #pragma mark--------tapGesture>>>Pan     平移  
  85. - (void)panGesture:(UIPanGestureRecognizer *)pan  
  86. {  
  87.     //获取平移手势在视图上的坐标  
  88.     CGPoint point = [pan locationInView:self.view];  
  89.     //把视图的中心点确定在平移手势的坐标上,即把视图拖动到平移点  
  90.     self.mView.center = point;  
  91.     NSLog(@"%@",NSStringFromCGPoint(point));  
  92. }  
  93. #pragma mark--------tapGesture>>>LongPress 长按  
  94. - (void)longPressGesture:(UILongPressGestureRecognizer *)longPress  
  95. {  
  96.     //长按手势对应的操作  
  97.     NSLog(@"长按操作");  
  98. }  
  99. #pragma mark--------tapGesture>>>Rotation  旋转  
  100. - (void)rotationGesture:(UIRotationGestureRecognizer *)rotation  
  101. {  
  102.     //确定手势旋转的大小,角度,使视图做出相应的反应  
  103.     float degree = rotation.rotation *(180/M_PI);  
  104.     NSLog(@"旋转的角度为%.2f",degree);  
  105. }  
  106. //释放创建的对象  
  107. - (void)dealloc  
  108. {  
  109.     [_mView release];  
  110.     [super dealloc];  
  111. }  
  112.   
  113. - (void)didReceiveMemoryWarning  
  114. {  
  115.     [super didReceiveMemoryWarning];  
  116. }  
  117.   
  118. @end  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多