【问题标题】:How to loop Multiple UIButton in swift如何快速循环多个 UIButton
【发布时间】:2016-03-28 10:02:00
【问题描述】:

我打算通过使用循环来使我的 UIButton 框架的新位置,但我没有运气,这是我的代码:

override func viewDidLoad() {
        super.viewDidLoad()


for (var i = 75 ; i <= 275; ++i) {
        let marginLoopButton: CGFloat = 175.0
        let widthLoopButton = view.bounds.width - 2 * marginLoopButton
        let marginYLoopButton: CGFloat = 295.0
        let minimalYLoopButton = view.bounds.maxY - 2 * marginYLoopButton
        self.loopButtonTwo.frame = CGRect(x: i, y: minimalYLoopButton, width: widthLoopButton, height: 70)
        self.loopButtonTwo.setTitle("Orange", forState: UIControlState.Normal)
        self.view.addSubview(self.loopButtonTwo)
    }
}

所以第一个按钮的位置将不同于第二个按钮。问题是:self.loopButtonTwo.frame = CGRect(x: i, y: minimalYLoopButton, width: widthLoopButton, height: 70) 当我将i 放在x: 上时,Xcode 给了我错误消息:“无法使用类型参数列表调用 CGRect 类型的初始化程序(x:int,y:CGFloat,宽度”

以及如何快速将数字相乘?我知道在 C 中我们只写 for (i = 75 ; i &lt;= 275; +=10){} 但是如何用 swift 写?

有人可以帮助我吗?

【问题讨论】:

  • 视图是否使用自动布局?
  • 我认为这段代码不会做你想要的,你是想动画按钮移动吗?如果是这种情况,您需要使用UIView.animate... 方法
  • @trojanfoe 根本没有
  • 问题实际上是关于铸造和for 快速循环,与UIButtons 无关。
  • @Fonix 我也打算为它制作动画,但我知道如何为我正在寻找的 UIButton 和 jason Nam 的代码制作动画

标签: ios uibutton swift2


【解决方案1】:

只需将 i 转换为 CGFloat。

self.loopButtonTwo.frame = CGRect(x: CGFloat(i), y: minimalYLoopButton, width: widthLoopButton, height: 70)

【讨论】:

  • 请选择我的答案,这对我很有帮助:)
猜你喜欢
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
相关资源
最近更新 更多