iPhone/iPad 上的 CALayer 自动调整大小:如何?
我正在尝试创建一个 UIView 子类(“GradientView”),它将用各种颜色的渐变填充自身。我通过添加 CAGradientLayer 作为 UIView 层的子层来实现它。
看起来不错,但是当屏幕旋转时,它没有调整渐变层的大小。由于没有找到要切换的图层上的 BOOL 属性,我覆盖了 GradientView 中的layoutSubviews。
-(void)layoutSubviews {
self.gradientLayer.frame = self.bounds;
}
这是可行的,但是在设备旋转动画期间,GradientView 后面的内容仍然可见。 “自动调整” CAGradientLayer 大小以匹配其父层边界以使动画平滑(如 UIView 自动调整大小)的最简单方法是什么?
I'm trying to create a UIView subclass ("GradientView") that will fill itself with a gradient of various colors. I've implemented it by adding a CAGradientLayer as a sub-layer of UIView's layer.
It looked good, but when the screen was rotated, it didn't resize the gradient layer. Having no luck finding a BOOL property on the layer to toggle, I overrode layoutSubviews in the GradientView.
-(void)layoutSubviews {
self.gradientLayer.frame = self.bounds;
}
This works, but the stuff behind the GradientView is still visible during the device rotation animation. What is the easiest way to 'autoresize' that CAGradientLayer to match its parent layer's bounds so that the animation is smooth (like for UIView autoresizing)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能可以在自定义 UIView 上定义 +layerClass 方法...
您可以在控制器的 viewDidLoad() (或其他地方)中初始化图层,例如...
You can probably get away with defining the +layerClass method on a custom UIView...
You can initialize the layer in your controller's viewDidLoad() (or wherever), something like...
我会:
增加视图的大小到 willRotateToInterfaceOrientation 代码中每个维度的最大值。 (例如,对于 320x480 iPhone - 将暗淡设置为 480x480)。
根据 didRotateFromInterfaceOrientation 函数中新旋转的视图设置边界。
这应该使视图足够大,以便无论它在动画期间如何定向,它都会覆盖整个屏幕。它不会是“平滑”的 - 因为渐变必须旋转,但至少你不会在旋转中间看到图层后面。
I would:
Increase the size of your view to the maximum of each dimension in the willRotateToInterfaceOrientation code. (For example for an 320x480 iPhone - set the dims to 480x480).
Set the bounds in accordance to the newly-rotated view in the didRotateFromInterfaceOrientation function.
This should make it so that the view is large enough so that regardless of how it is oriented during animation, it will cover the entire screen. It won't be "smooth" - because the gradient will have to be rotated, but at least you will not see behind the layer in the middle of the rotation.
请注意,仅当将视图背景颜色设置为透明时,返回 CAGradientLayer 作为图层类才有效。如果您需要透明以外的颜色,则需要设置图层背景颜色。
Note that returning CAGradientLayer as the layer class only works if you set the view background color to transparent. You'll need to set the layer background color if you need a color other than transparent.
就我而言,这项工作:
in my case this work: