我正在尝试在 CALayer 中添加自定义 View 。

http://i.imgur.com/sYzQ4kX.png

我想在里面放一些按钮和标签,但恐怕我做不到。我像这样制作 CALabel:

func addRectangleToLayer(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat, layer: CALayer, index: UInt32 ) {
    var sublayer = CALayer()
    sublayer.backgroundColor = UIColor.whiteColor().CGColor
    sublayer.shadowOffset = CGSizeMake(0, 3);
    sublayer.shadowRadius = 5.0;
    sublayer.shadowColor = UIColor.blackColor().CGColor
    sublayer.shadowOpacity = 0.8;
    sublayer.cornerRadius = 12.0;
    sublayer.frame = CGRectMake(x, y, width, height);
    sublayer.borderColor = UIColor.blackColor().CGColor;
    sublayer.borderWidth = 0.5;

    //An example
    let label = UILabel()
    label.text = "LOREN"
    sublayer.contents = label

    layer.insertSublayer(sublayer, atIndex: index)
}

可以做我想做的事吗?

非常感谢,请原谅我的英语水平

最佳答案

如果您创建 UIView ,您将可以为此访问其 layer 属性:

let label = UILabel()
label.text = "LOREN"
var sublayer = label.layer;

// .. the rest of your layer initialization
sublayer.backgroundColor = UIColor.whiteColor().CGColor
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = UIColor.blackColor().CGColor
sublayer.shadowOpacity = 0.8;
sublayer.cornerRadius = 12.0;
sublayer.frame = CGRectMake(x, y, width, height);
sublayer.borderColor = UIColor.blackColor().CGColor;
sublayer.borderWidth = 0.5;
// .. ended original source initialization

layer.insertSublayer(sublayer, atIndex: index)

关于ios - Swift:在 CALayer 中添加自定义 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26738748/

10-12 00:16