IOS 開發(fā)之swift中手勢的實(shí)例詳解
IOS 開發(fā)之swift中手勢的實(shí)例詳解
手勢操作主要包括如下幾類
| 手勢 | 屬性 | 說明 |
|---|---|---|
| 點(diǎn)擊 UITapGestureRecognizer | numberOfTapsRequired:點(diǎn)擊的次數(shù);numberOfTouchesRequired:點(diǎn)擊時(shí)有手指數(shù)量 | 設(shè)置屬性 numberOfTapsRequired 可以實(shí)現(xiàn)單擊,或雙擊的效果 |
| 滑動(dòng) UISwipeGestureRecognizer | direction:滑動(dòng)方向 | direction 滑動(dòng)方向分為上Up、下Down、左Left、右Right |
| 拖動(dòng) UIPanGestureRecognizer | 在拖動(dòng)過程中,通過方法 translationInView 獲取拖動(dòng)時(shí)的位移 | |
| 長按 UILongPressGestureRecognizer | minimumPressDuration:長按最少時(shí)間 | |
| 旋轉(zhuǎn) UIRotationGestureRecognizer | ||
| 縮放 UIPinchGestureRecognizer |
注意:手勢效果在實(shí)施過程中,存在幾種狀態(tài):
* Began
* Ended
* Cancelled
* Failed
* Possible
手勢效果圖

代碼示例
// 點(diǎn)擊
let label = UILabel(frame: CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-單指單擊手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("tapClick:"))
tapRecognizer.numberOfTapsRequired = 1
tapRecognizer.numberOfTouchesRequired = 1
label.userInteractionEnabled = true
label.addGestureRecognizer(tapRecognizer)
// 手勢響應(yīng)方法
func tapClick(recognizer:UITapGestureRecognizer)
{
let label:UILabel = recognizer.view as! UILabel
label.textColor = UIColor.redColor()
}
// 滑動(dòng)
let label = UILabel(frame: CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-左滑手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipeLeftClick:"))
swipeLeftRecognizer.direction = .Left
swipeLeftRecognizer.numberOfTouchesRequired = 1
label.userInteractionEnabled = true
label.addGestureRecognizer(swipeLeftRecognizer)
// 手勢響應(yīng)方法
func swipeLeftClick(recognizer:UISwipeGestureRecognizer)
{
let label = recognizer.view
label!.backgroundColor = UIColor.orangeColor()
}
// 拖動(dòng)
let label = UILabel(frame: CGRectMake(10.0, 10.0, 100.0, 100.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-拖動(dòng)手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let panRecognizer = UIPanGestureRecognizer(target: self, action: Selector("panClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(panRecognizer)
var pointValue:CGPoint! = CGPointZero // 移動(dòng)
// 手勢響應(yīng)方法
func panClick(recognizer:UIPanGestureRecognizer)
{
let label:UILabel = recognizer.view as! UILabel
let point = recognizer.translationInView(label)
print("pan point = \(point)")
// 移動(dòng)
label.transform = CGAffineTransformMakeTranslation(point.x + self.pointValue.x, point.y + self.pointValue.y)
if recognizer.state == .Began
{
label.backgroundColor = UIColor.yellowColor()
self.view.bringSubviewToFront(label)
}
else if recognizer.state == .Ended
{
label.backgroundColor = UIColor.lightGrayColor()
self.pointValue.x += point.x
self.pointValue.y += point.y
}
}
// 縮放
let label = UILabel(frame: CGRectMake(10.0, 10.0, 200.0, 200.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-捏合手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action: Selector("pinchClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(pinchRecognizer)
// 手勢響應(yīng)方法
var scaleValue:CGFloat! = 1.0 // 縮放
func pinchClick(recognizer:UIPinchGestureRecognizer)
{
let label:UILabel = recognizer.view as! UILabel
let scale = recognizer.scale;
if scale > 1.0
{
// 放大
label.transform = CGAffineTransformMakeScale(self.scaleValue + scale - 1.0, self.scaleValue + scale - 1.0)
}
else
{
// 縮小
label.transform = CGAffineTransformMakeScale(self.scaleValue * scale, self.scaleValue * scale)
}
if recognizer.state == .Began
{
label.backgroundColor = UIColor.greenColor()
self.view.bringSubviewToFront(label)
}
else if recognizer.state == .Ended
{
label.backgroundColor = UIColor.lightGrayColor()
if scale > 1.0
{
self.scaleValue = self.scaleValue + scale - 1.0;
}
else
{
self.scaleValue = self.scaleValue * scale
}
}
}
// 旋轉(zhuǎn)
let label = UILabel(frame: CGRectMake(10.0, 10.0, 200.0, 200.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-旋轉(zhuǎn)手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let rotationRecognizer = UIRotationGestureRecognizer(target: self, action: Selector("ratotionClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(rotationRecognizer)
var rotationValue:CGFloat! = 1.0 // 旋轉(zhuǎn)
// 手勢響應(yīng)方法
func ratotionClick(recognizer:UIRotationGestureRecognizer)
{
let label:UILabel = recognizer.view as! UILabel
let rotation = recognizer.rotation
label.transform = CGAffineTransformMakeRotation(rotation + self.rotationValue)
if recognizer.state == .Began
{
label.backgroundColor = UIColor.greenColor()
self.view.bringSubviewToFront(label)
}
else if recognizer.state == .Ended
{
label.backgroundColor = UIColor.lightGrayColor()
self.rotationValue = self.rotationValue + rotation
}
}
// 長按
let label = UILabel(frame: CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手勢操作-長按手勢";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手勢
let pressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("pressClick:"))
pressRecognizer.minimumPressDuration = 3.0
label.userInteractionEnabled = true
label.addGestureRecognizer(pressRecognizer)
// 手勢響應(yīng)方法
func pressClick(recognizer:UILongPressGestureRecognizer)
{
let label:UILabel = recognizer.view as! UILabel
if recognizer.state == .Began
{
let alertView = UIAlertView(title: nil, message: "長按響應(yīng)", delegate: nil, cancelButtonTitle: "知道了")
alertView.show()
label.backgroundColor = UIColor.orangeColor()
self.view.bringSubviewToFront(label)
}
else if recognizer.state == .Ended
{
label.backgroundColor = UIColor.lightGrayColor()
}
}
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
iOS復(fù)數(shù)cell下優(yōu)雅的代碼結(jié)構(gòu)詳解
這篇文章主要給大家介紹了關(guān)于iOS復(fù)數(shù)cell下優(yōu)雅的代碼結(jié)構(gòu)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
iOS基礎(chǔ)知識(shí)之@property 和 Ivar 的區(qū)別
這篇文章主要介紹了iOS基礎(chǔ)知識(shí)之@property 和 Ivar 的區(qū)別介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
iOS當(dāng)多個(gè)網(wǎng)絡(luò)請(qǐng)求完成后執(zhí)行下一步的方法詳解
在多線程中,有時(shí)候我們會(huì)遇到一個(gè)界面同時(shí)有多個(gè)網(wǎng)絡(luò)請(qǐng)求(比如a,b,c,d四個(gè)網(wǎng)絡(luò)請(qǐng)求),在這四個(gè)個(gè)請(qǐng)求結(jié)束后,在請(qǐng)求到數(shù)據(jù)去做其他操作(UI更新等),下面這篇文章主要給大家介紹了關(guān)于iOS當(dāng)多個(gè)網(wǎng)絡(luò)請(qǐng)求完成后執(zhí)行下一步的相關(guān)資料,需要的朋友可以參考下。2017-12-12
iOS判斷運(yùn)營商類型的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猧OS判斷運(yùn)營商類型的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
iOS實(shí)現(xiàn)APP程序內(nèi)部打開APP的AppStore頁面
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)APP程序內(nèi)部打開APP的AppStore頁面的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來看看吧。2017-06-06
iOS Moya實(shí)現(xiàn)OAuth請(qǐng)求的方法
這篇文章主要介紹了iOS Moya實(shí)現(xiàn)OAuth請(qǐng)求的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
舉例講解設(shè)計(jì)模式中的原型模式在iOS應(yīng)用開發(fā)中的作用
這篇文章主要介紹了設(shè)計(jì)模式中的原型模式在iOS應(yīng)用開發(fā)中的作用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04
ios12中遇到的帶input彈窗的錯(cuò)位問題的解決方法
這篇文章主要介紹了ios12中遇到的帶input彈窗的錯(cuò)位問題的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
iOS自定義View實(shí)現(xiàn)卡片滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了ios自定義View實(shí)現(xiàn)卡片滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02

