几年前我做过类似的事情。起初我尝试使用 CGImageMaskCreate 东西,但发现创建一个具有透明“切口”的图像然后使用动画效果滚动它下面的图片要容易得多。
对于您的情况,我会找到屏幕大小的亚麻图像。然后我会使用图像编辑器(我使用 GIMP)在亚麻布上使用纯色绘制一些框。然后我将该框颜色映射为透明以制作切口。还有其他方法可以做到这一点,但我就是这样做的。
在您的应用程序中,将两个或更多图像视图添加到主视图。不要担心位置,因为这将在运行时确定。您需要将这些图像视图设置为包含您希望在其下“滚动”的图像。然后添加带有切口的亚麻布 UIImageView 使其位于顶部并占据整个屏幕尺寸。确保顶部 UIImageView 的背景设置为透明。
当应用程序启动时,从上到下布局您的“下方”图像视图,然后启动一个 [UIView beginAnimation],通过修改“y”位置向上滚动您的下方图像视图。这个动画应该有一个 done 回调,当顶部图像视图完全离开屏幕时会调用该回调。然后,在 done 回调中,布局当前状态并再次启动动画。这是我使用的代码的主要内容(但请注意,我的滚动是从右到左,而不是从下到上,而且我的图像大小都相同。)
- (void)doAnimationSet
{
[iv1 setFrame:CGRectMake(0, 0, imageWidth, imageHeight)];
[iv2 setFrame:CGRectMake(imageWidth, 0, imageWidth, imageHeight)];
[iv3 setFrame:CGRectMake(imageWidth*2, 0, imageWidth, imageHeight)];
[self loadNextImageSet];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:10];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[iv1 setFrame:CGRectMake(-imageWidth, 0, imageWidth, imageHeight)];
[iv2 setFrame:CGRectMake(0, 0, imageWidth, imageHeight)];
[iv3 setFrame:CGRectMake(imageWidth, 0, imageWidth, imageHeight)];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(doneAnimation:finished:context:)];
[UIView commitAnimations];
}
- (void)doneAnimation:(NSString *)aid finished:(BOOL)fin context:(void *)context
{
[self doAnimationSet];
}
这应该会给你你正在寻找的效果。祝你好运 :)