手勢(shì)識(shí)別在 iOS 中非常重要约素,他極大地提高了移動(dòng)設(shè)備的使用便捷性届良。iOS 系統(tǒng)在 3.2 以后笆凌,提供了一些常用的手勢(shì)(UIGestureRecognizer 的子類),今天給大家分享一下在swift中這些手勢(shì)的用法士葫。 (≧▽≦)/
使用手勢(shì)很簡(jiǎn)單乞而,分為三步:
- 創(chuàng)建手勢(shì)識(shí)別器對(duì)象實(shí)例整以。創(chuàng)建時(shí),指定一個(gè)回調(diào)方法绘闷,當(dāng)手勢(shì)開(kāi)始寇漫,改變、或結(jié)束時(shí)齿穗,執(zhí)行回調(diào)方法。
- 設(shè)置手勢(shì)識(shí)別器對(duì)象實(shí)例的相關(guān)屬性(可選部分)
- 添加到需要識(shí)別的 View 中。每個(gè)手勢(shì)只對(duì)應(yīng)一個(gè) View鞋喇,當(dāng)屏幕觸摸在 View 的邊界內(nèi)時(shí),如果手勢(shì)和預(yù)定的一樣眉撵,那就會(huì)執(zhí)行回調(diào)方法侦香。
一、UISwipeGestureRecognizer-滑動(dòng)手勢(shì)
let swip = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeGesture(_:)))
//設(shè)置滑動(dòng)方向
//如果UISwipeGestureRecognizer在不指定方向的時(shí)候纽疟,默認(rèn)向右滑動(dòng)才會(huì)觸發(fā)事件罐韩。如果要指定方向,需要設(shè)置direction屬性
swip.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swip)
那么有朋友可能會(huì)問(wèn)污朽,如果想要在各個(gè)方向都能滑動(dòng)要怎辦呢散吵,其實(shí)只要多定義幾個(gè)UISwipeGestureRecognizer就可以了
let swipeUp = UISwipeGestureRecognizer(target:self, action:#selector(swipeGesture(_:)))
swipeUp.direction = UISwipeGestureRecognizerDirection.Up
self.view.addGestureRecognizer(swipeUp)
let swipeDown = UISwipeGestureRecognizer(target:self, action:#selector(swipeGesture(_:)))
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)
滑動(dòng)響應(yīng)事件:
//滑動(dòng)手勢(shì)
func swipeGesture(swip:UISwipeGestureRecognizer) {
print("開(kāi)始滑動(dòng)")
if swip.direction == UISwipeGestureRecognizerDirection.Up {
print("向上滑動(dòng)")
}else if swip.direction == UISwipeGestureRecognizerDirection.Down {
print("向下滑動(dòng)")
}
}
Tip: 有時(shí)候我們會(huì)發(fā)現(xiàn)滑動(dòng)手勢(shì)不響應(yīng),那可能是因?yàn)槟慊瑒?dòng)的太短了哦
二蟆肆、UITapGestureRecognizer-點(diǎn)擊手勢(shì)
在這六種手勢(shì)識(shí)別中矾睦,只有一種手勢(shì)是離散型手勢(shì),就是 UITapGestureRecognizer炎功。離散型手勢(shì)的特點(diǎn)就是:一旦識(shí)別就無(wú)法取消枚冗,而且只會(huì)調(diào)用一次手勢(shì)操作事件(初始化手勢(shì)時(shí)指定的回調(diào)方法)。
?換句話說(shuō)其他五種手勢(shì)是連續(xù)型手勢(shì)蛇损,而連續(xù)型手勢(shì)的特點(diǎn)就是:會(huì)多次調(diào)用手勢(shì)操作事件赁温,而且在連續(xù)手勢(shì)識(shí)別后可以取消手勢(shì)。
let tapOne = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapOne))
//通過(guò)numberOfTouchesRequired屬性設(shè)置觸摸點(diǎn)數(shù)淤齐,比如設(shè)置2表示必須兩個(gè)手指觸摸時(shí)才會(huì)觸發(fā)
tapOne.numberOfTapsRequired = 1
//通過(guò)numberOfTapsRequired屬性設(shè)置點(diǎn)擊次數(shù)股囊,單擊設(shè)置為1,雙擊設(shè)置為2
tapOne.numberOfTouchesRequired = 1
//雙擊
let tapTwo = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapTwo))
tapTwo.numberOfTapsRequired = 2
tapTwo.numberOfTouchesRequired = 1
//聲明點(diǎn)擊事件需要雙擊事件檢測(cè)失敗后才會(huì)執(zhí)行
tapOne.requireGestureRecognizerToFail(tapTwo)
點(diǎn)擊響應(yīng)事件:
//單擊
func tapOne() {
print("單擊")
}
//雙擊
func tapTwo() {
print("雙擊")
}
三床玻、UIPinchGestureRecognizer-捏合手勢(shì)
捏合即進(jìn)行當(dāng)前圖片視圖縮放
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(ViewController.pinchDid(_:)))
//捏合手勢(shì)
func pinchDid(pinch:UIPinchGestureRecognizer) {
print(pinch.scale)//打印捏合比例
print(pinch.velocity)//打印捏合速度
}
四毁涉、UIRotationGestureRecognizer-旋轉(zhuǎn)手勢(shì)
旋轉(zhuǎn)即進(jìn)行當(dāng)前圖片視圖角度旋轉(zhuǎn)
let rotation = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.rotationDid(_:)))
//旋轉(zhuǎn)手勢(shì)
func rotationDid(rotation:UIRotationGestureRecognizer) {
print(rotation.rotation*(180/(CGFloat(M_PI))))//打印旋轉(zhuǎn)的角度
}
五、UIPanGestureRecognizer-拖動(dòng)手勢(shì)
拖動(dòng)即進(jìn)行當(dāng)前圖片視圖位置移動(dòng)
var rect: UIView?
override func viewDidLoad() {
super.viewDidLoad()
rect = UIView(frame: CGRectMake(0, 0, 100, 100))
rect?.center = self.view.center
rect?.backgroundColor = UIColor.cyanColor()
self.view.addSubview(rect!)
//拖動(dòng)手勢(shì)
let pan = UIPanGestureRecognizer(target: self, action: #selector(ViewController.panDid(_:)))
pan.maximumNumberOfTouches = 1 //一個(gè)手指拖動(dòng)
}
//拖動(dòng)手勢(shì)
func panDid(pan:UIPanGestureRecognizer) {
let point = pan.locationInView(self.view)
//設(shè)置矩形的位置
rect?.center = point
}
六锈死、UILongPressGestureRecognizer-長(zhǎng)按手勢(shì)
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(_:)))
longPress.numberOfTapsRequired = 0 //默認(rèn)為0
longPress.numberOfTouchesRequired = 1 //默認(rèn)為1
//長(zhǎng)按手勢(shì)
func longPress(longPress:UILongPressGestureRecognizer) {
if longPress.state == .Began {
print("長(zhǎng)按響應(yīng)開(kāi)始")
} else {
print("長(zhǎng)按響應(yīng)結(jié)束")
}
}
當(dāng)我們使用手勢(shì)的時(shí)候贫堰,我們可能會(huì)監(jiān)聽(tīng)屏幕的狀態(tài)穆壕,這個(gè)時(shí)候我們還會(huì)用到UITouch對(duì)象,重寫他的相關(guān)方法:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("開(kāi)始觸摸")
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("觸摸移動(dòng)")
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
print("取消觸摸")
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("觸摸結(jié)束")
}
好啦其屏,手勢(shì)在swift中的使用就分享到這里啦喇勋。( _ )/~~拜拜