在单个Uiview.transition块中为Uilabel和Uibutton动画吗?

发布于 2025-01-28 20:38:16 字数 574 浏览 2 评论 0 原文

我需要动画化Uilabel和Uibutton的图像属性的变化。

我目前使用两个Uiview.Transition块,这些块似乎正常,但是看起来很糟糕,而且我肯定是避免重复代码的更好的方法。目前这样做:

UIView.transition(with: label, duration: 1, options: [.curveEaseInOut, .transitionCrossDissolve], animations: {
        self.label.text = someText
}, completion: nil)

UIView.transition(with: aButton, duration: 1, options: [.curveEaseInOut, .transitionCrossDissolve], animations: {
        self.aButton.setImage(aUIImage, for: .normal)
}, completion: nil)

afaik没有init for uiview.transition,它会吸收一系列的Uiviews来动画或类似的东西。

谢谢!

I need to animate the change of the value of a UILabel and a UIButton's image property.

I currently use two UIView.transition blocks which seem to work fine, but it seems awful and Im sure theres a far better way Im missing to avoid having duplicated code. Currently doing this:

UIView.transition(with: label, duration: 1, options: [.curveEaseInOut, .transitionCrossDissolve], animations: {
        self.label.text = someText
}, completion: nil)

UIView.transition(with: aButton, duration: 1, options: [.curveEaseInOut, .transitionCrossDissolve], animations: {
        self.aButton.setImage(aUIImage, for: .normal)
}, completion: nil)

Afaik theres no init for UIView.transition which takes in an array of UIViews to animate or something similar.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2025-02-04 20:38:16

概念

似乎没有“内置”方法可以在一个 uiview.transition 动画块中过渡多个视图。

相反,如果我们有一种控制过渡而没有块,我们可以尝试自己制作。输入 catransition

您可以使用 catransition 将交叉分解效果添加到 calayer 类似:

let transition = CATransition()
transition.duration = 0.3
transition.type = .fade
view.layer.add(transition, forKey: "transition")

答案

catransition 可以同时添加到多层。因此,我们可以使用这些知识来编写自己的代码,该代码将这种过渡应用于多层(或视图)。

我继续为您制作了 uiview 扩展。剥离版本看起来像这样:

extension UIView{

    class func groupTransition(
        with views: [UIView],
        duration: TimeInterval,
        animations: (() -> Void)?
    ){
        let transition = CATransition()
        transition.duration = duration
        transition.type = .fade
        
        for v in views{
            v.layer.add(transition, forKey: "transition")
        }
        
        animations?()
    }
}

您可以传递要过渡的视图列表,并在一个块中执行所有过渡更改。

UIView.groupTransition(
    with: [label, aButton],
    duration: 0.3
) {
    self.label.text = self.getRandomText()
    self.aButton.setImage(self.getRandomImage(), for: .normal)
}

讨论

我添加了更多功能(例如动画曲线,延迟等),并制作了更可用的版本,并在此处发布了它。

当然,这种方法有缺点。您将失去所有基于“翻转”过渡的花式过渡。这是因为CoreAimation Transition API只有一些基本的(但不错的)效果。

随意订购存储库并为您的喜好进行调整。

Concept

Looks like there are no 'built-in' ways to transition multiple views within one UIView.transition animation block.

Instead, we can try to make our own if we had a way to control transitions without blocks. Enter CATransition!

You can use CATransition to add cross-dissolve effects to CALayer like so:

let transition = CATransition()
transition.duration = 0.3
transition.type = .fade
view.layer.add(transition, forKey: "transition")

Answer

CATransition objects can be added to multiple layers at the same time. So we can use this knowledge to write our own code that applies this transition to multiple layers (or views) at the same time.

I went ahead and made a UIView extension for you. The stripped down version looks like this:

extension UIView{

    class func groupTransition(
        with views: [UIView],
        duration: TimeInterval,
        animations: (() -> Void)?
    ){
        let transition = CATransition()
        transition.duration = duration
        transition.type = .fade
        
        for v in views{
            v.layer.add(transition, forKey: "transition")
        }
        
        animations?()
    }
}

You can pass the list of views you want to transition and perform all the transition changes in a single block.

UIView.groupTransition(
    with: [label, aButton],
    duration: 0.3
) {
    self.label.text = self.getRandomText()
    self.aButton.setImage(self.getRandomImage(), for: .normal)
}

Discussion

I added some more functionality (like animation curve, delay, etc.) and made a more usable version of this and posted it here.

Of course there are disadvantages with this approach. You lose all the fancy transitions built in such as the "flip" transition. This is because the CoreAnimation Transition API only has some basic (but nice) effects baked in.

Feel free to fork the repo and tweak it for your liking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文