swift - UIGestureRecognizer

import UIKit
class ViewController: UIViewController{
    
  
    var view1: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //向上滑動
        let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(swipe(_:)))
        upSwipe.direction = .up;
        self.view.addGestureRecognizer(upSwipe)
        //向下滑動
        let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(swipe(_:)))
        downSwipe.direction = .down;
        self.view.addGestureRecognizer(downSwipe)
        
        //邊緣滑動
        let edgeSwipe = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(edgeSwipe(_:)))
        edgeSwipe.edges = .left
        self.view.addGestureRecognizer(edgeSwipe)
        
        //單點手勢
        let tapSingle = UITapGestureRecognizer(target: self, action: #selector(tapSingleClick))
        tapSingle.numberOfTapsRequired = 1
        tapSingle.numberOfTouchesRequired = 1
        self.view.addGestureRecognizer(tapSingle)
        
        //雙擊手勢
        let tapDouble = UITapGestureRecognizer(target: self, action: #selector(tapDoubleClick))
        tapDouble.numberOfTapsRequired = 2
        self.view.addGestureRecognizer(tapDouble)
        
        //捏合手勢
        let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pichClick(_:)))
        self.view.addGestureRecognizer(pinch)
        
        //旋轉手勢
        let rotation = UIRotationGestureRecognizer(target: self, action: #selector(rotationClick(_:)))
        self.view.addGestureRecognizer(rotation)
        
        //拖動手勢
        view1 = UIView(frame: CGRect(x: 50, y: 50, width: 80, height: 80))
        view1.backgroundColor = UIColor.red
        self.view.addSubview(view1)
         //
        let pan = UIPanGestureRecognizer(target: self, action: #selector(panClick(_:)))
        view1.addGestureRecognizer(pan)
        
        //長按手勢
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressClick(_:)))
        longPress.minimumPressDuration = 2//長按啓動時間設置
        self.view.addGestureRecognizer(longPress)
    }
    //上下滑動
    @objc func swipe(_ recongizer:UISwipeGestureRecognizer){
       let point = recongizer.location(in: self.view)
        if recongizer.direction == .up{
           print("向上滑動point = \(point)")
        }else if recongizer.direction == .down{
           print("向下滑動point = \(point)")
        }
    }
    
    //邊緣滑動
    @objc func edgeSwipe(_ recongizer:UIScreenEdgePanGestureRecognizer){
        let point = recongizer.location(in: self.view)
        print("邊緣滑動point = \(point)")
    }
    
    //單擊
    @objc func tapSingleClick(){
        print("單擊了")
    }
    //雙擊
    @objc func tapDoubleClick(){
        print("雙擊了")
    }
    //捏合
    @objc func pichClick(_ recongnizer:UIPinchGestureRecognizer){
        print(recongnizer.scale)
        print(recongnizer.location(in: self.view))
    }
    //旋轉
    @objc func rotationClick(_ recongnizer:UIRotationGestureRecognizer){
        //旋轉的弧度裝換爲角度
       print(recongnizer.rotation*(180/CGFloat.pi))
    }
    //拖動手勢
    @objc func panClick(_ recongizer:UIPanGestureRecognizer){
        let point = recongizer.location(in: self.view)
        view1.center = point
    }
    //長按手勢
    @objc func longPressClick(_ recongnizer:UILongPressGestureRecognizer){
        if recongnizer.state == .began {
            print("開始點擊")
        }else{
            print("結束點擊")
        }
    }
}
相關文章
相關標籤/搜索