首页 > 解决方案 > 当我将 UIView 动画放在不同类的 containerView 中时,为什么我的 UIView 动画不起作用?

问题描述

我在 a 上做了一个简单的动画UIView,它按照我想要的方式工作。但是当我分离代码时它停止工作。我已将UIView应该动画化的那个放入 acontainerView并且containerView连接到animationCode class. “主要”也与触发动画的按钮viewController分开。class

从主要viewController

bottomTableView.rx.modelSelected(ModeButtons.self)
    .subscribe(onNext: { [weak self] value in
        switch value {
            case .One:
                self?.container.animationOne()
            case .Two:
                self?.container.animationTwo()
        }
    }).disposed(by: disposeBag)

和动画功能之一:

func animationOne(){
    UIView.animate(withDuration: 0.3, animations: {
        self.downarrow?.alpha = 1.0
        self.upArrow?.alpha = 1.0
        self.leftLine?.frame.size.width = 10.0
        self.rightLine?.frame.size.width = 10.0
        self.rightLine?.frame.origin.x = self.animationView.frame.width - self.rightLine!.frame.width
        self.upArrow?.frame = CGRect(x: self.animationView.frame.width - self.rightLine!.frame.width - self.upArrow!.frame.width - 5.0, y: (self.animationView.frame.height - self.upArrow!.frame.height) / 2, width: self.upArrow!.frame.width, height: self.upArrow!.frame.height)
        self.downarrow?.frame = CGRect(x: self.leftLine!.frame.width + 5.0, y: (self.animationView.frame.height - self.upArrow!.frame.height) / 2, width: self.upArrow!.frame.width, height: self.upArrow!.frame.height)
    })
}

这是一张可以更好地解释设置的图像:左边是containerView带有 的UIView,下面是buttons. 就像我之前说的。当一切都在同一个viewControllerclass. 那么为什么动画现在不工作了?我错过了什么? 在此处输入图像描述

标签: iosswiftuiviewcontrollercontainersuianimation

解决方案


有时将动画代码放入主队列可以解决这个问题

DispatchQueue.main.async {
    UIView.animate(withDuration: 0.3) {

    }
}

推荐阅读