我的 ViewController 中有一个 UIButton。当点击此按钮时,它会移动到 4 个调整位置。 问题是当我在 iphone 5s 模拟器中运行该项目时,UIButton 移动到边缘。
此外,调整后的 postitons 无法正常工作。
let size : CGSize = CGSize(width: 100.0, height: 100.0);
var intStep = 0;
override func viewDidLoad() {
super.viewDidLoad()
self.movable_Button.frame.size = size;
self.changePosition();
}
UIView.animate(withDuration: 1) {
self.changePosition();
}
}
func changePosition() {
switch self.intStep {
case 0 :
self.movable_Button.frame.origin = CGPoint(x: 10.0, y: 10.0);
case 1 :
self.movable_Button.frame.origin = CGPoint(x: 280, y: 10)
case 2 :
self.movable_Button.frame.origin = CGPoint(x: 280.0, y: 300.0);
case 3 :
self.movable_Button.frame.origin = CGPoint(x: 10.0, y: 300.0);
default:
self.movable_Button.frame.origin = CGPoint(x: 10.0, y: 10.0);
}
self.intStep += 1;
if self.intStep > 3
{
self.intStep = 0;
}
}
}
最佳答案
你到底想在这里做什么?
您似乎正在将按钮的 x
值移动到超出 iPhone 5s 的屏幕限制。
我建议您更改一些案例以使其可调整。 建议的更改可能是:
case 1 :
let isMovable1 = (self.movable_Button.frame.origin.x + 280 + 100) < UIScreen.main.bounds.width
self.movable_Button.frame.origin = CGPoint(x: isMovable1 ? 280 : UIScreen.main.bounds.width - 100, y: 10)
case 2 :
let isMovable2 = (self.movable_Button.frame.origin.x + 280 + 100) < UIScreen.main.bounds.width
self.movable_Button.frame.origin = CGPoint(x: isMovable2 ? 280 : UIScreen.main.bounds.width - 100, y: 10)
关于Swift - UI 按钮 : Animated Button crosses the edge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185894/