Swift - 各种手势检测大全(UIGestureRecognizer及其子类)
UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var swipe = UISwipeGestureRecognizer(target:self, action:Selector("swipe:")) swipe.direction = UISwipeGestureRecognizerDirection.Up self.view.addGestureRecognizer(swipe) } func swipe(recognizer:UISwipeGestureRecognizer){ println("swipe ok") var point=recognizer.locationInView(self.view) //这个点是滑动的起点 println(point.x) println(point.y) }} |
|
1
2
|
//表示监听滑动的方向为向上swipe.direction = UISwipeGestureRecognizerDirection.Up |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var swipeUp = UISwipeGestureRecognizer(target:self, action:Selector("swipe:")) swipeUp.direction = UISwipeGestureRecognizerDirection.Up self.view.addGestureRecognizer(swipeUp) var swipeDown = UISwipeGestureRecognizer(target:self, action:Selector("swipe:")) swipeDown.direction = UISwipeGestureRecognizerDirection.Down self.view.addGestureRecognizer(swipeDown) } func swipe(recognizer:UISwipeGestureRecognizer){ if recognizer.direction == UISwipeGestureRecognizerDirection.Up{ println("向上滑动") }else if recognizer.direction == UISwipeGestureRecognizerDirection.Down{ println("向下滑动") } var point=recognizer.locationInView(self.view) //这个点是滑动的起点 println(point.x) println(point.y) }} |
2,UITapGestureRecognizer:轻点手势(点击)
(1)可以通过numberOfTouchesRequired属性设置触摸点数,比如设置2表示必须两个手指触摸时才会触发
(2)通过numberOfTapsRequired属性设置点击次数,单击设置为1,双击设置为2
(3)如果一个控件既监听了单击事件也监听了双击事件,默认当双击事件触发的时候也同时会触发单击事件。如果想双击时不触发单击,需要通过requireGestureRecognizerToFail进行设置
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //单击监听 var tapSingle=UITapGestureRecognizer(target:self,action:"tapSingleDid") tapSingle.numberOfTapsRequired=1 tapSingle.numberOfTouchesRequired=1 //双击监听 var tapDouble=UITapGestureRecognizer(target:self,action:"tapDoubleDid:") tapDouble.numberOfTapsRequired=2 tapDouble.numberOfTouchesRequired=1 //声明点击事件需要双击事件检测失败后才会执行 tapSingle.requireGestureRecognizerToFail(tapDouble); self.view.addGestureRecognizer(tapSingle) self.view.addGestureRecognizer(tapDouble) } func tapSingleDid(){ println("单击了") } func tapDoubleDid(sender:UITapGestureRecognizer){ if sender.view == self.view{ println("双击了") } } } |
3,UIPinchGestureRecognizer:捏合手势(两个手指进行放大缩小)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //设置监听方法为pinchDid方法 var pinch=UIPinchGestureRecognizer(target:self,action:"pinchDid:") self.view.addGestureRecognizer(pinch) } func pinchDid(recognizer:UIPinchGestureRecognizer) { //在监听方法中可以实时获得捏合的比例 println(recognizer.scale); }} |
4,UIRotationGestureRecognizer:旋转手势(两个手指进行旋转)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var rotation=UIRotationGestureRecognizer(target:self,action:"rotationDid:") self.view.addGestureRecognizer(rotation) } func rotationDid(recognizer:UIRotationGestureRecognizer){ //旋转的弧度转换为角度 println(recognizer.rotation*(180/CGFloat(M_PI))) }} |
5,UIPanGestureRecognizer:拖动手势
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class ViewController: UIViewController { var rect:UIView! override func viewDidLoad() { super.viewDidLoad() rect=UIView(frame:CGRectMake(0, 0, 100, 100)) rect.center=self.view.center rect.backgroundColor=UIColor.redColor() self.view.addSubview(rect) var pan = UIPanGestureRecognizer(target:self,action:"panDid:") pan.maximumNumberOfTouches=1 rect.addGestureRecognizer(pan) } func panDid(recognizer:UISwipeGestureRecognizer){ var point=recognizer.locationInView(self.view) //设置矩形的位置 rect.center=point } } |
6,UILongPressGestureRecognizer:长按
Swift - 各种手势检测大全(UIGestureRecognizer及其子类)的更多相关文章
- swift - 各种手势用法大全
UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有: 1.首先创建几个view,来用于手势的检测 let view1 = UIView() let view2 ...
- iOS 手势识别器(UIGestureRecognizer)
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势. UIGestureRecognizer的子类有: UITapGestureRecogni ...
- swift 中手势的使用
swift 中手势的使用 /**点击手势*/ func tapGestureDemo() { //建立手势识别器 let gesture = UITapGestureRecognizer(target ...
- iOS8 Core Image In Swift:人脸检测以及马赛克
iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...
- 9.3、Libgdx手势检测
(官网:www.libgdx.cn) 触摸屏在输入的基础上增加了手势检测,比如两个手指实现缩放,单击或双击屏幕,长按屏幕等. Libgdx提供了GestureDetector来帮助你检测以下手势: t ...
- Android 手势检测实战 打造支持缩放平移的图片预览效果(下)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39480503,本文出自:[张鸿洋的博客] 上一篇已经带大家实现了自由的放大缩小图 ...
- 看完这篇还不会 GestureDetector 手势检测,我跪搓衣板!
引言 在 android 开发过程中,我们经常需要对一些手势,如:单击.双击.长按.滑动.缩放等,进行监测.这时也就引出了手势监测的概念,所谓的手势监测,说白了就是对于 GestureDetector ...
- UIGestureRecongnizer 手势检测 swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- ios的手势操作之UIGestureRecognizer浅析
转载地址:http://blog.csdn.net/likendsl/article/details/7554150 每一个手势的实现例子,可参考下面网址:http://www.cnblogs.com ...
随机推荐
- 1 #安装php
#安装php #备注:php5..3以后的版本源码不需要打php-fpm补丁,该补丁已经集成进5..3中强制启用fastcgi. [root@dba01 nginx-]# cd [root@dba01 ...
- MAMP:在 OSX 中搭建 Apache, MySQL, PHP 环境并本地安装、调试 WordPress
MAMP 这个名字来源于 Macintosh Apache MySQL PHP,显然专门用来在 Mac 环境下搭建 Apache.MySQL.PHP 平台. 虽然 OSX 中已经预装了 Apache ...
- perl 传递对象到模块
perl 中的对象 就是引用 通过new方法传递数据结构给各个模块 [root@wx03 test]# cat x1.pm package x1; use Data::Dumper; sub new ...
- Windows Azure 安全最佳实践 - 第 7 部分:提示、工具和编码最佳实践
在撰写这一系列文章的过程中,我总结出了很多最佳实践.在这篇文章中,我介绍了在保护您的WindowsAzure应用程序时需要考虑的更多事项. 下面是一些工具和编码提示与最佳实践: · 在操作系统上运行 ...
- TCP/IP笔记 三.运输层(1)——UDP,TCP
1. 运输层 1.1 两种协议:TCP和UDP. (1)TCP:提供了一种可靠的数据传输服务,TCP是面向连接的,只有链接建立起来后才能通信. (2)UDP:是把数据直接发出去,而不管对方是不是在收信 ...
- Linux下搭建Hadoop集群
本文地址: 1.前言 本文描述的是如何使用3台Hadoop节点搭建一个集群.本文中,使用的是三个Ubuntu虚拟机,并没有使用三台物理机.在使用物理机搭建Hadoop集群的时候,也可以参考本文.首先这 ...
- Server-side Sessions with Redis | Flask (A Python Microframework)
Server-side Sessions with Redis | Flask (A Python Microframework) Server-side Sessions with Redis By ...
- MongoDB---性能优化---(1)
MONGODB数据架构 升级解决.计划 发现问题 应用server用户数的突然涌入,创建server反应慢 检查server,我发现,每次反应非常慢,至30ops 检查过程 .发现数据库查询缓 ...
- EasyUI - DataGrid 组建 - [ 新增功能 ]
效果: html代码: <div> <!--使用JS加载方式--> <table id="tab"></table> <!-- ...
- HTTP协议--简析
HTTP--超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议,是所有的www文件都必须遵守的标准. 要想成为优秀的web开发人员,必须熟悉H ...