简体   繁体   中英

How do I end UILongPressGestureRecognizer with an animation?

I've animated a UIImageView with a UILongPressGestureRecognizer like so:

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")
    }

the animation just causes the UIImageview's height and width to expand on the longPress, however when the press is released, the bounds continue to grow.. How do I return the bounds of the UIImageView to the origin width and height when the press is released?

Try this:

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")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM