服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Swift - Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍

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

2020-12-17 15:46Swift教程网 Swift

这篇文章主要介绍了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
}

 

延伸 · 阅读

精彩推荐
  • SwiftSwift教程之基础数据类型详解

    Swift教程之基础数据类型详解

    这篇文章主要介绍了Swift教程之基础数据类型详解,本文详细讲解了Swift中的基本数据类型和基本语法,例如常量和变量、注释、分号、整数、数值类型转换等...

    Swift教程网5162020-12-18
  • SwiftSwift能代替Objective-C吗?

    Swift能代替Objective-C吗?

    这是我在网上上看到的答案,复制粘贴过来和大家分享一下,因为我和很多人一样很关心Swift的出现对Mac开发的影响和对Objective-C的影响。...

    Swift教程网4412020-12-16
  • Swiftswift where与匹配模式的实例详解

    swift where与匹配模式的实例详解

    这篇文章主要介绍了swift where与匹配模式的实例详解的相关资料,这里附有简单的示例代码,讲的比较清楚,需要的朋友可以参考下...

    追到梦的魔术师14382021-01-06
  • Swiftmac git xcrun error active developer path 错误

    mac git xcrun error active developer path 错误

    本文主要是讲诉了如何解决在mac下使用git;xcode4.6的环境时,出现了错误(mac git xcrun error active developer path)的解决办法,希望对大家有所帮助...

    Swift教程网2232020-12-16
  • SwiftSwift使用CollectionView实现广告栏滑动效果

    Swift使用CollectionView实现广告栏滑动效果

    这篇文章主要为大家详细介绍了Swift使用CollectionView实现广告栏滑动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Stevin的技术博客12372021-01-13
  • SwiftSwift中转义闭包示例详解

    Swift中转义闭包示例详解

    在Swift 中的闭包类似于结构块,并可以在任何地方调用,下面这篇文章主要给大家介绍了关于Swift中转义闭包的相关资料,需要的朋友可以参考下...

    小小小_小朋友11412021-12-26
  • SwiftSwift的74个常用内置函数介绍

    Swift的74个常用内置函数介绍

    这篇文章主要介绍了Swift的74个常用内置函数介绍,这篇文章列举出了所有的Swift库函数,内置函数是指无需引入任何模块即可以直接使用的函数,需要的朋友可...

    Swift教程网5802020-12-19
  • SwiftSwift实现多个TableView侧滑与切换效果

    Swift实现多个TableView侧滑与切换效果

    这篇文章主要为大家详细介绍了Swift实现多个TableView侧滑与切换效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    乞力马扎罗的雪雪5822021-01-08