UIButton限制了动画问题

时间:2015-08-13 08:50:29

标签: ios autolayout cabasicanimation

我使用CABasicAnimation来设置按钮边界的动画。我没有为按钮添加任何自动布局限制。这是我的代码:

class ViewController: UIViewController {

    @IBOutlet var button: UIButton!

    @IBAction func onClick(sender: AnyObject) {

        //self.button.translatesAutoresizingMaskIntoConstraints = true

        let animation = CABasicAnimation(keyPath: "bounds")
        animation.toValue = NSValue(CGRect : CGRect(origin: CGPointZero, size: CGSizeMake(self.button.bounds.size.height, self.button.bounds.size.height)))
        animation.delegate = self
        self.button.layer.addAnimation(animation, forKey: "")

    }

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {

        self.button.setTitle("", forState: .Normal)
        self.button.bounds = CGRect(origin: CGPointZero, size: CGSizeMake(self.button.bounds.size.height, self.button.bounds.size.height))
    }

}

如果我注释掉 setTitle("",forState:.Normal),一切都很好。但是,如果使用 setTitle("",forState:.Normal),该按钮将立即返回原始大小。我认为是因为自动布局生效了。所以我将 translatesAutoresizingMaskIntoConstraints 设置为true。它按我的意愿工作。但是我得到了#34;无法同时满足约束。"我的控制台出错了。

如何处理这个问题?我不想在我的项目中禁用自动布局。虽然我使用UIButton进行测试,但对于普通的UIView来说却是一样的。

0 个答案:

没有答案