gpt4 book ai didi

ios - 在选择 UITableViewCell 时旋转图像

转载 作者:行者123 更新时间:2023-11-29 00:31:56 25 4
gpt4 key购买 nike

我正在尝试在选择 UITableViewCell 时为图像设置动画,该图像是 UITableViewCell 的子级,但在旋转后它会返回到默认状态,这是我下面的代码。

override func setSelected(_ selected: Bool, animated: Bool) {

if selected {
super.setSelected(selected, animated: animated)
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotationAnimation.fromValue = 0.0
rotationAnimation.toValue = (180.0 * CGFloat(M_PI)) / 180.0
rotationAnimation.duration = 0.45

self.updownImage.layer.add(rotationAnimation, forKey: nil)
}


// Configure the view for the selected state
}

最佳答案

您应该在添加动画之前立即将图层的变换设置为最终值。

CABasicAnimation 实际上并没有改变图层的属性,它只是设置图层的“表示层”以显示更改的外观。动画完成后,表示层将隐藏,该层将恢复为非动画状态。通过在添加动画之前将层设置为最终状态,当移除动画时,层将显示为最终状态。

编辑:

这是对您现有代码的一个非常简单的更改:

override func setSelected(_ selected: Bool, animated: Bool) {

if selected {
super.setSelected(selected, animated: animated)
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotationAnimation.fromValue = 0.0

//Put the angle in a constant so we can use to create a transform
let angle = (180.0 * CGFloat(M_PI)) / 180.0
rotationAnimation.toValue =
rotationAnimation.duration = 0.45

self.updownImage.layer.add(rotationAnimation, forKey: nil)

//Create a transform for our rotation
let transform = CATransform3DMakeRotation(angle, 0, 0, 1)

//install the transform on the layer so the change is active after
//the animation completes
layer.transform = transform
}
}

关于ios - 在选择 UITableViewCell 时旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41533645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com