简体   繁体   English

如何用动画结束UILongPressGestureRecognizer?

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

I've animated a UIImageView with a UILongPressGestureRecognizer like so: 我使用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")
    }

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? 动画只会导致UIImageview的高度和宽度在longPress上展开,但是当释放按下时,边界会继续增长。当释放按下时,如何将UIImageView的边界返回到原点宽度和高度?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Swift 中将 UILongPressGestureRecognizer 与 UICollectionViewCell 一起使用? - How do I use UILongPressGestureRecognizer with a UICollectionViewCell in Swift? 在UILongPressGestureRecognizer上,如何检测生成事件的对象? - On a UILongPressGestureRecognizer how do I detect which object generated the event? 如何启用一次触摸即可处理UILongPressGestureRecognizer和UIPanGestureRecognizer? - How do I enable a single touch to handle both UILongPressGestureRecognizer and UIPanGestureRecognizer? 一次由UILongPressGestureRecognizer触发拖动时,如何防止UIView“跳动”? - How do I keep my UIView from “jumping” when dragging it once triggered by UILongPressGestureRecognizer? 如何在Core Animation中平滑更新动画的toValue /终点? - How do I smoothly update the toValue/end point of an animation in Core Animation? UILongPressGestureRecognizer 不做任何事情 - UILongPressGestureRecognizer does not do anything 创建动画以使UIImageView无限向上和向下(几乎浮动)时,如何在动画结束时停止暂停? - When creating an animation to cause a UIImageView to go up and down (almost float) infinitely, how do I stop a pause at the end of the animation? 如何使UIPanGestureRecognizer的UILongPressGestureRecognizer失败? - how to fail UILongPressGestureRecognizer for UIPanGestureRecognizer? 如何将UILongPressGestureRecognizer添加到UITextField? - How to add a UILongPressGestureRecognizer to a UITextField? UILongPressGestureRecognizer的工作方式 - how UILongPressGestureRecognizer works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM