【问题标题】:stop animation doesn't work with image view停止动画不适用于图像视图
【发布时间】:2026-02-08 20:05:01
【问题描述】:

我正在尝试停止一个动画,一个矩形(由代码创建)并且它适用于此代码,但是当使用相同的 removeAllAnimationimageView(替换由代码创建的矩形)时,它不会没用……

----更正---

它工作错误,停止动画,但图像视图进入我将它放在情节提要中的位置,而不是我点击停止按钮时的位置......

@IBAction func ButtonStop(sender: UIButton) {
        var layer = movementBar.layer.presentationLayer() as CALayer
        var frame = layer.frame

        movementBar.layer.removeAllAnimations()
        movementBar.frame = frame
}

movementBar 是一个imageView,在我通过代码创建一个带有 cgrect (x, y, w, h) 的矩形之前,填充了颜色,带有柔和的边缘 感谢您的帮助!

这里是制作动画的代码,有 2 个函数可以向下移动,然后向上移动,按下一个按钮移动栏停止,我读取位置:

func movementUp() {
    UIView.animateWithDuration(self.speed, animations: {
          self.movementBar.frame = CGRect (x:0, y:self.yUp, width:self.barWidth, height:self.barHeight)
        }, completion: { animationFinished in
            if animationFinished {
                self.movementDown()
            }
    })
}

【问题讨论】:

    标签: image animation swift view freeze


    【解决方案1】:

    我也做过同样的事情请查看演示项目
    Download from here
    请让我知道你的实现和我的有什么不同。

    【讨论】:

    • 真的我不知道你的项目为什么能工作,我没有......我尝试从头开始重写代码,也许从一开始就出错了......谢谢你的帮助!跨度>
    • WC :) 祝你好运,我在 Objective-c 和 swift 中都尝试了工作......并在 UIImageView 中添加了约束以产生问题,但它工作正常。