swift -> 给任意控件添加 手势(点击,拖拽等) 事件

   

   1、拍击UITapGestureRecognizer (任意次数的拍击)  
    2、向里或向外捏UIPinchGestureRecognizer (用于缩放)  
    3、摇动或者拖拽UIPanGestureRecognizer  
    4、擦碰UISwipeGestureRecognizer (一般用以左右切换滑动)  
    5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)  
    6、长按UILongPressGestureRecognizer 

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

        //拖动事件, 任意方向
        let handDrag = UIPanGestureRecognizer(target: self, action: #selector(funDrag))
        //v.addGestureRecognizer(handDrag)
        
        //左右滑动,不可与拖动事件UIPanGestureRecognizer并存 , 默认只支持向右
        let handLeftRight = UISwipeGestureRecognizer(target: self, action: #selector(funLeftRight))
        handLeftRight.direction = .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);//现对于起始点的移动位置
        Point = sender.location(in: self.view);//在整个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()));
    }

  

如果想获取 参数 sender:UIxxxGestureRecognizer 中的视图 信息

    func showLongPress(sender:UILongPressGestureRecognizer){

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

    }

** 传递 和 获取 UIButton 信息

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

** 其中 UILongPressGestureRecogizer  长按事件详解

class xxx: aaa, UIGestureRecognizerDelegate{


    //让 添加 的 长按事件 与 控件本身 的长按事件 并存,不添加不会触发 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)
    }

}

猜你喜欢

转载自mft.iteye.com/blog/2377610