CABasicAnimation not working in iOS 9 - ios

I just want to animate a view's background color. What I did up to now is:
CABasicAnimation *backgroundColorAnimation = [CABasicAnimation animationWithKeyPath:#"backgroundColor"];
backgroundColorAnimation.duration = 1.0;
backgroundColorAnimation.beginTime = CACurrentMediaTime() + 0.5;
backgroundColorAnimation.repeatCount = HUGE_VALF;
backgroundColorAnimation.autoreverses = YES;
backgroundColorAnimation.fromValue = (id)[[UIColor whiteColor] CGColor];
backgroundColorAnimation.toValue = (id)[[UIColor redColor] CGColor];
[self.layer addAnimation:backgroundColorAnimation forKey:#"animation.backgroundColor"];
The above works fine up to iOS 8 but not with iOS 9. Any help with what has changed?

Related

CAShapeLayer shrink and fade out animation not working correctly

I have this circle (that fits inside the letter O which is currently not exactly a circle) and I want it to shrink and regrow, fading out and in. So I have it shrinking, and fading out, but when it does the animation, the CAShapeLayer moves to the top left of the screen and doesn't stay put in place (which is the objective). How do I accomplish this?
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(74.93, 59.5, 31.52, 31.2)] CGPath]];
[circleLayer setStrokeColor:[[UIColor colorWithRed:0.8 green:0.1 blue:0.2 alpha:1.0] CGColor]];
[circleLayer setFillColor:[[UIColor colorWithRed:0.8 green:0.1 blue:0.2 alpha:1.0] CGColor]];
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:#"opacity"];
CABasicAnimation* shrinkAnim = [CABasicAnimation animationWithKeyPath:#"transform.scale"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 1.25;
shrinkAnim.fromValue = [NSNumber numberWithFloat:1.0];
shrinkAnim.toValue = [NSNumber numberWithFloat:0.05];
shrinkAnim.duration = 1.25;
[circle1 addAnimation:fadeAnim forKey:#"opacity"];
[circle1 addAnimation:shrinkAnim forKey:#"transform.scale"];
circle1.opacity = 0.0;

How to get shadow in UIButton inside

Using below code for shadow in outside for UIButton. But how can i get shadow for inside the button
button.imageView.layer.cornerRadius = 7.0f;
button.layer.shadowRadius = 3.0f;
button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
button.layer.shadowOpacity = 0.5f;
button.layer.masksToBounds = NO;
#define kDEFAULT_SHADOW_COLOR [UIColor lightGrayColor]
UIColor *color = [kDEFAULT_SHADOW_COLOR colorWithAlphaComponent:0.3f];
NSArray *colorsArray = #[(id)[color CGColor], (id)[[UIColor clearColor] CGColor]];
CGFloat yOffset = 0.0f;
CGFloat leftHeight = button.bounds.size.height;
CAGradientLayer *shadow;
shadow = [CAGradientLayer layer];
shadow.colors = colorsArray;
shadow.frame = CGRectMake(0, yOffset, 5.0, leftHeight);
shadow.startPoint = CGPointMake(0.0, 0.5);
shadow.endPoint = CGPointMake(1.0, 0.5);
[button.layer insertSublayer:shadow atIndex:0];
You have not implemented the button shadows property. Add the following lines of code and show shadow. The Code is...
self.submitBtn.layer.masksToBounds = YES;
self.submitBtn.layer.clipsToBounds = YES;
self.submitBtn.backgroundColor = [UIColor colorWithRed:(200.0f/255.0f) green:0.0 blue:0.0 alpha:1.0];
self.submitBtn.layer.cornerRadius = 3.0;
self.submitBtn.layer.borderWidth = 2.0;
self.submitBtn.layer.borderColor = [[UIColor clearColor] CGColor];
self.submitBtn.layer.shadowColor = [UIColor colorWithRed:(100.0f/255.0f) green:0.0 blue:0.0 alpha:1.0].CGColor;
self.submitBtn.layer.shadowOpacity = 1.0f;
self.submitBtn.layer.shadowRadius = 1.0f;
self.submitBtn.layer.shadowOffset = CGSizeMake(0, 3);

how to create a line graph with multiple lines and different colors using bezierpath

I created one bezierpath and added that to cashapelayer but i cant able to change the color of path
//create path
UIBezierPath *graphPath = [[UIBezierPath alloc]init];
[graphPath setLineWidth:30];
[[UIColor blackColor] setStroke];
[graphPath moveToPoint:CGPointMake(30 , 30)];
[graphPath addLineToPoint:CGPointMake(30,600)];
[graphPath addLineToPoint:CGPointMake(380, 600)];
//adding path to layer
CAShapeLayer *graphLayout = [CAShapeLayer layer];
graphLayout.frame = CGRectMake(0, 65, VIEW_WIDTH, VIEW_HEIGHT);
graphLayout.fillColor = [[UIColor clearColor] CGColor];
graphLayout.strokeColor = [UIColor redColor].CGColor;
graphLayout.lineWidth = 2;
graphLayout.path = [graphPath CGPath];
[self.layer addSublayer:graphLayout];
//animation
drawAnimation = [CABasicAnimation animationWithKeyPath:#"strokeEnd"];
drawAnimation.duration = 3;
drawAnimation.repeatCount = 1.0;
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[graphLayout addAnimation:drawAnimation forKey:#"drawCircleAnimation"];

ObjC animation won't run

I'm trying to change my view background color with this animation, it should change from blue to red to green but it won't run, I can't figure out what I'm doing wrong.
CAKeyframeAnimation * anim = [ CAKeyframeAnimation animationWithKeyPath:#"backgroundColor" ] ;
anim.values = #[ [UIColor blueColor],[UIColor redColor],[UIColor greenColor] ] ;
anim.autoreverses = NO;
anim.repeatCount = 7.20f ;
anim.duration = 0.1f ;
UIWindow *win = [[UIApplication sharedApplication]keyWindow];
[win.rootViewController.view.layer addAnimation:anim forKey:nil];
You are trying to add animation to CALayer. CALayer's backgroundColor property have type CGColorRef, not UIColor. I've modified your code in the following way to make it work:
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:#"backgroundColor"];
anim.values = #[(__bridge id)[[UIColor blueColor] CGColor],
(__bridge id)[[UIColor redColor] CGColor],
(__bridge id)[[UIColor greenColor] CGColor]];
anim.repeatCount = 7.20f ;
anim.duration = 10.0f ;
[self.window.rootViewController.view.layer addAnimation:anim forKey:nil];

iOS the CABasicAnimation color animation not effect

I am implement the the color animation.
I want to black color change to gradient color.
I search some method, that can use the CAGradientLayer to define the gradient color, and the CABasicAnimation can achieve the color change animation.
But I implement below the code, the self view not change to the black in the first, and the color was not animation .
Have anyone know what happened for these code?
thank you very much.
// set black color in the view
CALayer *orginLayer = [CALayer layer];
orginLayer.backgroundColor = [[UIColor blackColor] CGColor];
[self.view.layer addSublayer:orginLayer];
// set gradient layer
CAGradientLayer *colorLayer = [ CAGradientLayer layer];
colorLayer.frame = ( CGRect ){ CGPointZero , CGSizeMake ( self.view.frame.size.width , self.view.frame.size.height )};
colorLayer.position = self .view.center;
// define colors
UIColor *color1 = [UIColor colorWithRed:(255/255.0) green:(137/255.0) blue:(0/255.0) alpha:1.0];
UIColor *color2 = [UIColor colorWithRed:(247/255.0) green:(241/255.0) blue:(107/255.0) alpha:1.0];
NSArray *colors = [NSArray arrayWithObjects:(id)color1.CGColor,color2.CGColor,nil];
colorLayer.colors = colors;
colorLayer.startPoint = CGPointMake ( 1 , 0 );
colorLayer.endPoint = CGPointMake ( .3, 1 );
// set the color animation black color-> gradient color
CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:#"colors"];
[animation setToValue:colorLayer];
[animation setDuration:10];
[animation setRemovedOnCompletion:YES];
[animation setFillMode:kCAFillModeForwards];
[animation setDelegate:self];
[self.view.layer addAnimation:animation forKey:#"animateGradient"];
Here is what you need to do. Cleaned up your code a little bit too. :)
// set gradient layer
CAGradientLayer *colorLayer = [CAGradientLayer layer];
colorLayer.frame = (CGRect){ { }, self.view.frame.size };
colorLayer.position = self.view.center;
// define colors
UIColor *color1 = [UIColor colorWithRed:255/255. green:137/255. blue:0/255. alpha:1.0];
UIColor *color2 = [UIColor colorWithRed:247/255. green:241/255. blue:107/255. alpha:1.0];
NSArray *colors = #[(id)color1.CGColor, (id)color2.CGColor];
colorLayer.colors = colors;
colorLayer.startPoint = CGPointMake(1, 0);
colorLayer.endPoint = CGPointMake(.3, 1);
// set the color animation black color-> gradient color
CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:#"colors"];
animation.fromValue = #[(id)[UIColor blackColor].CGColor, (id)[UIColor blackColor].CGColor];
animation.toValue = colors;
animation.duration = 10;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
[colorLayer addAnimation:animation forKey:#"animateGradient"];
[self.view.layer addSublayer:colorLayer];

Resources