Animating images using CAAnimation - iphone

Is it possible to implement animating sequence of images using CAAnimation? And i dont want to use UIImageView...
I need Something similar to UIImageView where we can set imageView.animationImages and calling startAnimation... but using CAAnimation

CAKeyframeAnimation *animationSequence = [CAKeyframeAnimation animationWithKeyPath: #"contents"];
animationSequence.calculationMode = kCAAnimationLinear;
animationSequence.autoreverses = YES;
animationSequence.duration = kDefaultAnimationDuration;
animationSequence.repeatCount = HUGE_VALF;
NSMutableArray *animationSequenceArray = [[NSMutableArray alloc] init];
for (UIImage *image in self.animationImages)
{
[animationSequenceArray addObject:(id)image.CGImage];
}
animationSequence.values = animationSequenceArray;
[animationSequenceArray release];
[self.layer addAnimation:animationSequence forKey:#"contents"];

Related

How to add Animated Image In Cocos2D without use of TexturePacker?

I am New at iOS Development.
I am also starting to learn Cocos2D.
I've read this tutorial: http://www.raywenderlich.com/tutorials#cocos2d
It is a superb tutorial for beginners, but I'm also interested in animating the image. How can I accomplish animation?
So I read tutorial (describe from Above Link) about how to put animated image with simple Project.
In This tutorial I used TexturePacker and it's working... but I want to know more about how to animate images without using TexturePacker.
Is it possible? If so, then please explain how or link to tutorials on how to make it work.
Thanks in advance.
You can run animation from file.
CCSprite *coin = [CCSprite spriteWithFile:#"MyImage_1.png"];
coin.position = ccp(mS.width*0.5f, mS.height*0.5f);
[self addChild:coin z:2];
{
NSString *animationName = #"UNIQUE_ANIMATION_NAME";
CCAnimation* animation = nil;
animation = [[CCAnimationCache sharedAnimationCache] animationByName:animationName];
if(!animation)
{
NSMutableArray *animFrames = [NSMutableArray array];
for( int i=1;i<=5;i++)
{
NSString* path = [NSString stringWithFormat:#"MyImage_%d.png", i];
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:path];
CGSize texSize = tex.contentSize;
CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:tex rect:texRect];
[animFrames addObject:frame];
}
animation = [CCAnimation animationWithSpriteFrames:animFrames];
animation.delayPerUnit = 0.175f;
animation.restoreOriginalFrame = YES;
[[CCAnimationCache sharedAnimationCache] addAnimation:animation name:animationName];
}
if(animation)
{
CCAnimate *animAction = [CCAnimate actionWithAnimation:animation];
[coin runAction:animAction];
}
}

Access Method After UIImageView Animation Finish

I have an array of images loaded into a UIImageView that I am animating through one cycle. After the images have been displayed, I would like a #selector to be called in order to dismiss the current view controller. The images are animated fine with this code:
NSArray * imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"HowTo1.png"],
[UIImage imageNamed:#"HowTo2.png"],
nil];
UIImageView * instructions = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
instructions.animationImages = imageArray;
[imageArray release];
instructions.animationDuration = 16.0;
instructions.animationRepeatCount = 1;
instructions.contentMode = UIViewContentModeBottomLeft;
[instructions startAnimating];
[self.view addSubview:instructions];
[instructions release];
After the 16 seconds, I would like a method to be called. I looked into the UIView class method setAnimationDidStopSelector: but I cannot get it to work with my current animation implementation. Any suggestions?
Thanks.
Use performSelector:withObject:afterDelay::
[self performSelector:#selector(animationDidFinish:) withObject:nil
afterDelay:instructions.animationDuration];
or use dispatch_after:
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, instructions.animationDuration * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self animationDidFinish];
});
You can simple use this category UIImageView-AnimationCompletionBlock
And for Swift version: UIImageView-AnimationImagesCompletionBlock
Take a look on ReadMe file in this class..
Add UIImageView+AnimationCompletion.h and UIImageView+AnimationCompletion.m files in project and them import UIImageView+AnimationCompletion.h file where you want to use this..
For eg.
NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
for(int i = 10001 ; i<= 10010 ; i++)
[imagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",i]]];
self.animatedImageView.animationImages = imagesArray;
self.animatedImageView.animationDuration = 2.0f;
self.animatedImageView.animationRepeatCount = 3;
[self.animatedImageView startAnimatingWithCompletionBlock:^(BOOL success){
NSLog(#"Animation Completed",);}];
There is no way to do this precisely with UIImageView. Using a delay will work most of the time, but there can be some lag after startAnimating is called before the animation happens on screen so its not precise. Rather than using UIImageView for this animation try using a CAKeyframeAnimation which will call a delegate method when the animation is finished. Something along these lines:
NSArray * imageArray = [[NSArray alloc] initWithObjects:
(id)[UIImage imageNamed:#"HowTo1.png"].CGImage,
(id)[UIImage imageNamed:#"HowTo2.png"].CGImage,
nil];
UIImageView * instructions = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//create animation
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
[anim setKeyPath:#"contents"];
[anim setValues:imageArray];
[anim setRepeatCount:1];
[anim setDuration:1];
anim.delegate = self; // delegate animationDidStop method will be called
CALayer *myLayer = instructions.layer;
[myLayer addAnimation:anim forKey:nil];
Then just implement the animationDidStop delegate method
With this method you avoid timers to fire while the imageView is still animating due to slow image loading.
You could use both performSelector: withObject: afterDelay: and GCD in this way:
[self performSelector:#selector(didFinishAnimatingImageView:)
withObject:imageView
afterDelay:imageView.animationDuration];
/!\ self.imageView.animationDuration has to bet configured before startAnimating, otherwise it will be 0
Than if -(void)didFinishAnimatingImageView:create a background queue, perform a check on the isAnimating property, than execute the rest on the main queue
- (void)didFinishAnimatingImageView:(UIImageView*)imageView
{
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.yourcompany.yourapp.checkDidFinishAnimatingImageView", 0);
dispatch_async(backgroundQueue, ^{
while (self.imageView.isAnimating)
NSLog(#"Is animating... Waiting...");
dispatch_async(dispatch_get_main_queue(), ^{
/* All the rest... */
});
});
}
Created Swift Version from GurPreet_Singh Answer (UIImageView+AnimationCompletion)
I wasn't able to create a extension for UIImageView but I believe this is simple enough to use.
class AnimatedImageView: UIImageView, CAAnimationDelegate {
var completion: ((_ completed: Bool) -> Void)?
func startAnimate(completion: ((_ completed: Bool) -> Void)?) {
self.completion = completion
if let animationImages = animationImages {
let cgImages = animationImages.map({ $0.cgImage as AnyObject })
let animation = CAKeyframeAnimation(keyPath: "contents")
animation.values = cgImages
animation.repeatCount = Float(self.animationRepeatCount)
animation.duration = self.animationDuration
animation.delegate = self
self.layer.add(animation, forKey: nil)
} else {
self.completion?(false)
}
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
completion?(flag)
}
}
let imageView = AnimatedImageView(frame: CGRect(x: 50, y: 50, width: 200, height: 135))
imageView.image = images.first
imageView.animationImages = images
imageView.animationDuration = 2
imageView.animationRepeatCount = 1
view.addSubview(imageView)
imageView.startAnimate { (completed) in
print("animation is done \(completed)")
imageView.image = images.last
}
You can create a timer to fire after the duration of your animation. The callback could process your logic you want to execute after the animation finishes. In your callback be sure check the status of the animation [UIImageView isAnimating] just in case you want more time to let the animation finish.
in ImageView.m
- (void) startAnimatingWithCompleted:(void (^)(void))completedHandler {
if (!self.isAnimating) {
[self startAnimating];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, self.animationDuration * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(),completedHandler);
}}

Can I add UIImageView inside custom UIAlertView?

I already have a custom UIAlertView and in which i want to add UIImageView for adding custom UIAlertView.
I have already added UIImageView with array and start animating it but it doesn't work. If you want, I can post the code.
Please help me ASAP. I'm stuck :(
Here is code
- (id)initWithImage:(UIImage *)image text:(NSString *)text
{
//CGSize imageSize = self.backgroundImage.size;
CGRect frame = [[UIScreen mainScreen] bounds];
width = frame.size.width;
height = frame.size.height;
if (self = [super init])
{
loadtext = [[UILabel alloc]initWithFrame:CGRectZero];
loadtext.textColor = [UIColor blackColor];
loadtext.backgroundColor = [UIColor whiteColor];
[self addSubview:loadtext];
//Create the first status image and the indicator view
UIImage *statusImage = [UIImage imageNamed:#"status1.png"];
activityImageView = [[UIImageView alloc]
initWithImage:statusImage];
//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"status1.png"],
[UIImage imageNamed:#"status2.png"],
[UIImage imageNamed:#"status3.png"],
[UIImage imageNamed:#"status4.png"],
[UIImage imageNamed:#"status5.png"],
[UIImage imageNamed:#"status6.png"],
[UIImage imageNamed:#"status7.png"],
nil];
//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 1.0;
//Position the activity image view somewhere in
//the middle of your current view
activityImageView.frame = CGRectZero;
//Start the animation
[activityImageView startAnimating];
//Add your custom activity indicator to your current view
[self addSubview:activityImageView];
//self.backgroundImage = image;
}
return self;
}
- (void) layoutSubviews{
//lblUserName.transform = CGAffineTransformMake;
//[lblUserName sizeToFit];
CGRect textRect = activityImageView.frame;
textRect.origin.x = (CGRectGetWidth(self.bounds) - CGRectGetWidth(textRect))/2;
textRect.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(textRect))/2;
textRect.origin.x -= 100.0;
textRect.origin.y -= 60.0;
textRect.size.height = 30;
textRect.size.width = self.bounds.size.width - 50;
activityImageView.frame = textRect;
}
This code started working. But now this UIImageView is taking my whole screen and coming in front of UIAlertView. Though i give 10px height and width. It shows same. Can anyone help please?
Thanks,
Anks
Yes, you can add image view - in custom alert-view. instead of taking as UIView custom class, need to take UIImage-View & you can use image-view properties.
Change activityImageView.frame = CGRectZero;
to some custom frame.
eg:
activityImageView.frame = CGRectMake(0,0,100,100);

UIImage view animation problem

I have done the effect of playing lots of images like below:
NSInteger faceNum = 12;
NSMutableArray *faceArray = [[NSMutableArray alloc] initWithCapacity:faceNum];
for (int i = 1;i<faceNum+1; i++) {
NSString *facename = [[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:#"animationFace%d",i] ofType:#"png"];
UIImage *faceImage = [UIImage imageWithContentsOfFile:facename];
[faceArray addObject:faceImage];
}
UIImageView *faceView = [[UIImageView alloc]
initWithFrame:CGRectMake(414, 157, 161, 124)];
faceView.animationImages = faceArray;
faceView.animationDuration = 30;
faceView.animationRepeatCount = 0;
[faceView startAnimating];
[self.view addSubview:faceView];
[faceView release];
[faceArray release];
And how to add the EaseInEaseOut effect to this.One picture disappear gradually,then another picture appear gradually.
Thanks
There is no inbuilt fade-in and out feature with imageview startAnimating. You can achieve this by manually setting the alpha of two view laid over each other:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
imageviewToFadeOut.alpha = 0.0f;
imageviewToFadeIn.alpha = 1.0f;
[UIView commitAnimations];
and setImage manually alternatively to these UIViews.
[imageview setImage:image];
and to recursively call this method, use something like [self performSelector:#selector(methodname) withObject: afterDelay: ] inside the method itself.
Edit: For clarity, specifying the recursion to call this method over and over with delay:
-(void)methodname {
.. (do your task)
[self performSelector:#selector(methodname) withObject:NULL afterDelay:10 ];
}

Save trail of animated image

I have a car image in a UIImageView, with movement and rotation animations.
I have a tyre-mark image in a UIImageView, which i've added as a subview to the car.
This means that all the same movement and rotation animations apply to both.
What I want to do is leave a trail of tyre skidmarks.
Can anyone suggest a strategy on how to do this?
Searching through other topics I saw this snippet, not sure if I can use it:
UIGraphicsBeginImageContext(drawingView.bounds.size);
[drawingView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//then display viewImage in another UIImageView...
If it were usable, any clue on how to get it called during an animation?
That snippet is not quite what you're looking for. That snippet saves what is currently displayed in the context as a UIImage. You can use the ImageContext to draw as well, but not like that.
The ideal would be if CAKeyframeAnimation notified its delegate before doing the following keyframe; since I don't think that's possible (?) the only way I can think of doing something like this is using an array of positions, and using consecutive CABasicAnimation instances in order to do this. This is like a "poor man's" CAKeyframeAnimation.
Something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
_step = 0;
_positions = [[NSArray alloc] initWithObjects:[NSValue valueWithCGPoint:CGPointMake(20.0, 20.0)],
[NSValue valueWithCGPoint:CGPointMake(40.0, 80.0)],
[NSValue valueWithCGPoint:CGPointMake(60.0, 120.0)],
[NSValue valueWithCGPoint:CGPointMake(80.0, 160.0)],
[NSValue valueWithCGPoint:CGPointMake(100.0, 200.0)],
[NSValue valueWithCGPoint:CGPointMake(120.0, 240.0)],
[NSValue valueWithCGPoint:CGPointMake(140.0, 280.0)],
[NSValue valueWithCGPoint:CGPointMake(160.0, 320.0)],
[NSValue valueWithCGPoint:CGPointMake(180.0, 360.0)],
[NSValue valueWithCGPoint:CGPointMake(200.0, 400.0)],
nil];
[self moveToNextPosition];
}
- (void)moveToNextPosition
{
if (_step < [_positions count] - 1)
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"position"];
animation.fromValue = [_positions objectAtIndex:_step];
animation.toValue = [_positions objectAtIndex:(_step + 1)];
animation.delegate = self;
animation.removedOnCompletion = YES;
[_sprite.layer addAnimation:animation forKey:#"position"];
++_step;
}
else
{
_sprite.center = [[_positions objectAtIndex:_step] CGPointValue];
}
}
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
UIImageView *trail = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sprite.png"]];
trail.center = [[_positions objectAtIndex:_step] CGPointValue];
[self.view insertSubview:trail belowSubview:_sprite];
[trail release];
[self moveToNextPosition];
}
In this case, the animations execute one after the other, with values specified in the _positions NSArray ivar, and the _step is incremented at every step. When each animation stops, we draw a sprite image below the one we're animating, and we restart our animation, until there are no more points to move to. And then we finish.
Hope this helps!