Animating a custom property of CALayer subclass

Yes, it’s possible (only in the latest Core Animation releases though, I believe, i.e. iPhone 3.0+ and OS X 10.6+). Make your property dynamic so that CA implements the accessors for you: @dynamic myInt; Tell the layer that changes of the property require redrawing: + (BOOL)needsDisplayForKey:(NSString*)key { if ([key isEqualToString:@”myInt”]) { return YES; } else … Read more

CALayer: add a border only at one side

I made a right border using this: leftScrollView.clipsToBounds = YES; CALayer *rightBorder = [CALayer layer]; rightBorder.borderColor = [UIColor darkGrayColor].CGColor; rightBorder.borderWidth = 1; rightBorder.frame = CGRectMake(-1, -1, CGRectGetWidth(leftScrollView.frame), CGRectGetHeight(leftScrollView.frame)+2); [leftScrollView.layer addSublayer:rightBorder];

CALayer with transparent hole in it

I was able to solve this with Jon Steinmetz suggestion. If any one cares, here’s the final solution: int radius = myRect.size.width; UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.mapView.bounds.size.width, self.mapView.bounds.size.height) cornerRadius:0]; UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius]; [path appendPath:circlePath]; [path setUsesEvenOddFillRule:YES]; CAShapeLayer *fillLayer = [CAShapeLayer layer]; fillLayer.path = path.CGPath; fillLayer.fillRule = kCAFillRuleEvenOdd; … Read more

UIImage from CALayer in iOS

Sounds like you want to render your layer into a UIImage. In that case, the method below should do the trick. Just add this to your view or controller class, or create a category on CALayer. Obj-C – (UIImage *)imageFromLayer:(CALayer *)layer { UIGraphicsBeginImageContextWithOptions(layer.frame.size, NO, 0); [layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return outputImage; } … Read more

Disabling implicit animations in -[CALayer setNeedsDisplayInRect:]

You can do this by setting the actions dictionary on the layer to return [NSNull null] as an animation for the appropriate key. For example, I use NSDictionary *newActions = @{ @”onOrderIn”: [NSNull null], @”onOrderOut”: [NSNull null], @”sublayers”: [NSNull null], @”contents”: [NSNull null], @”bounds”: [NSNull null] }; layer.actions = newActions; to disable fade in / … Read more

Inner shadow effect on UIView layer?

For anyone else wondering how to draw an inner shadow using Core Graphics as per Costique’s suggestion, then this is how: (on iOS adjust as needed) In your drawRect: method… CGRect bounds = [self bounds]; CGContextRef context = UIGraphicsGetCurrentContext(); CGFloat radius = 0.5f * CGRectGetHeight(bounds); // Create the “visible” path, which will be the shape … Read more