簡體   English   中英

如何用動畫結束UILongPressGestureRecognizer?

[英]How do I end UILongPressGestureRecognizer with an animation?

我使用UILongPressGestureRecognizer為UIImageView設置了動畫, UILongPressGestureRecognizer所示:

override func viewDidAppear(animated: Bool) {

        var longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPress"))
        imageView.addGestureRecognizer(longPress)
        imageView.userInteractionEnabled = true
    }

    func longPress()
    {
        let bounds = self.imageView.bounds
        UIView.animateWithDuration(0.5, animations: {
            self.imageView.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 22, height: bounds.size.height + 22)
        })

        println("user pressed on image")
    }

動畫只會導致UIImageview的高度和寬度在longPress上展開,但是當釋放按下時,邊界會繼續增長。當釋放按下時,如何將UIImageView的邊界返回到原點寬度和高度?

嘗試這個:

var oldbounds:CGRect!
@IBOutlet var image: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func viewDidAppear(animated: Bool) {

    var longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPress:"))
    longPress.minimumPressDuration = 0.01
    image.addGestureRecognizer(longPress)
    image.userInteractionEnabled = true
}

func longPress(gesture:UILongPressGestureRecognizer)
{
    if gesture.state == UIGestureRecognizerState.Began
    {
        oldbounds = self.image.bounds
    }
    else if gesture.state == UIGestureRecognizerState.Changed
    {
        let bounds = self.image.bounds
        UIView.animateWithDuration(0.5, animations: {
            self.image.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 22, height: bounds.size.height + 22)
        })

        println("user pressed on image")
    }
    else
    {
        let bounds = self.image.bounds
        UIView.animateWithDuration(0.5, animations: {
            self.image.bounds = self.oldbounds
        })

        println("user release on image")
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM