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

 更新时间:2015年01月09日 12:46:34   投稿:junjie  
这篇文章主要介绍了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
}

相关文章

  • swift3.0键盘弹起遮挡输入框问题的解决方案

    swift3.0键盘弹起遮挡输入框问题的解决方案

    这篇文章主要介绍了swift3.0键盘弹起遮挡输入框问题的解决方案,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-11-11
  • Swift 中的 JSON 反序列化示例详解

    Swift 中的 JSON 反序列化示例详解

    这篇文章主要为大家介绍了Swift中的JSON 反序列化示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Swift教程之属性详解

    Swift教程之属性详解

    这篇文章主要介绍了Swift教程之属性详解,属性是描述特定类、结构或者枚举的值,计算属性存在于类、结构与枚举中,存储属性仅仅只在类与结构中,需要的朋友可以参考下
    2015-01-01
  • RxSwift学习之Observable的新建、订阅及取消订阅

    RxSwift学习之Observable的新建、订阅及取消订阅

    这篇文章主要给大家介绍了关于RxSwift学习教程之Observable的相关资料,文中详细的给大家介绍了关于新建Observable、订阅Observable和取消订阅并消除内存泄漏等相关的内容,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-09-09
  • SwiftUI开发总结combine原理简单示例详解

    SwiftUI开发总结combine原理简单示例详解

    这篇文章主要为大家介绍了SwiftUI开发总结combine原理简单示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • swift在IOS应用图标上添加提醒个数的方法

    swift在IOS应用图标上添加提醒个数的方法

    本文是通过swift语言实现在应用图标右上角添加消息个数提醒的功能,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
    2016-08-08
  • 详解Swift编程中的for循环的编写方法

    详解Swift编程中的for循环的编写方法

    这篇文章主要介绍了Swift编程中的for循环的编写方法,包括相关的for...in循环,需要的朋友可以参考下
    2015-11-11
  • Swift 字符串类型及常用方法详解总结

    Swift 字符串类型及常用方法详解总结

    Swift 字符串是一系列字符的集合。例如 "Hello, World!" 这样的有序的字符类型的值的集合,它的数据类型为 String,接下来文章将详细探讨
    2021-11-11
  • Swift使用enum抹平数组元素差异实例详解

    Swift使用enum抹平数组元素差异实例详解

    这篇文章主要为大家介绍了Swift使用enum抹平数组元素差异实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Swift中Optional值的链式调用学习笔记

    Swift中Optional值的链式调用学习笔记

    这篇文章主要介绍了Swift中Optional值的链式调用学习笔记,Optional链是Swift入门学习中的基础知识,需要的朋友可以参考下
    2016-07-07

最新评论