文档章节

手势

Hown
 Hown
发布于 2016/01/13 09:36
字数 584
阅读 57
收藏 3

手势分为点击手势、捏合、滑动、左右滑动、长按、旋转等手势。

点击手势:

初始化

UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizeralloc] initWithTarget:self
                                           action:@selector(fingerIncident:)];

设置手指数

singleFingerOne.numberOfTouchesRequired = 1;

设置点击数

singleFingerOne.numberOfTapsRequired = 1;

设置代理

singleFingerOne.delegate= self;

添加到所属的view

[self.imageView addGestureRecognizer:singleFingerOne];

当单指单击与双击同时存在时,双击时会首先触发单击手势,因此需要对双击做注册

[singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];

同理,触发双指点击时也会有先触发单指手势的情况

[doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
//手指点击事件
-
(void)fingerIncident:(UITapGestureRecognizer *)sender
{
   if(sender.numberOfTouchesRequired==1)
{
    //单指点击事件
    if(sender.numberOfTapsRequired == 1) {
      //单指单击
      NSLog(@"单指单击");
      [sender.view.layer removeAllAnimations];
    }
    elseif(sender.numberOfTapsRequired ==2 ){
      //单指双击
      NSLog(@"单指双击");
    }
  }
   elseif (sender.numberOfTouchesRequired==2)
{
    //双指点击事件
    if(sender.numberOfTapsRequired == 1) {
      //双指单击
      NSLog(@"双指单击");
    }
    elseif(sender.numberOfTapsRequired ==2 ){
      //双指双击
      NSLog(@"双指双击");
    }
  }
}

2.捏合手势

UIPinchGestureRecognizer*pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self
                                       action:@selector(handlePinchGesture:)];
//imageView添加手势识别 
[self.imageView addGestureRecognizer:pinchGesture];
//手指捏事件
-(void)handlePinchGesture:(UIPinchGestureRecognizer *)sender{
  NSLog(@"捏");
   
   if([sender state] ==UIGestureRecognizerStateEnded)
{
    lastScale =1.0;
    return;
  }
   
  CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)senderscale]);
   
  CGAffineTransform newTransform =CGAffineTransformScale(sender.view.transform, scale, scale);
   
  [sender.view setTransform:newTransform];
  lastScale = [sender scale];
}

3.旋转手势

UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizeralloc]initWithTarget:
                                           self
                                           action:@selector(handleRotateGesture:)];
[self.imageView addGestureRecognizer:rotateGesture];
//手指旋转事件
-(void)handleRotateGesture:(UIRotationGestureRecognizer *)sender{
  NSLog(@"旋转");
   
   NSLog(@"sender.scale=%f",sender.rotation);
   NSLog(@"sender.velocity=%f",sender.velocity);

  sender.view.transform=CGAffineTransformRotate(sender.view.transform,
sender.rotation);
  sender.rotation=0;
}

4.拖手势

UIPanGestureRecognizer*panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self 
                                   action:@selector(handlePanGesture:)];  
[self.imageView addGestureRecognizer:panGesture];
//手指拖事件
-(void)handlePanGesture:(UIPanGestureRecognizer *)sender{
  NSLog(@"拖");
  NSLog(@"sender=%@",sender);
   CGPoint translation=[(UIPanGestureRecognizer*)sendertranslationInView:self.view];
   
  //开始
   if(sender.state==UIGestureRecognizerStateBegan) {
    x=sender.view.center.x;
    y=sender.view.center.y;
  }
   //进行中
   if(sender.state==UIGestureRecognizerStateChanged)
{
    sender.view.center=CGPointMake(x+translation.x, y+translation.y);
  }
  //结束
   if(sender.state==UIGestureRecognizerStateEnded) {
    sender.view.center=CGPointMake(x+translation.x, y+translation.y);
  }
}

5.左划

UISwipeGestureRecognizer*swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self
                                           action:@selector(handleSwipeGesture:)]; 
swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不设置默认是右
[self.imageView addGestureRecognizer:swipeLeftGesture];

6.右划

UISwipeGestureRecognizer*swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self 
                                      action:@selector(handleSwipeGesture:)]; 
 [self.imageView addGestureRecognizer:swipeGesture];
//手指左右滑动事件
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender{
   if(sender.direction==UISwipeGestureRecognizerDirectionLeft)
{
    NSLog(@"左滑");
  }
  else{
    NSLog(@"右滑");
  }
}

7.长按

UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizeralloc]initWithTarget:self
                                                action:@selector(handleLongpressGesture:)];
   //长按时间为1秒
  longpressGesutre.minimumPressDuration=0.5;
   //允许15秒中运动
  longpressGesutre.allowableMovement=3;
   //所需触摸1次
  longpressGesutre.numberOfTouchesRequired=1;
  [self.imageView addGestureRecognizer:longpressGesutre];
  [longpressGesutrerelease];
}
//手指长按事件
-(void)handleLongpressGesture:(UILongPressGestureRecognizer *)sender{
  NSLog(@"长按");
   if(sender.state==UIGestureRecognizerStateBegan) {
    CABasicAnimation* rotationAnimation_ =
[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //角度转弧度
    rotationAnimation_.fromValue = [NSNumber numberWithFloat:(-0.02)];
    rotationAnimation_.toValue = [NSNumber numberWithFloat:(0.02)];
    //动画时间
    rotationAnimation_.duration = 0.1f;
    //动画次数
    rotationAnimation_.repeatCount=FLT_MAX;
    //回退动画
    rotationAnimation_.autoreverses = YES;
    //动画开始结束的快慢,设置为加速
    rotationAnimation_.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [[sender.view layer] addAnimation:rotationAnimation_forKey:@"revItUpAnimation"];
  }
}


© 著作权归作者所有

Hown
粉丝 1
博文 9
码字总数 11384
作品 0
广州
项目经理
私信 提问

暂无文章

Jenkins系列_插件安装及报错处理

进入Jenkins之后我们可以进行插件的安装,插件管理位于以下模块: 发现上面报了一堆错误,是因为插件的依赖没有安装好,那么这一节,就先把这些错误解决掉吧。解决完成后,也就基本会使用插件...

shzwork
今天
2
0
mysql mysql的所有查询语句和聚合函数(整理一下,忘记了可以随时看看)

查询所有字段 select * from 表名; 查询自定字段 select 字段名 from 表名; 查询指定数据 select * from 表名 where 条件; 带关键字IN的查询 select * from 表名 where 条件 [not] in(元素...

edison_kwok
昨天
9
0
多线程同时加载缓存实现

import com.google.common.cache.Cache;import com.google.common.cache.CacheBuilder;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorServi......

暗中观察
昨天
3
0
利用VisualVM 内存查看

准备工作,建几个测试类。等下就是要查看这几个类里面的属性 package visualvm;public class MultiObject { private String str; private int i; MultiObject(String str...

冷基
昨天
2
0
组装一台工作游戏两用机

一、配置清单如下: 分类 项目 价格(元) 主板 华硕(ASUS)TUF Z370-PLUS GAMING II 电竞特工 Z370二代 支持9代CPU 1049 CPU 英特尔(Intel) i7 8700K 酷睿六核 盒装CPU处理器 2640 风扇 九...

mbzhong
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部