composite colors: CALayer and blend mode on iPhone

I managed to get the affect of compositing multiple CALayers by drawing them directly into a UIView’s graphics context. -(void)drawRect:(CGRect)rect { CGContextRef c = UIGraphicsGetCurrentContext(); CGContextSetBlendMode(c, kCGBlendModeDifference); [myLayer drawInContext:c]; } BTW, I did not add the layers as sublayers of the view’s layer (that is I never called [myView.layer addSublayer:myLayer])

How to Animate a UIBezierPath

I am not an expert on CoreAnimation but you can define a CABasicAnimation as follows CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@”path”]; morph.duration = 5; morph.toValue = (id) [self getTrianglePath]; [maskLayer addAnimation:morph forKey:nil]; The first line states that you want to define an animation that changes the path property of the layer. The second line states that … Read more

How is the relation between UIView’s clipsToBounds and CALayer’s masksToBounds?

They are different names because UIView and CALayer are different and have different terminology associated with them, but they are functionally equivalent. If you disassemble clipsToBounds you will see it just calls masksToBounds (disassembly from the simulator framework, so x86): -(BOOL)[UIView(Rendering) clipsToBounds] +0 3091938a 55 pushl %ebp +1 3091938b 89e5 movl %esp,%ebp +3 3091938d e800000000 … Read more

UIView with a Dashed line

Check UIBezierPath setLineDash:count:phase: method: – (void)setLineDash:(const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase` method. This allows you to draw dashed lines. First add a CAShapeLayer. Add it as sublayer to your UIView. It has a path property. Now make an object of UIBezierPath. Draw the line using setLineDash. For example: UIBezierPath *path = [UIBezierPath bezierPath]; //draw a line … Read more

Swift – Cut hole in shadow layer

Fortunately it’s now very easy today (2020) These days it’s very easy to do this: Here’s the whole thing import UIKit class GlowBox: UIView { override func layoutSubviews() { super.layoutSubviews() backgroundColor = .clear layer.shadowOpacity = 1 layer.shadowColor = UIColor.red.cgColor layer.shadowOffset = CGSize(width: 0, height: 0) layer.shadowRadius = 3 let p = UIBezierPath( roundedRect: bounds.insetBy(dx: 0, … Read more

add UIImage in CALayer

This is a general answer for the sake of future viewers. It is based on the question title rather than the details of the original question. How to add a UIImage to a CALayer You can add an image to a view’s layer simply by using its contents property: myView.layer.contents = UIImage(named: “star”)?.cgImage Note that … Read more