UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有:
2,UITapGestureRecognizer:轻点手势(点击)
(1)可以通过numberOfTouchesRequired属性设置触摸点数,比如设置2表示必须两个手指触摸时才会触发
(2)通过numberOfTapsRequired属性设置点击次数,单击设置为1,双击设置为2
(3)如果一个控件既监听了单击事件也监听了双击事件,默认当双击事件触发的时候也同时会触发单击事件。如果想双击时不触发单击,需要通过requireGestureRecognizerToFail进行设置
3,UIPinchGestureRecognizer:捏合手势(两个手指进行放大缩小)
4,UIRotationGestureRecognizer:旋转手势(两个手指进行旋转)
5,UIPanGestureRecognizer:拖动手势
6,UILongPressGestureRecognizer:长按
1,UISwipeGestureRecognizer:滑动(快速移动)
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()
let
swipe =
UISwipeGestureRecognizer
(target:
self
, action:
Selector
(
"swipe:"
))
swipe.direction =
UISwipeGestureRecognizerDirection
.
Up
self
.view.addGestureRecognizer(swipe)
}
func
swipe(recognizer:
UISwipeGestureRecognizer
){
print
(
"swipe ok"
)
var
point=recognizer.locationInView(
self
.view)
//这个点是滑动的起点
print
(point.x)
print
(point.y)
}
}
|
注意1:如果UISwipeGestureRecognizer在不指定方向的时候,默认向右滑动才会触发事件。如果要指定方向,需要设置direction属性
1
2
|
//表示监听滑动的方向为向上
swipe.direction =
UISwipeGestureRecognizerDirection
.
Up
|
注意2:有网友问如果各个方向都要响应怎么办,只要多定义几个UISwipeGestureRecognizer就可以了
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()
let
swipeUp =
UISwipeGestureRecognizer
(target:
self
, action:
Selector
(
"swipe:"
))
swipeUp.direction =
UISwipeGestureRecognizerDirection
.
Up
self
.view.addGestureRecognizer(swipeUp)
let
swipeDown =
UISwipeGestureRecognizer
(target:
self
, action:
Selector
(
"swipe:"
))
swipeDown.direction =
UISwipeGestureRecognizerDirection
.
Down
self
.view.addGestureRecognizer(swipeDown)
}
func
swipe(recognizer:
UISwipeGestureRecognizer
){
if
recognizer.direction ==
UISwipeGestureRecognizerDirection
.
Up
{
print
(
"向上滑动"
)
}
else
if
recognizer.direction ==
UISwipeGestureRecognizerDirection
.
Down
{
print
(
"向下滑动"
)
}
let
point=recognizer.locationInView(
self
.view)
//这个点是滑动的起点
print
(point.x)
print
(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()
//单击监听
let
tapSingle=
UITapGestureRecognizer
(target:
self
,action:
"tapSingleDid"
)
tapSingle.numberOfTapsRequired=1
tapSingle.numberOfTouchesRequired=1
//双击监听
let
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(){
print
(
"单击了"
)
}
func
tapDoubleDid(sender:
UITapGestureRecognizer
){
if
sender.view ==
self
.view{
print
(
"双击了"
)
}
}
}
|
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方法
let
pinch=
UIPinchGestureRecognizer
(target:
self
,action:
"pinchDid:"
)
self
.view.addGestureRecognizer(pinch)
}
func
pinchDid(recognizer:
UIPinchGestureRecognizer
)
{
//在监听方法中可以实时获得捏合的比例
print
(recognizer.scale);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class
ViewController
:
UIViewController
{
override
func
viewDidLoad() {
super
.viewDidLoad()
let
rotation=
UIRotationGestureRecognizer
(target:
self
,action:
"rotationDid:"
)
self
.view.addGestureRecognizer(rotation)
}
func
rotationDid(recognizer:
UIRotationGestureRecognizer
){
//旋转的弧度转换为角度
print
(recognizer.rotation*(180/
CGFloat
(
M_PI
)))
}
}
|
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)
let
pan =
UIPanGestureRecognizer
(target:
self
,action:
"panDid:"
)
pan.maximumNumberOfTouches=1
rect.addGestureRecognizer(pan)
}
func
panDid(recognizer:
UISwipeGestureRecognizer
){
let
point=recognizer.locationInView(
self
.view)
//设置矩形的位置
rect.center=point
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import
UIKit
class
ViewController
:
UIViewController
{
override
func
viewDidLoad() {
super
.viewDidLoad()
//长按监听
let
longPress=
UILongPressGestureRecognizer
(target:
self
,action:
"longPressDid:"
)
self
.view.addGestureRecognizer(longPress)
}
func
longPressDid(sender:
UILongPressGestureRecognizer
){
if
sender.state == .
Began
{
print
(
"长按响应开始"
)
}
else
{
print
(
"长按响应结束"
)
}
}
}
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_546.html