gpt4 book ai didi

ios - 中断 UIView 转换

转载 作者:可可西里 更新时间:2023-11-01 01:08:10 24 4
gpt4 key购买 nike

是否有可能在 iOS 的当前状态下中断 UIView.transition

为您提供一些背景信息:我有一个 UIButton,我需要在其中设置标题颜色的动画 - 不过,动画可以附加不同的延迟。由于 UIView.transition 不支持延迟,我只是延迟了整个 block 的执行:

DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
UIView.transition(with: button, duration: 0.4, options: [.beginFromCurrentState, .allowUserInteraction, .transitionCrossDissolve], animations: {
button.setTitleColor(selected ? .red : .black, for: .normal)
}, completion: nil)
}

这里的问题是,如果此代码以不同的延迟快速连续执行,结果可能会出乎意料。因此,例如,可以使用 selected=true, delay=0.5 调用,然后立即使用 selected=false, delay=0.0 调用。在这种情况下,按钮最终会变成红色,尽管它应该是黑色的。

因此,我正在寻找一种方法来延迟 UIView.transform,中断 UIView.transform 或制作 setTitleColor() 可通过 UIView.animate 设置动画。

最佳答案

我实际上创建了一个框架来专门做这个,你可以触发相对于时间进度或值插值进度的动画。框架和文档可以在这里找到:

https://github.com/AntonTheDev/FlightAnimator

以下示例假定您有一个 CATextLayer 支持的 View ,因为这不是关于如何设置它的问题,这里是一个帮助您入门的小示例:

class CircleProgressView: UIView {
.....

override class func layerClass() -> AnyClass {
return CircleProgressLayer.self
}
}

示例 1:

假设您有一个 CATextLayer 支持的 View ,我们可以按如下方式为其前景色设置动画,并触发另一个与时间相关的动画:

var midPointColor = UIColor.orange.cgColor
var finalColor = UIColor.orange.cgColor
var keyPath = "foregroundColor"

textView.animate { [unowned self] (a) in
a.value(midPointColor, forKeyPath : keyPath).duration(1.0).easing(.OutCubic)

// This will trigger the interruption
// at 0.5 seconds into the animation

animator.triggerOnProgress(0.5, onView: self.textView, animator: { (a) in
a.value(finalColor, forKeyPath : keyPath).duration(1.0).easing(.OutCubic)
})
}

示例 2:

就像在前面的例子中一样,一旦你有一个 CATextLayer 支持的 View ,我们就可以为它的 foregroundColor 设置动画,相对于 RBG 值的进度矩阵,根据 3 个值的幅度进度计算:

var midPointColor = UIColor.orange.cgColor
var finalColor = UIColor.orange.cgColor
var keyPath = "foregroundColor"

textView.animate { [unowned self] (a) in
a.value(midPointColor, forKeyPath : keyPath).duration(1.0).easing(.OutCubic)

// This will trigger the interruption
// when the magnitude of the starting point
// is equal to 0.5 relative to the final point

a.triggerOnValueProgress(0.5, onView: self.textView, animator: { (a) in
a.value(finalColor, forKeyPath : keyPath).duration(1.0).easing(.OutCubic)
})
}

希望这有帮助:)

关于ios - 中断 UIView 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53007191/

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