【问题标题】:Animation difference between UIView and UIButtonUIView 和 UIButton 的动画区别
【发布时间】:2016-07-14 16:00:36
【问题描述】:

在我的自定义表格视图单元格中,我有 4 个带有动画的按钮(如下所示)。问题是当我使用UIButton 时,动画没有按照我的意愿进行动画。但是当我使用UIView 时,它的工作方式完全符合我的要求。

代码完全相同,只是使用了不同类型的UIView

此动画使用 UIButton

此动画使用 UIView

为了让事情更清楚一点,我在代码中唯一替换的是:

// Test with Buttons
let button1 = Button()  // Subclass of UIButton
let button2 = Button()    

// Test with UIViews
let button1 = UIView()
let button2 = UIView()

问题:
有人能告诉我为什么UIButton 的行为与普通的UIView 不同吗?


最初我认为通过不发布代码,我可以使问题更易于阅读,因为两个测试都使用完全相同的代码(“元素”除外(元素为 UIViewUIButton),并认为也许问题在于“元素”之间的差异。我现在意识到这是我的错误。

我的代码:

class CustomView: UIView {

    private var base: [NSLayoutConstraint] = []
    private var open: [NSLayoutConstraint] = []

    var buttons: [UIView] = []

    private var active = false

    override init(frame: CGRect) {
        super.init(frame: frame)

        let button1 = CustomButton(frame: CGRectZero, color: UIColor.yellowColor().CGColor)
        let button2 = CustomButton(frame: CGRectZero, color: UIColor.redColor().CGColor)

    //  let button1 = UIView(); button1.backgroundColor = UIColor.yellowColor()
    //  let button2 = UIView(); button2.backgroundColor = UIColor.redColor()

        let views = ["button1": button1, "button2": button2]

        buttons = [button1, button2]

        buttons.enumerate().forEach {
            $0.element.translatesAutoresizingMaskIntoConstraints = false
            addSubview($0.element)

            addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[button\($0.index + 1)]|", options: [], metrics: nil, views: views))

            base += [NSLayoutConstraint(item: $0.element, attribute: .Width , relatedBy: .Equal, toItem: buttons.first!, attribute: .Width , multiplier: 1, constant: 0)]
        }

        open += [NSLayoutConstraint(item: buttons.last!, attribute: .Width, relatedBy: .Equal, toItem: buttons.first!, attribute: .Width, multiplier: 0.33, constant: 0)]           
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[button1]-0.5-[button2]|", options: [], metrics: nil, views: views))
        addConstraints(base)

        backgroundColor = .blackColor()
        clipsToBounds = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func changeState() {
        removeConstraints(active ? open : base); addConstraints(active ? base : open)

        active = !active

        UIView.animateWithDuration(0.5, animations: { self.layoutIfNeeded() })
    }
}

解决方案:
在发布代码并意外更改按钮的背景颜色后,我注意到它的行为相应。这让我意识到我在按钮中使用了CAShapeLayer,这导致了第一个动画中出现的行为。现在我知道要解决什么了。如果这篇文章应该关闭或删除,请告诉我。然后我将删除答案。并感谢那些试图提供帮助的人!

【问题讨论】:

  • 你介意告诉我为什么投反对票吗?我不介意添加动画代码,但看到它适用于UIView,我认为它只会使帖子不必要地更长。
  • 能否添加动画代码?这可能是最重要的事情。

标签: ios animation uiview uibutton


【解决方案1】:

如果您不提供更多详细信息,我唯一能做的就是猜测您所看到的行为归结为按钮的“弹性”不如视图,因为它们具有由其内容(标题和/或图像)和自动布局真的很喜欢强制执行自然大小(正如您从 IB 的警告中看到的那样)。

如果您需要更多帮助,您应该提供更多详细信息。你真的在使用自动布局吗?有什么限制?你在动画什么?等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多