这是我的代码:
UIView.animate(withDuration: 2.5, animations: {
animatingScoreCircle.alpha = 0.0
let screenSize = UIScreen.main.bounds
let screenHeight = screenSize.height * 0.10
animatingScoreCircle.center.y += screenHeight
})
我想要的是,图像视图将从原始center.y位置向上移动,占当前屏幕大小的10%。但现在,它从最终位置(原始中心+屏幕高度的10%)到我想要的图像,到原始中心y。所以它是逆转的。所以它应该是:
步骤1:图像应处于正常状态,即center.y 第2步:图像应该慢慢进入最终状态,即center.y + 10
但现在好了:
步骤1:图像从center.y + 10开始
第2步:图像缓慢移动到原始中心
如果我将+=
更改为-=
,它会执行相同的操作,但现在图像从下方传到原始center.y位置。
我已经尝试过了:
let originalCenterYForScoreCircle = animatingScoreCircle.center.y
let screenSize = UIScreen.main.bounds
let screenHeight = screenSize.height * 0.10
animatingScoreCircle.center.y = screenHeight
UIView.animate(withDuration: 2.5, animations: {
animatingScoreCircle.alpha = 0.0
animatingScoreCircle.center.y = originalCenterYForScoreCircle
})
但它有相同的结果。如何修复此代码?谢谢。
编辑:我正在使用自动布局,我正在制作纸牌游戏。当用户点击UIButton时,我想使用动画使其看起来更好。所以这就是我打电话的动画。当用户点击按钮时,将触发此动画。该功能自动调整该按钮的约束。之后,我想使用上面描述的动画。这是更多代码:
let animatingScoreCircle = UIImageView()
animatingScoreCircle.image = UIImage(named: "emptyPicture")
animatingScoreCircle.layer.cornerRadius = animatingScoreCircle.frame.size.width / 2
animatingScoreCircle.clipsToBounds = true
animatingScoreCircle.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animatingScoreCircle)
let horizontalConstraint = animatingScoreCircle.centerXAnchor.constraint(equalTo: sender.centerXAnchor)
let verticalConstraint = animatingScoreCircle.centerYAnchor.constraint(equalTo: sender.centerYAnchor)
let heigthContraint = animatingScoreCircle.heightAnchor.constraint(equalTo: sender.heightAnchor, multiplier: 1.0)
let widthConstraint = animatingScoreCircle.widthAnchor.constraint(equalTo: sender.widthAnchor, multiplier: 1.0)
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, heigthContraint, widthConstraint])
let originalCenterYForScoreCircle = animatingScoreCircle.center.y
let screenSize = UIScreen.main.bounds
let screenHeight = screenSize.height * 0.10
animatingScoreCircle.center.y = screenHeight
UIView.animate(withDuration: 2.5, animations: {
animatingScoreCircle.alpha = 0.0
animatingScoreCircle.center.y = originalCenterYForScoreCircle
})
}
当用户点击按钮时会调用此方法。