我有一个按钮,当我按下它时,我想缩小宽度。但是,我目前的方法(以及我在网上到处看到的方法)似乎不起作用。
方法是这样的
@IBAction func addToOrderPressed(sender: AnyObject) {
UIView.animateWithDuration(1.0, animations: {
self.addToOrderButton.frame = CGRectMake(
self.addToOrderButton.frame.origin.x,
self.addToOrderButton.frame.origin.y,
self.addToOrderButton.frame.size.width - 100,
self.addToOrderButton.frame.size.height)
})
self.addProductToCart(productAtIndex)
println("[Info] Product Added: \(productAtIndex.title)")
println("[Info] Cart Count: \(self.dm.cartItems.count)")
}
点击按钮,其他操作运行,但按钮保持相同大小。
我在这个按钮上使用了约束,下面是显示我正在使用的约束的屏幕截图:
问题可能是约束优先于大小更改吗?
最佳答案
如果您为按钮使用自动布局,则需要更改宽度约束。首先,您可以为按钮宽度约束创建 IBOutlet NSLayoutConstraint。
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
[self.view setNeedsUpdateConstraints];
[self.view layoutIfNeeded];
UIView.animateWithDuration(1.0, animations: {
widthConstraint = widthConstraint - 100
[self.view layoutIfNeeded];
})
关于ios - 按钮不改变动画中的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32144020/