ios - 如何用动画结束 UILongPressGestureRecognizer?

标签 ios swift animation uiimageview uigesturerecognizer

我用 UILongPressGestureRecognizer 为 UIImageView 设置了动画,如下所示:

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

关于ios - 如何用动画结束 UILongPressGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32432803/

相关文章:

html - 关键帧动画不启动

CSS3 过渡在 Chrome 中不平滑

c++ - windows/osx/ipad 上的跨平台 GUI

swift - 如何使用 Swift 从 Apple Pay 获取电子邮件和电话号码

iphone - 如何在 iOS 中制作水平可滚动的 UITabBar?

swift - Alamofire 4 + 模型返回一个空值

ios - Swift - 显示另一个带有导航栏的 View Controller

jquery - 重新调整动画链中的过渡速度

ios - 彩色 UITextview 的自动更正问题

ios - 无法从 [UIActivityViewController excludedActivityTypes] 中排除 UIActivityTypePostToFacebook