平移手勢識別(UIPanGestureRecognizer)
以下創(chuàng)建手勢將會添加到gestureView上
//將view的背景顏色設(shè)置為白色
self.view.backgroundColor = UIColor.white
//創(chuàng)建一個UIView
let gestureView = UIView(frame: CGRect(x: 0, y: 100, width:
308, height: 308))
gestureView.backgroundColor = UIColor.green
self.view.addSubview(gestureView)
創(chuàng)建平移手勢
let pan = UIPanGestureRecognizer(target: self, action: #selector(panAction))
把平移手勢添加到gestureView上
gestureView.addGestureRecognizer(pan)
實現(xiàn)平移手勢關(guān)聯(lián)方法panAction
//MARK:- 平移手勢關(guān)聯(lián)方法
func panAction(sender:UIPanGestureRecognizer){
//1.獲取手勢在視圖上的平移增量
let point = sender.translation(in: sender.view)
//2.讓手勢所在的視圖對象做放射變換
sender.view?.transform = (sender.view?.transform.translatedBy(x: point.x, y: point.y))!
//3.將上一次的平移增量置為0
sender.setTranslation(CGPoint(x: 0.0, y: 0.0), in: sender.view)
}
Simulator Screen Shot 2016年11月23日 下午5.44.25.png
Simulator Screen Shot 2016年11月23日 下午5.57.46.png