ios - 按钮不改变动画中的宽度

标签 ios swift animation uibutton

我有一个按钮,当我按下它时,我想缩小宽度。但是,我目前的方法(以及我在网上到处看到的方法)似乎不起作用。

方法是这样的

@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)")
    }

点击按钮,其他操作运行,但按钮保持相同大小。

我在这个按钮上使用了约束,下面是显示我正在使用的约束的屏幕截图:

enter image description here

问题可能是约束优先于大小更改吗?

最佳答案

如果您为按钮使用自动布局,则需要更改宽度约束。首先,您可以为按钮宽度约束创建 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/

相关文章:

ios - 带有导航 Controller 方向的标签栏 Controller ios 6

ios - Collection view Flow布局有执行分页的概念吗?

swift - 将 Firestore 添加到我的项目会导致我的实时数据库查询出错

iphone - 如何重复使用多个 .xib 作为另一个 .xib 的模板?

ios - 将 CocoaPods 库存储在自己的 Git 存储库中

ios - UITableView 单元格重复

javascript - fullpage.js 直接转到单击的幻灯片

ios - iOS swift 中推送通知的文本对齐

wpf - 共享同一时间线的多个 Storyboard或动画?

iphone - 在立方体动画中切换两个 View Controller 的 View