Swift: add custom view inside CALayer
I am trying to add a custom view inside a CALayer.
http://i.imgur.com/sYzQ4kX.png
And I want to insert some buttons and shortcuts, but I'm afraid I can't do it. I am doing CALabel like this:
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)
}
Can I do what I want?
Thanks a lot and sorry for my english level
+3
source to share
1 answer
If you create UIView
, for this purpose you will have property access 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)
+9
source to share