UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有:
2
18
2,UITapGestureRecognizer:轻点手势(点击)
(1)可以通过numberOfTouchesrequired属性设置触摸点数,比如设置2表示必须两个手指触摸时才会触发
(2)通过numberOfTapsrequired属性设置点击次数,单击设置为1,双击设置为2
(3)如果一个控件既监听了单击事件也监听了双击事件,默认当双击事件触发的时候也同时会触发单击事件。如果想双击时不触发单击,需要通过requireGestureRecognizerToFail进行设置
27
3,UIPinchGestureRecognizer:捏合手势(两个手指进行放大缩小)
4,UIRotationGestureRecognizer:旋转手势(两个手指进行旋转)
14
5,UIPanGestureRecognizer:拖动手势
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
.view.addGestureRecognizer(swipe)
}
swipe(recognizer:
){
print
"swipe ok"
)
var
point=recognizer.locationInView(
.view)
//这个点是滑动的起点
(point.x)
(point.y)
}
}
|
注意1:如果UISwipeGestureRecognizer在不指定方向的时候,默认向右滑动才会触发事件。如果要指定方向,需要设置direction属性
//表示监听滑动的方向为向上
Up
|
注意2:有网友问如果各个方向都要响应怎么办,只要多定义几个UISwipeGestureRecognizer就可以了
swipeUp =
swipeUp.direction =
.view.addGestureRecognizer(swipeUp)
swipeDown =
))
swipeDown.direction =
Down
.view.addGestureRecognizer(swipeDown)
}
){
if
recognizer.direction ==
Up
{
"向上滑动"
)
}
else
Down
{
"向下滑动"
)
}
.view)
//这个点是滑动的起点
(point.x)
(point.y)
}
@H_11_301@}
|
2,UITapGestureRecognizer:轻点手势(点击)
(1)可以通过numberOfTouchesrequired属性设置触摸点数,比如设置2表示必须两个手指触摸时才会触发
(2)通过numberOfTapsrequired属性设置点击次数,单击设置为1,双击设置为2
(3)如果一个控件既监听了单击事件也监听了双击事件,默认当双击事件触发的时候也同时会触发单击事件。如果想双击时不触发单击,需要通过requireGestureRecognizerToFail进行设置
//单击监听
tapSingle=
UITapGestureRecognizer
"tapSingleDid"
)
tapSingle.numberOfTapsrequired=1
tapSingle.numberOfTouchesrequired=1
//双击监听
tapDouble=
"tapDoubleDid:"
)
tapDouble.numberOfTapsrequired=2
tapDouble.numberOfTouchesrequired=1
//声明点击事件需要双击事件检测失败后才会执行
tapSingle.requireGestureRecognizerToFail(tapDouble);
.view.addGestureRecognizer(tapSingle)
.view.addGestureRecognizer(tapDouble)
}
tapSingleDid(){
"单击了"
)
}
tapDoubleDid(sender:
){
sender.view ==
@H_74_404@"双击了"
.view{
)
@H_11_301@}
}
}
|
rotation=
UIRotationGestureRecognizer
"rotationDid:"
)
.view.addGestureRecognizer(rotation)
}
rotationDid(recognizer:
){
//旋转的弧度转换为角度
(recognizer.rotation*(180/
CGFloat
(
M_PI
)))
}
|
@H_404_536@
23
rect:
UIView
!
viewDidLoad() {
.viewDidLoad()
rect=
(frame:
CGRectMake
(0,100,100))
rect.center=
.view.center
rect.backgroundColor=
UIColor
.redColor()
.view.addSubview(rect)
pan =
UIPanGestureRecognizer
"panDid:"
)
pan.maximumNumberOfTouches=1
rect.addGestureRecognizer(pan)
panDid(recognizer:
){
.view)
//设置矩形的位置
rect.center=point
}
|
6,UILongPressGestureRecognizer:长按