简体   繁体   English

添加CALayer作为UIView内部的子图层

[英]Add a CALayer as sublayer centered inside an UIView

I'm developing an iOS application with latest SDK. 我正在开发一个带有最新SDK的iOS应用程序。

I want to add a CALayer inside an UIView and this CALayer will be smaller than the UIView . 我想在UIView添加一个CALayer ,这个CALayer将小于UIView I want to simulate some kind of 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];
}

But I don't know how to set it centered. 但我不知道如何将它设置为中心。

Do I have to change values for NSLayerX and NSLayerY? 我是否必须更改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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM