iPhone/iPad 上的 CALayer 自动调整大小:如何?

发布于 2024-09-18 10:35:24 字数 406 浏览 2 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

喜爱纠缠 2024-09-25 10:35:24

您可能可以在自定义 UIView 上定义 +layerClass 方法...

@implementation MyGradientView
+(Class) layerClass {
    return [CAGradientLayer class];
}
@end

您可以在控制器的 viewDidLoad() (或其他地方)中初始化图层,例如...

-(void)viewDidLoad {
    [super viewDidLoad];
    [(CAGradientLayer*)[mMyGradientViewInstance layer] setColors:nil];
}

You can probably get away with defining the +layerClass method on a custom UIView...

@implementation MyGradientView
+(Class) layerClass {
    return [CAGradientLayer class];
}
@end

You can initialize the layer in your controller's viewDidLoad() (or wherever), something like...

-(void)viewDidLoad {
    [super viewDidLoad];
    [(CAGradientLayer*)[mMyGradientViewInstance layer] setColors:nil];
}
烟酉 2024-09-25 10:35:24

我会:

  1. 增加视图的大小到 willRotateToInterfaceOrientation 代码中每个维度的最大值。 (例如,对于 320x480 iPhone - 将暗淡设置为 480x480)。

  2. 根据 didRotateFromInterfaceOrientation 函数中新旋转的视图设置边界。

这应该使视图足够大,以便无论它在动画期间如何定向,它都会覆盖整个屏幕。它不会是“平滑”的 - 因为渐变必须旋转,但至少你不会在旋转中间看到图层后面。

I would:

  1. 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).

  2. 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.

段念尘 2024-09-25 10:35:24
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    CGRect b;

    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))b = CGRectMake(0, 0, 480, 320);
    else b = CGRectMake(0, 0, 320, 480);    

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:duration] forKey:kCATransactionAnimationDuration];
    [self.view.layer setBounds:b];
    [CATransaction commit];

}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    CGRect b;

    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))b = CGRectMake(0, 0, 480, 320);
    else b = CGRectMake(0, 0, 320, 480);    

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:duration] forKey:kCATransactionAnimationDuration];
    [self.view.layer setBounds:b];
    [CATransaction commit];

}
怪异←思 2024-09-25 10:35:24

请注意,仅当将视图背景颜色设置为透明时,返回 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.

若有似无的小暗淡 2024-09-25 10:35:24

就我而言,这项工作:

[[[view.layer sublayers] objectAtIndex:gradientIndex] setFrame:view.bounds];

in my case this work:

[[[view.layer sublayers] objectAtIndex:gradientIndex] setFrame:view.bounds];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文