我最近一直在为iPhone做一些编程,现在我正在涉足iPad领域。我要实现的概念依赖于类似于osx中时间机器的导航。简而言之,与任何普通 View 一样,我有许多可以平移和缩放的 View 。但是, View 使用第三维(在这种情况下为深度)彼此堆叠。在这种情况下,用户可以通过选择一个字母来导航到任何 View ,随后该应用将在 View 中飞行,直到到达所选字母的 View 为止。
我的问题是:有人可以给出完成此操作的完整最终代码吗?只是在开玩笑。 :)我需要朝着正确的方向发展,因为我不确定如何开始这样做,以及使用现有框架是否完全有可能。任何提示表示赞赏
谢谢!
最佳答案
核心动画(或更具体地说,基于核心动画构建的UIView动画模型)是您的 friend 。您可以通过将 View 放置在其父 View 内的垂直线(使用其center
属性)中,使它们的 View 与Time Machine相似,使距离该行较远的 View 的缩放比例略小于下面的比例(“”)(使用transform
函数使用CGAffineTransformMakeScale
属性),并设置图层的z-index(使用 View 的layer
属性获取图层,然后设置其zPosition
),以使行中较远的行出现在其他行的后面。这是一些示例代码。
// animate an array of views into a stack at an offset position (0 has the first view in the stack at the front; higher values move "into" the stack)
// took the shortcut here of not setting the views' layers' z-indices; this will work if the backmost views are added first, but otherwise you'll need to set the zPosition values before doing this
int offset = 0;
[UIView animateWithDuration:0.3 animations:^{
CGFloat maxScale = 0.8; // frontmost visible view will be at 80% scale
CGFloat minScale = 0.2; // farthest-back view will be at 40% scale
CGFloat centerX = 160; // horizontal center
CGFloat frontCenterY = 280; // vertical center of frontmost visible view
CGFloat backCenterY = 80; // vertical center of farthest-back view
for(int i = 0; i < [viewStack count]; i++)
{
float distance = (float)(i - offset) / [viewStack count];
UIView *v = [viewStack objectAtIndex:i];
v.transform = CGAffineTransformMakeScale(maxScale + (minScale - maxScale) * distance, maxScale + (minScale - maxScale) * distance);
v.alpha = (i - offset > 0) ? (1 - distance) : 0; // views that have disappeared behind the screen get no opacity; views still visible fade as their distance increases
v.center = CGPointMake(centerX, frontCenterY + (backCenterY - frontCenterY) * distance);
}
}];
这是它的外观,带有几个随机颜色的 View :