Swift - UIGestureRecognizer

import UIKit
class ViewController: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
        //SwipeGestureRecognizer:滑動手勢
        let swipe = UISwipeGestureRecognizer(target: self, action: #selector(swipeUp(_ :)))
        //設置監聽滑動的方向爲向上
        swipe.direction = .up
        //添加到視圖中
        self.view.addGestureRecognizer(swipe)
        
        //ScreenEdgePanGestureRecognizer:邊緣滑動(是UISwipeGestureRecognizer的子類)
        let edgeSwipe = UIScreenEdgePanGestureRecognizer(target: self, action:#selector(ViewController.edgeSwipe))
        //設置監聽滑動的方向
            edgeSwipe.edges = .left//從左邊緣往右滑
        self.view.addGestureRecognizer(edgeSwipe)
        
        //UITapGestureRecognizer:輕擊手勢
        //單擊監聽
        let tapSingle=UITapGestureRecognizer(target:self,action:#selector(Tap(_:)))
        tapSingle.numberOfTapsRequired = 1//連續點擊次數
        tapSingle.numberOfTouchesRequired = 1//同時按下次數
        //雙擊監聽
        let tapDouble=UITapGestureRecognizer(target:self,action:#selector(Tap(_:)))
        tapDouble.numberOfTapsRequired = 2
        tapDouble.numberOfTouchesRequired = 1
        //若是一個空間同時有單擊,雙擊爽個事件要想點擊雙擊不觸發單擊事件,使用如下方法
        tapSingle.require(toFail: tapDouble)
        //開啓用戶交互(如VIew使用該手勢須要更換畫面,需開啓交互)
        self.view.isUserInteractionEnabled = true
        self.view.addGestureRecognizer(tapSingle)
        self.view.addGestureRecognizer(tapDouble)
        
        //UIPinchGesturnRecognizer:捏合手勢
        let pinch = UIPinchGestureRecognizer(target: self, action: #selector(Pinch(_:)))
        self.view.addGestureRecognizer(pinch)
        
        //UIRotationGestrueRecognizer:旋轉手勢
        let rotation = UIRotationGestureRecognizer(target: self, action: #selector(Rotation(_:)))
        self.view.addGestureRecognizer(rotation)
        
        //UIPanGestureRecognizer:拖動手勢
        let View = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        View.backgroundColor = UIColor.black
        View.center = self.view.center
        View.tag = 1
        self.view.addSubview(View)
        
        let pan = UIPanGestureRecognizer(target: self, action: #selector(Pan(_:)))
        View.addGestureRecognizer(pan)
        
        //UILongPressGestureRecognzier:長按
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(LongPress(_:)))
        //設置長按手勢按下多少時長才被監聽
        longPress.minimumPressDuration = 1.0
        //取消長按手勢
        longPress.allowableMovement = 20.0//受災在按下狀態下距離超過20視爲非長按手勢。此屬性默認值爲10
        self.view.addGestureRecognizer(longPress)
    }
    //響應監聽事件
    @objc func swipeUp(_ recognizer:UISwipeGestureRecognizer){
        let point = recognizer.location(in: self.view)
        print(point.x)
        print(point.y)
    }
    
    @objc func edgeSwipe()
    {
        print("使用了邊緣滑動手勢")
    }
    
    @objc func Tap(_ cognizer:UITapGestureRecognizer)
    {
        let Tapnumber = cognizer.numberOfTapsRequired
        print("你點擊了:\(Tapnumber)")
    }
    
    @objc func Pinch(_ recognizer:UIPinchGestureRecognizer){
        print("捏合比例爲:\(recognizer.scale)")
    }
    
    @objc func Rotation(_ recognizer:UIRotationGestureRecognizer){
        print("旋轉角度爲:\(recognizer.rotation*(180/CGFloat.pi))")
    }
    
    @objc func Pan(_ recognizer:UIPanGestureRecognizer){
        let point = recognizer.location(in: self.view)
        let View = self.view.viewWithTag(1)
        View?.center = point
        //也能夠用recognizer.view獲取View對象
        recognizer.view?.center = point
    }
    
    @objc func LongPress(_ sender:UILongPressGestureRecognizer){
        if sender.state == .began{
            print("長按開始")
        }else{
            print("長按結束")
        }
    }

}
相關文章
相關標籤/搜索