所以我要随机排列三张图片。我使用三个对象的原点作为基点。我尝试了一些不同的东西,但他们不会让步。
重要的是要注意我对这些图像有限制。它们在顶部和底部都有填充,两侧填充为 0。它们也被限制为相同的大小,以防约束设置限制对象的移动。
//the starting positions of the objects
spot_1 = img1.frame.origin
spot_2 = img2.frame.origin
spot_3 = img3.frame.origin
我对这些代码集尝试了一些不同的调整:
img2.frame = CGRect(x: spot_1.x, y: spot_1.y, width: box_width, height: box_width)
img3.frame = CGRect(x: spot_2.x, y: spot_2.y, width: box_width, height: box_width)
img1.frame = CGRect(x: spot_3.x, y: spot_3.y, width: box_width, height: box_width)
&
img2.frame = spot_1
img3.frame = spot_2
img1.frame = spot_3
我尝试过制作动画、不同版本的 CGrect 等。无法弄清楚。感谢您的帮助!
最佳答案
尝试同时使用(自动布局)约束和框架是不兼容的。约束确定并设置 View 的框架。您不应该以编程方式更改框架。
如果您想移动受约束的对象,您要做的是以编程方式修改其位置约束。
您可以通过添加 IBOutlet 并将 Storyboard 约束连接到您的属性来以编程方式更改约束。
@IBOutlet weak var image1LeadingSpaceConstraint: NSLayoutConstraint!
然后在代码中,您可以更改约束的常量来为图像设置新的边距
image1LeadingSpaceConstraint.constant = ...
您甚至可以为您的更改设置动画,以便图像以动画方式移动到它的新位置
[UIView animateWithDuration:2.0 animations:^{
[self.view layoutIfNeeded];
}];
关于ios - Swift 2 Xcode - UIImageView 上的 CGRect(在框架内移动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32362549/