努力让我的应用程序在 iPhone 5 和更小的 iPhone 上都能运行。很高兴需要 iOS6 并使用 Autolayout,因为这很好用。
但是,该应用程序有 4 个按钮可以定期交换位置,因此我从数组中为每个按钮分配了不同的位置,然后使用以下内容为运动设置动画:
[UIView animateWithDuration:0.5 animations:^{
button1.center = [[locations objectAtIndex:0] CGPointValue];
button2.center = [[locations objectAtIndex:1] CGPointValue];
button3.center = [[locations objectAtIndex:2] CGPointValue];
button4.center = [[locations objectAtIndex:3] CGPointValue];
}];
这不适用于自动布局。没有什么变化。我得到的最好的是它会动画到一个位置然后立即弹回。
然而,当我删除 Autolayout 时,所有按钮在较小的 iPhone 上都被卷起,并且因为我使用的是 iPhone 5 尺寸设置的点,它们最终会降低,使底行离开屏幕。我最初有从 Interface Builder 获得的不同 CGPoint 数字,但它们是“错误的”,尽管我认为它们是“错误的”,因为它们是 Autolayout 使用的数字。该数组只是为了您可以看到:
buttonLocations = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:CGPointMake(57, 523)],
[NSValue valueWithCGPoint:CGPointMake(109, 523)],
[NSValue valueWithCGPoint:CGPointMake(57, 471)],
[NSValue valueWithCGPoint:CGPointMake(109, 471)],
nil];
我应该怎么做才能解决这个问题?为每个大小的设备设置不同的点似乎不是很有效。
最佳答案
解决方案是为 items 转换属性设置动画,而不是它的位置。
位置将由自动布局设置,您不想与之抗争。然而,屏幕上的实际位置将是它的“正确”(由自动布局设置)位置,由其变换属性偏移。
所以你可以说 button.transform = CGTransformMake(x,y) 并且它会从它的中心位置出现 x 和 y 。
有多种 CGWhatever 宏可以轻松地进行这些转换,您只需在动画块中更改它们即可移动项目。