gpt4 book ai didi

ios - 添加一个 CALayer 作为 UIView 居中的子层

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:29 30 4
gpt4 key购买 nike

我正在使用最新的 SDK 开发 iOS 应用程序。

我想在 UIView 中添加一个 CALayer,这个 CALayer 将小于 UIView。我想模拟某种 margin 。

static int const NSLayerX       = 0;
static int const NSLayerY = 0;
static int const NSLayerWidth = 60;
static int const NSLayerHeight = 60;

- (void)setBackgroundLayer:(UIColor*)startColor endColor:(UIColor*)endColor cornerRadius:(int)radius
{
backgroundGradientLayer = [CAGradientLayer layer];
backgroundGradientLayer.frame = CGRectMake(NSLayerX, NSLayerY, NSLayerWidth, NSLayerHeight);
backgroundGradientLayer.cornerRadius = radius;
backgroundGradientLayer.colors =
[NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
[self.layer insertSublayer:backgroundGradientLayer atIndex:0];
}

但是我不知道怎么设置居中。

我是否必须更改 NSLayerX 和 NSLayerY 的值?

最佳答案

如下分配层的框架。

backgroundGradientLayer.frame = CGRectMake(self.frame.size.width/4,  self.frame.size.height/4, self.frame.size.width/2, self.frame.size.height/2);

关于ios - 添加一个 CALayer 作为 UIView 居中的子层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15638505/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com