swift -> add gesture (click, drag, etc.) events to any control

   

   1. Tap UI Tap GestureRecognizer (any number of taps)  
    2. Pinch UI Pinch GestureRecognizer in or out (for zooming)  
    3. Shake or drag UI Pan GestureRecognizer  
    4. Swipe UI Swipe GestureRecognizer (usually used for Swipe left and right)  
    5. Rotate UI Rotation GestureRecognizer (move your finger in the opposite direction)  
    6. Long press UI LongPress GestureRecognizer 

 

    override func viewDidLoad() {
        super.viewDidLoad()
 
        var v = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight));
        
        //click event
        let handTap = UITapGestureRecognizer(target: self, action: #selector(funTap))
        v.addGestureRecognizer(handTap)

        //Drag event, any direction
        let handDrag = UIPanGestureRecognizer(target: self, action: #selector(funDrag))
        //v.addGestureRecognizer(handDrag)
        
        //Slide left and right, cannot coexist with drag event UIPanGestureRecognizer, only supports right by default
        let handLeftRight = UISwipeGestureRecognizer(target: self, action: #selector(funLeftRight))
        handLeftRight.direction = .left //support left
        v.addGestureRecognizer(handLeftRight)
        
        self.view.addSubview(v)

    }

    func funTap(sender: UIPanGestureRecognizer){
        print("funTap_"+String(arc4random()));
    }
    func funDrag(sender: UIPanGestureRecognizer){
        var Point = sender.translation(in: self.view);//Now for the moving position of the starting point
        Point = sender.location(in: self.view);//Location in the entire self.view
        print("funDrag_"+String(describing: Point.x)+","+String(describing:Point.y))

        if(sender.state == .began){
            print("begin: "+String(describing: Point.x)+","+String(describing:Point.y))
        }else if(sender.state == .ended){
            print("ended: "+String(describing: Point.x)+","+String(describing:Point.y))
        }else{
            print("ing: "+String(describing: Point.x)+","+String(describing:Point.y))
        }
    }
    func funLeftRight(sender: UIPanGestureRecognizer){
        print("funLeftRight_"+String(arc4random()));
    }

  

If you want to get the view information in the parameter sender:UIxxxGestureRecognizer

    func showLongPress(sender:UILongPressGestureRecognizer){

        print((sender.view as! XXX).url.text);

    }

 

** Pass and get UIButton information

func xxx(sender: UILongPressGestureRecognizer){
        let btn:UIButton = sender.view as! UIButton
        print (btn.tag)
}

 

 

 

** UILongPressGestureRecogizer long press event details

class xxx: aaa, UIGestureRecognizerDelegate{


    //Let the added long press event coexist with the long press event of the control itself, without adding the long press that will not trigger addGestureRecognizer
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true;
    }
    func longPressWeb(sender:UILongPressGestureRecognizer){
        if(sender.state == .began){
            print("long preses began")
        }else if(sender.state == .ended){
            print(" long press end")
        }
        print("you do long press");
        
    }


    override func viewDidLoad() {
        let long = UILongPressGestureRecognizer(target: self, action: #selector(longPressWeb));
        long.delegate = self
        self.view.addGestureRecognizer(long)
    }

}

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326217612&siteId=291194637