imzyf / ios-swift-learning-notes

📝 iOS Swift Learning Notes - see Issues
MIT License
0 stars 0 forks source link

手势 - Gesture Recognizer #4

Open imzyf opened 7 years ago

imzyf commented 7 years ago
        // 单击
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissClose))
        tapGesture.require(toFail: doubleTapGesture)
imzyf commented 6 years ago

双击

        // 设置双击手势
        let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(doubleClick))
        doubleTapGesture.numberOfTapsRequired = 2
        imageView.addGestureRecognizer(doubleTapGesture)
        imageView.isUserInteractionEnabled = true

解决与单击的冲突

imageView.addGestureRecognizer(tapGesture)
imzyf commented 6 years ago

捏合手势

UIPinchGestureRecognizer

旋转手势

UIRotationGestureRecognizer

拖手势

UIPanGestureRecognizer

划动手势

UISwipeGestureRecognizer swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left //不设置是右

长按手势

var longpressGesutre = UILongPressGestureRecognizer(target: self, action: "handleLongpressGesture:")  
        //长按时间为1秒  
        longpressGesutre.minimumPressDuration = 1  
        //允许15秒运动  
        longpressGesutre.allowableMovement = 15  
        //所需触摸1次  
        longpressGesutre.numberOfTouchesRequired = 1  
imzyf commented 6 years ago

https://stackoverflow.com/questions/38445262/pass-parameter-with-uitapgesturerecognizer

传递参数

class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var image: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        image.userInteractionEnabled = true;
        let tappy = MyTapGesture(target: self, action: #selector(self.tapped(_:)))
        image.addGestureRecognizer(tappy)
        tappy.title = "val"
    }

    func tapped(sender : MyTapGesture) {
        print(sender.title)
        label1.text = sender.title
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

class MyTapGesture: UITapGestureRecognizer {
    var title = String()
}
imzyf commented 6 years ago

https://stackoverflow.com/questions/38445262/pass-parameter-with-uitapgesturerecognizer

传递参数

class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var image: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        image.userInteractionEnabled = true;
        let tappy = MyTapGesture(target: self, action: #selector(self.tapped(_:)))
        image.addGestureRecognizer(tappy)
        tappy.title = "val"
    }

    func tapped(sender : MyTapGesture) {
        print(sender.title)
        label1.text = sender.title
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

class MyTapGesture: UITapGestureRecognizer {
    var title = String()
}