Animating UIImageView with mask (Core Animations with masks) - ios

I have one UIImage in UIImageView (object on image). It needs to be animated to the right, but this animation needs to be masked with this circle mask.
My current code is:
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, 44, 44, 21, 0, 2 * M_PI, NO);
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
[shapeLayer setPath:path];
[shapeLayer setFillColor:[[UIColor blackColor] CGColor]];
[shapeLayer setBounds:CGRectMake(0, 0, 44, 44)];
[shapeLayer setPosition:CGPointMake(0, 0)];
UIImageView *loadingShape = [[UIImageView alloc] initWithFrame:CGRectMake(6.5, 7.5, 89, 40)];
[loadingShape setImage:[UIImage imageNamed:#"object_image.png"]];
[self addSubview:loadingShape];
// Set the mask for the image view's layer
[[loadingShape layer] setMask:shapeLayer];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3];
[UIView setAnimationRepeatCount:10];
[loadingShape setFrame:CGRectMake(0, 7.5, 89, 40)];
[UIView commitAnimations];
But this animates my whole UIImageView masked. I need mask to be static, just object behind to be animated.
What is the best solution to make this?

The mask layer shapeLayer acts as though it is a child of the other layer [loadingShape layer]. When the parent layer moves, the child moves with it.
In addition to your current animation, you will have to animate shapeLayer, in the opposite direction.
Edit: The animation established by -[UIView beginAnimations:context:] does not automatically apply to a bare CALayer, so set up the animation manually.
CGPoint p = /* you decide the new position of the shapeLayer */;
CABasicAnimation* anim = [[CABasicAnimation alloc] init];
anim.toValue = [NSValue valueWithCGPoint:p];
anim.keyPath = #"position";
anim.timingFunction = [CAMediaTimingFunction functionWithName:#"easeOut"];
anim.duration = 0.3;
anim.repeatCount = 10;
[shapeLayer.mask addAnimation:anim forKey:nil];
[anim release];

Thanks to Kurt, I refined a little bit my code and got what I wanter :)
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, 44, 44, 21, 0, 2 * M_PI, NO);
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
[shapeLayer setPath:path];
[shapeLayer setFillColor:[[UIColor blackColor] CGColor]];
[shapeLayer setBounds:CGRectMake(0, 0, 44, 44)];
[shapeLayer setPosition:CGPointMake(0, 0)];
UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(6.5, 7.5, 44, 44)];
UIImageView *loadingShape = [[UIImageView alloc] initWithFrame:CGRectMake(0, 18, 89, 40)];
[loadingShape setImage:[UIImage imageNamed:#"object_image.png"]];
[[loadingView layer] setMask:shapeLayer];
[loadingView addSubview:loadingShape];
[self addSubview:loadingView];
// Set the mask for the image view's layer
CABasicAnimation* anim = [[CABasicAnimation alloc] init];
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 18)];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(15, 18)];
anim.keyPath = #"position";
anim.timingFunction = [CAMediaTimingFunction functionWithName:#"linear"];
anim.duration = 0.3;
anim.repeatCount = 100000;
[[loadingShape layer] addAnimation:anim forKey:nil];

Related

UIView Icon Animation

Im trying to do burst animation when the user clicks a view. Im bursting the view into circular pieces when the user clicks a specific view. So I have converted the uiview to uiimage as follows,
- (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Then I have broken the uiimage to pieces as follows,
-(void)splitImage:(UIImage *)image
{
CGFloat imgWidth = image.size.width/2;
CGFloat imgheight = image.size.height;
CGRect leftImgFrame = CGRectMake(0, 0, imgWidth, imgheight);
CGRect rightImgFrame = CGRectMake(imgWidth, 0, imgWidth, imgheight);
CGImageRef left = CGImageCreateWithImageInRect(image.CGImage, leftImgFrame);
CGImageRef right = CGImageCreateWithImageInRect(image.CGImage, rightImgFrame);
UIImage* leftImage = [UIImage imageWithCGImage:left];
UIImage* rightImage = [UIImage imageWithCGImage:right];
CGImageRelease(left);
CGImageRelease(right);
}
But there are certain issues im facing while doing it.
Im able to break the uiimage into only two pieces but not into
dynamic pieces.
How can i show like then uiview is bursting into circular pieces with these broken uiimages?
UPDATE:
Following is my updated code...
-(void)startAnimation{
//Add the initial circle
// UIView* circleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 60, 60)];
UIView *circleView = [[UIImageView alloc] initWithFrame:self.submit.bounds];
circleView.bounds = self.submit.bounds;
CAShapeLayer *circleLayer = [CAShapeLayer layer];
//set colors
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:circleView.bounds] CGPath]];
[circleView.layer addSublayer:circleLayer];
[self.view addSubview:circleView];
//Animate circle
[circleView setTransform:CGAffineTransformMakeScale(0, 0)];
[UIView animateWithDuration:0.7 animations:^{
[circleView setTransform:CGAffineTransformMakeScale(1.3, 1.3)];
} completion:^(BOOL finished) {
circleView.hidden = YES;
//start next animation
[self createIconAnimation];
}];
}
-(void)createIconAnimation{
//load icon which pops up
UIImageView* iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ic_tick"]];
iconImage.frame = CGRectMake(50, 50, 60, 60);
iconImage.bounds = self.submit.bounds;
[iconImage setTransform:CGAffineTransformMakeScale(0, 0)];
[self.view addSubview:iconImage];
//animate icon
[UIView animateWithDuration:0.3/1.5 animations:^{
iconImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
iconImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
iconImage.transform = CGAffineTransformIdentity;
}];
}];
}];
//add circles around the icon
int numberOfCircles = 20;
CGPoint center = iconImage.center;
float radius= 35;
BOOL isBig = YES;;
for (int i = 0; i<numberOfCircles; i++) {
float x = radius*cos(M_PI/numberOfCircles*i*2) + center.x;
float y = radius*sin(M_PI/numberOfCircles*i*2) + center.y;
float circleRadius = 10;
if (isBig) {
circleRadius = 5;
isBig = NO;
}else{
isBig = YES;
}
UIView* circleView = [[UIView alloc] initWithFrame:CGRectMake(x, y, circleRadius, circleRadius)];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setFillColor:[[UIColor redColor] CGColor]];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:circleView.bounds] CGPath]];
[circleView.layer addSublayer:circleLayer];
[self.view addSubview:circleView];
//animate circles
[UIView animateWithDuration:0.8 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[circleView setTransform:CGAffineTransformMakeTranslation(radius/3*cos(M_PI/numberOfCircles*i*2), radius/3*sin(M_PI/numberOfCircles*i*2))];
[circleView setTransform:CGAffineTransformScale(circleView.transform, 0.01, 0.01)];
} completion:^(BOOL finished) {
circleView.hidden = YES;
}];
}
}
The animation has to be on top of the self.submit button but it is not positioned on top of it
Here you go, just add this code to your view controller. It gives you this result:
Just play around with the colors and the animations to get your desired result.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self startAnimation];
}
-(void)startAnimation{
//Add the initial circle
UIView* circleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 60, 60)];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
//set colors
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:circleView.bounds] CGPath]];
[circleView.layer addSublayer:circleLayer];
[self.view addSubview:circleView];
//Animate circle
[circleView setTransform:CGAffineTransformMakeScale(0, 0)];
[UIView animateWithDuration:0.7 animations:^{
[circleView setTransform:CGAffineTransformMakeScale(1.3, 1.3)];
} completion:^(BOOL finished) {
circleView.hidden = YES;
//start next animation
[self createIconAnimation];
}];
}
-(void)createIconAnimation{
//load icon which pops up
UIImageView* iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Untitled"]];
iconImage.frame = CGRectMake(50, 50, 60, 60);
[iconImage setTransform:CGAffineTransformMakeScale(0, 0)];
[self.view addSubview:iconImage];
//animate icon
[UIView animateWithDuration:0.3/1.5 animations:^{
iconImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
iconImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
iconImage.transform = CGAffineTransformIdentity;
}];
}];
}];
//add circles around the icon
int numberOfCircles = 20;
CGPoint center = iconImage.center;
float radius= 35;
BOOL isBig = YES;;
for (int i = 0; i<numberOfCircles; i++) {
float x = radius*cos(M_PI/numberOfCircles*i*2) + center.x;
float y = radius*sin(M_PI/numberOfCircles*i*2) + center.y;
float circleRadius = 10;
if (isBig) {
circleRadius = 5;
isBig = NO;
}else{
isBig = YES;
}
UIView* circleView = [[UIView alloc] initWithFrame:CGRectMake(x, y, circleRadius, circleRadius)];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setFillColor:[[UIColor redColor] CGColor]];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:circleView.bounds] CGPath]];
[circleView.layer addSublayer:circleLayer];
[self.view addSubview:circleView];
//animate circles
[UIView animateWithDuration:0.8 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[circleView setTransform:CGAffineTransformMakeTranslation(radius/3*cos(M_PI/numberOfCircles*i*2), radius/3*sin(M_PI/numberOfCircles*i*2))];
[circleView setTransform:CGAffineTransformScale(circleView.transform, 0.01, 0.01)];
} completion:^(BOOL finished) {
circleView.hidden = YES;
}];
}
}
#end

CAScrollLayer not working on Video

I am trying to create image animation on Video with the help of CAScrollLayer I am able to add images on CAScrollLayer but it's not scrolling when video is played any help will be appreciable below is my code i have tried so far.
CALayer *layer;
// self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scrollLayer = [CAScrollLayer layer];
scrollLayer.backgroundColor = [[UIColor blackColor] CGColor];
scrollLayer.bounds = parentLayer.bounds;
scrollLayer.contentsRect = CGRectMake(0, 0, scrollLayer.bounds.size.width*images.count, scrollLayer.bounds.size.height);
scrollLayer.borderWidth = 2.5;
scrollLayer.borderColor = [[UIColor redColor] CGColor];
scrollLayer.position = CGPointMake(self.view.center.x, self.view.center.y - 20);
scrollLayer.scrollMode = kCAScrollHorizontally;
scrollLayer.speed = 2.0;
// [self.view.layer addSublayer:scrollLayer];
[parentLayer insertSublayer:scrollLayer atIndex:0];
for(int i=0; i<[images count]; i++) {
UIImage *image = [images objectAtIndex:i];
layer = [CALayer layer];
layer.backgroundColor = [[UIColor blackColor] CGColor];
layer.bounds = CGRectMake(0, 0, 300, 300);
layer.contents = (id)[image CGImage];
layer.position = CGPointMake(layer.bounds.size.width * i, self.view.center.y);
// scrollLayer.speed = 2.0;
[scrollLayer addSublayer:layer];
}
[parentLayer addSublayer:scrollLayer];
[self performSelector:#selector(scrollPoint) withObject:nil afterDelay:2.0];
compostion.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
Thank you.

Scale a circle at same center position

I am working on scaling the circle and keep the same center position, which looks like the pulsing circle on map.
AnimationView.m
#property (nonatomic, strong) UIBezierPath *ovalPath;
- (void)drawCircle
{
UIGraphicsBeginImageContext(self.frame.size);
CGRect rect = CGRectMake(25.45, 49.25, 5.8, 5.8);
self.ovalPath = [UIBezierPath bezierPathWithOvalInRect: rect];
[self.ovalPath fill];
UIGraphicsEndImageContext();
}
- (void)bumpUpCircle
{
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.bounds;
pathLayer.path = self.ovalPath.CGPath;
pathLayer.fillColor = [UIColor blackColor].CGColor;
pathLayer.lineWidth = 2.0f;
pathLayer.lineJoin = kCALineJoinBevel;
[self.layer addSublayer:pathLayer];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"transform.scale"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.0, 0.0, 0)];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(10.0, 10.0, 10.0)];
animation.repeatCount = 1;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.duration = 1;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[pathLayer addAnimation:animation forKey:#"transform.scale"];
NSLog(#"%#",NSStringFromCGPoint(self.bounds.origin));
}
However, the scaling position is in a strange position, rather than the center of the circle.
Any ideas will be very appreciated. Thanks.
Edit:
GIF uploaded
My suggestion for you with a pulsing circle, is to use an UIView and add it as a subview in your UIViewController. In following example you get a pulsing circle (a UIView with a cornerRadius like the radius) scaling up and down in a very few lines. Include this e.g. in your UIViewController's viewDidLoad to try it out.
int radius = 100;
UIView * circleView = [[UIView alloc] init];
circleView.backgroundColor = [UIColor greenColor];
circleView.frame = CGRectMake(0, 0, radius*2, radius*2);
circleView.center = self.view.center;
circleView.layer.cornerRadius = radius;
[self.view addSubview:circleView];
[UIView animateWithDuration:0.8f
delay:0.0f
options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
circleView.transform = CGAffineTransformMakeScale(1.1, 1.1);
} completion:^(BOOL fin) {
// Do nothing
}];

How to put 2 layers to follow same bezierpath without one hiding the other

I have created 2 CALayers of same size and am passing these to the method below. However, the two layers runs together. How can I seperate these so that both are visible?
- (void) myAnimation : (CALayer *) sublayer {
UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(30, 100, 270, 270)];
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:#"position"];
anim.path = aPath.CGPath;
anim.rotationMode = kCAAnimationRotateAuto;
anim.repeatCount = HUGE_VALF;
anim.duration =35.0;
[sublayer addAnimation:anim forKey:#"race"];
}
Your path begins and ends at the same point. Assuming both beginning times are the same and duration is the same, your layers will precisely overlap. You can either shift the beginning time or rotate your UIBezierPath* aPath Here's an example of rotating your UIBezierPath* aPath and altering the duration. It should give you an idea how you could alter duration, begin time, rotation, etc.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
CALayer * layer1 = [CALayer layer];
CALayer * layer2 = [CALayer layer];
[layer1 setFrame:CGRectMake(0, 0, 5, 50)];
[layer2 setFrame:CGRectMake(0, 0, 5, 100)];
layer1.backgroundColor = [[UIColor colorWithRed:.1 green:.5 blue:1 alpha:.35] CGColor];
layer2.backgroundColor = [[UIColor colorWithRed:.9 green:.2 blue:.5 alpha:.35] CGColor];
[self.view.layer addSublayer:layer1];
[self.view.layer addSublayer:layer2];
[self myAnimation:layer1 andRotation:0 andDuration:35.0];
[self myAnimation:layer2 andRotation:10 andDuration:10.0];
}
- (void) myAnimation:(CALayer *)sublayer andRotation:(CGFloat)rot andDuration:(CFTimeInterval)dur {
CGRect rect = CGRectMake(30, 100, 270, 270);
UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:rect];
// Creating a center point around which we will transform the path
CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, center.x, center.y);
transform = CGAffineTransformRotate(transform, rot);
// Recenter the transform
transform = CGAffineTransformTranslate(transform, -center.x, -center.y);
// This is the new path we will use.
CGPathRef rotated = CGPathCreateCopyByTransformingPath(aPath.CGPath, &transform);
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:#"position"];
anim.path = rotated;
anim.rotationMode = kCAAnimationRotateAuto;
anim.repeatCount = HUGE_VALF;
anim.duration =dur;
[sublayer addAnimation:anim forKey:#"race"];
}

CAGradientLayer with CAShapeLayer mask not showing

I have the following code:
- (void)setupLayer {
self.faucetShape = [CAShapeLayer layer];
self.faucetShape.strokeColor = [[UIColor blackColor] CGColor];
self.faucetShape.lineWidth = 2;
self.faucetShape.fillColor = nil;
self.faucetShape.path = [self faucetPath].CGPath; // faucetPath returns a 4 point diamond shaped UIBezierPath*
self.faucet = [CAGradientLayer layer];
self.faucet.mask = self.faucetShape;
self.faucet.colors = #[(id)[UIColor redColor].CGColor, (id)[UIColor redColor].CGColor];
[self.layer addSublayer: self.faucet];
}
But it shows nothing. Not sure what I'm missing (other than my graphic).
(faucet and faucetPath are both properties)
UPDATE
OK, I guess a CAGradientLayer has to have a meaningful frame. I assume a CAShapeLayer's frame isn't as meaningful, since the path can go outside the frame. Adding the following line made it show up:
self.faucet.frame = CGRectMake(0, 0, 64, 64);
So NOW my question is, is there a way to avoid having to do that? I'd just like the frame of the layer to implicitly be that of the containing View/Layer.
You need to set the frame of the layers. The following code will draw a red circle:
- (void)setupLayer
{
CAShapeLayer *faucetShape = [CAShapeLayer layer];
faucetShape.frame = CGRectMake(0, 0, 100, 100);
faucetShape.strokeColor = [[UIColor blackColor] CGColor];
faucetShape.lineWidth = 2;
faucetShape.fillColor = nil;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, nil, CGRectMake(0, 0, 100, 100));
faucetShape.path = path;
CGPathRelease(path);
CAGradientLayer *faucet = [CAGradientLayer layer];
faucet.frame = CGRectMake(10, 10, 100, 100);
faucet.mask = faucetShape;
faucet.colors = #[(id)[UIColor redColor].CGColor, (id)[UIColor redColor].CGColor];
[self.view.layer addSublayer: faucet];
}

Resources