iPhone - 是否可以创建一个动画,从中心向外显示图像?

Mat*_*cey 6 iphone animation objective-c

我已经看过很多关于如何通过改变他们的alpha值(例如这里)在iPhone上淡入淡出图像的教程.

多数民众赞成,但我正在努力实现略微不同的东西; 而不是整个图像淡入/淡出,我会对图像进行动画处理,以便随着时间的推移它从图像的中心显示到边缘(反之亦然).

因此,在动画开始时,我希望图像不会显示在屏幕上,然后在动画的持续时间内从中心逐位开始显示图像,直到我们到达结尾并且整个图像显示在屏幕.

我无法找到这些方面的任何内容,有谁知道这是否可以在iPhone上实现?任何可能有用的想法/链接?

Tom*_*mmy 4

应该很容易实现 \xe2\x80\x94 创建一个持有该东西的 UIImageView,将 contentMode 设置为 UIViewContentModeCenter,然后使用 CoreAnimation 块将帧中的变化从零宽度/高度并居中到完整您想要覆盖的区域。

\n\n

例如

\n\n
/* coming into here, imageView is a suitable UIImageView,\n   frameToFill is a CGRect describing the area the image\n   view should cover when fully unfurled */\n\n// set up image view: content mode is centre so that the current\n// frame doesn't affect image sizing, and we'll be keeping the same\n// centre throughout so it won't move. The initial frame is on the\n// centre and of zero size. The view contents are clipped to the view's\n// bounds so that the changing frame does actually cut off the image\nimageView.contentMode = UIViewContentModeCenter;\nimageView.frame = CGRectMake(frameToFill.origin.x + frameToFill.size.width*0.5f,\n                             frameToFill.origin.y + frameToFill.size.height*0.5f,\n                             0.0f, 0.0f);\nimageView.clipsToBounds = YES;\n\n// set up an animation block, lasting 3 seconds, in which the\n// only thing animated is the view's frame, expanding back out\n// to the original\n[UIView beginAnimations:nil context:nil];\n[UIView setAnimationDuration:3.0f];\n    imageView.frame = frameToFill;\n[UIView commitAnimations];\n
Run Code Online (Sandbox Code Playgroud)\n\n

您的视图边缘将是坚硬的矩形。如果您想要更微妙的东西,您可能无法使用 CoreAnimation 来实现;下降到 CoreGraphics 级别可能是当今的惯例。

\n

  • @MattStacey尝试设置属性“imageView.clipsToBounds”和/或“imageView.layer.masksToBounds”。默认情况下,子图层不会被剪裁。 (3认同)