在Swift程序中實現(xiàn)手勢識別的方法
在這次IOS應(yīng)用開發(fā)教程中,我們打算實現(xiàn)手勢識別。正如你所知道的,IOS支持大量的手勢操作,它們能提供了很好的應(yīng)用控制和出色用戶體驗。
讓我們開始吧!
首先需要在Xcode中創(chuàng)建一個新的Single View Application:
然后點擊Next,彈出的窗口要求你填寫項目設(shè)置。在第一欄 (“Product name”) 中填入項目名稱后,點擊Next.
確保語言選擇的是 “Swift”.
設(shè)計界面
點擊 “Main.storyboard” 文件,拖出6個 UIViews放到視圖中.把視圖排列成如圖所示的樣子.當(dāng)你排列UIViews時,在每個view下面添加一個UILabel并依圖設(shè)定文本值。
我們開始寫代碼吧.
是時候編輯實現(xiàn)文件了 (在我們的案例 “ViewController.swift” ).
為了聲明一些我們將會用到的變量,要在 “class ViewController: UIViewController “塊中添加如下代碼.
@IBOutlet var tapView: UIView
@IBOutlet var swipeView: UIView
@IBOutlet var longPressView: UIView
@IBOutlet var pinchView: UIView
@IBOutlet var rotateView: UIView
@IBOutlet var panView: UIView
var lastRotation = CGFloat()
let tapRec = UITapGestureRecognizer()
let pinchRec = UIPinchGestureRecognizer()
let swipeRec = UISwipeGestureRecognizer()
let longPressRec = UILongPressGestureRecognizer()
let rotateRec = UIRotationGestureRecognizer()
let panRec = UIPanGestureRecognizer()
}
在第2 – 7行,我們聲明了在之前界面里排列過的 UIViews.
在第8行,我們聲明了實現(xiàn)旋轉(zhuǎn)手勢要用到的變量(lastRotation).
在第 9 – 14行,我們?yōu)槊總€view聲明了一個手勢識別對象.
注意: 在 Swift中,我們用let關(guān)鍵字聲明常量,這意味著它的值在程序運行時不可改變。關(guān)鍵字var則聲明普通變量。
當(dāng)聲明完應(yīng)用需要的主要變量后,在viewDidLoad 方法中添加如下代碼.
super.viewDidLoad()
tapRec.addTarget(self, action: "tappedView")
pinchRec.addTarget(self, action: "pinchedView:")
swipeRec.addTarget(self, action: "swipedView")
longPressRec.addTarget(self, action: "longPressedView")
rotateRec.addTarget(self, action: "rotatedView:")
panRec.addTarget(self, action: "draggedView:")
tapView.addGestureRecognizer(tapRec)
swipeView.addGestureRecognizer(swipeRec)
pinchView.addGestureRecognizer(pinchRec)
longPressView.addGestureRecognizer(longPressRec)
rotateView.addGestureRecognizer(rotateRec)
panView.addGestureRecognizer(panRec)
rotateView.userInteractionEnabled = true
rotateView.multipleTouchEnabled = true
pinchView.userInteractionEnabled = true
pinchView.multipleTouchEnabled = true
tapView.userInteractionEnabled = true
swipeView.userInteractionEnabled = true
longPressView.userInteractionEnabled = true
panView.userInteractionEnabled = true
}
第 3 – 8行,為每個視圖設(shè)定手勢識別的目標(biāo)。所謂的目標(biāo),就是每個view中的手勢完成后要調(diào)用的方法。
第 9 -14行,把手勢識別添加到視圖中.
第15 – 22行,把每個視圖的 userInteractionEnabled 屬性設(shè)為ture,并把擁有需要多點觸控(rotateView and pinchView)的手勢所在的視圖的multipleTouchEnabled 屬性設(shè)為true.
現(xiàn)在,我們編寫每個手勢識別器要調(diào)用的方法 (第3 – 8行設(shè)置的目標(biāo)方法 ).
添加如下代碼:
let tapAlert = UIAlertController(title: "Tapped", message: "You just tapped the tap view", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
}
func swipedView(){
let tapAlert = UIAlertController(title: "Swiped", message: "You just swiped the swipe view", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
}
func longPressedView(){
let tapAlert = UIAlertController(title: "Long Pressed", message: "You just long pressed the long press view", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
}
這三種方法都很好地完成同一件事.每次在手勢在相應(yīng)的視圖中完成后,每種方法都彈出一個對話框.
所以 tappedView() 方法在用戶滑動視圖時彈出一個對話框,swipedView() 方法在用戶觸摸滑動 swipe視圖時彈出對話框,而longPressedView() 方法則在用戶長按long press view時彈出對話框.
另兩種手勢 (rotate and pinch ) 的代碼稍微有點復(fù)雜.
為旋轉(zhuǎn)手勢添加如下代碼:
var lastRotation = CGFloat()
self.view.bringSubviewToFront(rotateView)
if(sender.state == UIGestureRecognizerState.Ended){
lastRotation = 0.0;
}
rotation = 0.0 - (lastRotation - sender.rotation)
var point = rotateRec.locationInView(rotateView)
var currentTrans = sender.view.transform
var newTrans = CGAffineTransformRotate(currentTrans, rotation)
sender.view.transform = newTrans
lastRotation = sender.rotation
}
這個方法包含 sender:UIRotationGestureRecognizer 參數(shù). sender 參數(shù)( UIRotationGestureRecognizer 類型) 含有這個方法(在這個案例中是rotateRec)調(diào)用的手勢識別器的值.
第2行聲明了 lastRotation.
第3行我們把 rotateView放到前面.
接下來,在 if語句中,我們檢查手勢是否完成,如果沒有完成,我們就將視圖旋轉(zhuǎn)。
第 8 – 10行,我們計算rotate view的旋轉(zhuǎn)程度,第10行,我們設(shè)置rotate view的旋轉(zhuǎn)程度。
On line 12 we set the lastRotation 作為旋轉(zhuǎn)手勢識別器的當(dāng)前旋轉(zhuǎn).
現(xiàn)在我們添加pinch 手勢的代碼:
self.view.bringSubviewToFront(pinchView)
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale)
sender.scale = 1.0
}
在之前方法的第1行中,我們把pinch視圖放到了頂端。然后設(shè)置每個pinch視圖的transform,并把pinchRec的scale設(shè)為1.
然后是實現(xiàn) pan (drag) 手勢. 添加如下代碼:
self.view.bringSubviewToFront(sender.view)
var translation = sender.translationInView(self.view)
sender.view.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y)
sender.setTranslation(CGPointZero, inView: self.view)
}
第2行,我們把 drag視圖放到頂端 (和前面的方法一樣).
然后我們聲明變量translation,并用 sender.translationInView(self.view)的值給它賦值。 完成后,把sender.view object (panRec) 的center屬性設(shè)為計算出來的新center ( 通過CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y) 計算) 并把translation 設(shè)為 sender (panRec).
現(xiàn)在,代碼部分算是完成了!
回到界面設(shè)計.
現(xiàn)在我們回到 “Main.storyboard” 文件. 選擇視圖控制器并把聲明的每個UIView連接到相應(yīng)的視圖,如下圖所示.
完工
現(xiàn)在你可以在模擬器或你的設(shè)備上運行該應(yīng)用并測試手勢。
后記
我希望這篇教程對你有所幫助。你可以在下載完整源代碼
相關(guān)文章
Swift利用AFN實現(xiàn)封裝網(wǎng)絡(luò)請求詳解
網(wǎng)絡(luò)請求工具是我們經(jīng)常用到的工具類,所以下面這篇文章主要給大家介紹了關(guān)于Swift利用AFN如何實現(xiàn)封裝網(wǎng)絡(luò)請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10Swift踩坑實戰(zhàn)之一個字符引發(fā)的Crash
swift通常都是通過對應(yīng)的signal來捕獲crash,下面這篇文章主要給大家介紹了關(guān)于Swift踩坑實戰(zhàn)之一個字符引發(fā)的Crash的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02Swift使用transform 實現(xiàn)重復(fù)平移動畫效果
這篇文章主要介紹了Swift使用transform 實現(xiàn)重復(fù)平移動畫效果,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07