CALayer在iPhone / iPad上自动调整:如何? [英] CALayer autoresizing on iPhone/iPad: How?
问题描述
我正在尝试创建一个UIView子类(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.
看起来不错,但是当屏幕旋转时,它没有调整大小渐变层。没有运气在图层上找到BOOL属性来切换,我在GradientView中覆盖了layoutSubviews。
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;
}
这样可行,但GradientView背后的东西在设备旋转期间仍然可见动画。 自动调整CAGradientLayer与其父图层边界匹配的最简单方法是什么,以便动画流畅(如UIView自动调整)?
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)?
推荐答案
你可以在定制的UIView上定义+ layerClass方法......
You can probably get away with defining the +layerClass method on a custom UIView...
@implementation MyGradientView
+(Class) layerClass {
return [CAGradientLayer class];
}
@end
您可以在控制器的viewDidLoad中初始化图层( )(或任何地方),类似......
You can initialize the layer in your controller's viewDidLoad() (or wherever), something like...
-(void)viewDidLoad {
[super viewDidLoad];
[(CAGradientLayer*)[mMyGradientViewInstance layer] setColors:nil];
}
这篇关于CALayer在iPhone / iPad上自动调整:如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!