【问题标题】:How to move UIButtons?如何移动 UIButtons?
【发布时间】:2015-09-28 11:27:33
【问题描述】:

按下时如何移动按钮?

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if let touch = touches.first { 
        location = touch.locationInView(self.view)
        Button.center = location 
    } 
    super.touchesBegan(touches, withEvent:event) 
} 
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if let touch = touches.first { 
        location = touch.locationInView(self.view) 
        Button.center = location 
    } 
    super.touchesMoved(touches, withEvent:event)
}

按钮不动。

【问题讨论】:

    标签: ios uibutton swift2


    【解决方案1】:

    如果您使用 AutoLayout 放置按钮,则必须更新确定按钮位置的约束。

    这通常通过将约束作为IBOutlet 暴露给您的视图并修改其constant 属性(实际上根本不是常量)来完成。

    你需要两个约束:

    • UIView 左侧的水平距离
    • UIView 顶部的垂直距离

    例如

    horizontalConstraint.constant = location.x
    verticalConstraint.constant = location.y
    

    如果您没有使用 AutoLayout(即您使用的是 autoresizing/springs 和 struts),那么您上面的代码应该可以正常工作。因此,您可以尝试在视图的 xib 中关闭 AutoLayout。

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多