服务器之家

服务器之家 > 正文

Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍

时间:2020-12-17 15:46     来源/作者:Swift教程网

1.UITapGestureRecognizer 点击/双击手势

复制代码 代码如下:

var tapGesture = UITapGestureRecognizer(target: self, action: "handleTapGesture:") 
//设置手势点击数,双击:点2下 
tapGesture.numberOfTapsRequired = 2 
self.view.addGestureRecognizer(tapGesture)


2.UIPinchGestureRecognizer 捏 (放大/缩小)手势

复制代码 代码如下:

var pinchGesture = UIPinchGestureRecognizer(target: self, action: "handlePinchGesture:") 
self.view.addGestureRecognizer(pinchGesture)


3.UIRotationGestureRecognizer 旋转手势

复制代码 代码如下:

var rotateGesture = UIRotationGestureRecognizer(target: self, action: "handleRotateGesture:") 
 self.view.addGestureRecognizer(rotateGesture) 


4. UIPanGestureRecognizer 拖动手势

复制代码 代码如下:

 var panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:") 
 self.view.addGestureRecognizer(panGesture) 


5. UISwipeGestureRecognizer 划动手势

复制代码 代码如下:

var swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:") 
swipeGesture.direction = UISwipeGestureRecognizerDirection.Left //不设置是右 
self.view.addGestureRecognizer(swipeGesture)


6. UILongPressGestureRecognizer 长按手势

复制代码 代码如下:


   var longpressGesutre = UILongPressGestureRecognizer(target: self, action: "handleLongpressGesture:") 
    //长按时间 
    // longpressGesutre.minimumPressDuration
    //所需触摸次数
    /// longpressGesutre.numberOfTouchesRequired 
    self.view.addGestureRecognizer(longpressGesutre) 
UIGestureRecognizerState 枚举定义如下

 

enum UIGestureRecognizerState : Int {

    case Possible // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state

    case Began // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
    case Changed // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
    case Ended // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    case Cancelled // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible

    case Failed // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
}

 

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部