UIImageview is not animating in iOS7 but working good in iOS6 - iphone

revealImgView.animationImages =[NSArray arrayWithObjects:[UIImage imageNamed:#"reveal1.png"],[UIImage imageNamed:#"reveal2.png"],[UIImage imageNamed:#"reveal3.png"],[UIImage imageNamed:#"reveal4.png"],[UIImage imageNamed:#"reveal5.png"],nil];
revealImgView.animationDuration=1.5;
revealImgView.animationRepeatCount=INFINITY;
[revealImgView startAnimating];
This is working good in iOS 6 bt not working in iOS 7. i have made the UIImageView in nib

It should work and if not call the method after some delay,
double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self getAllImgViewAnimating];
});
-(void)getAllImgViewAnimating
{
revealImgView.animationImages =[NSArray arrayWithObjects:[UIImage imageNamed:#"reveal1.png"],[UIImage imageNamed:#"reveal2.png"],[UIImage imageNamed:#"reveal3.png"],[UIImage imageNamed:#"reveal4.png"],[UIImage imageNamed:#"reveal5.png"],nil];
revealImgView.animationDuration=1.5;
revealImgView.animationRepeatCount=INFINITY;
[revealImgView startAnimating];
}

Related

UIImageView change Background Color and setImage not working during Animation

As the title said, it's very strange, did you agree?
So , if someone found your codes
imageView.backgroundColor = [UIColor greenColor];
or
imageView.image = [UIImage imageName:#"angryBird"];"
are not working. Just mind that if your imageView is animating , or whether the animation has been removed or not.
----------The Answer Below---------------
Just stop animating before you set image and change background of the animating UIImageView.
UIImageView* imageView = [UIImageView alloc] init];
//......do sth. like setFrame ...
imageView.images = [NSArray arrayWithObjects....]; // set the animation images array
imageView.animationDuration = 1.0;
[imageView startAnimating];
//......do sth.
[imageView stopAnimating]; // **** do not forget this!!!!! ****
imageView.backgroundColor = [UIColor greenColor];
imageView.image = [UIImage imageName:#"angryBird"];
When you performSelector: withObject: afterDey:1.0s , in the selector method , also, you need to stopAnimating too, and then setImage or change background color.
Try to change background color and changing the image once animation is complete. it will simply work for you.
Try this:
[UIView animateWithDuration:1.0 animations:^(){} completion:^(BOOL finished){}];
Try this,Animation using block
[UIView animateWithDuration:1.0 animations:^{
}
completion:^(BOOL finished)
{
[imageView setImage:[UIImage imageNamed:#"YOUR_IMAGE_NAME"]];
[imageView setBackgroundColor:[UIColor greenColor]];
}];
Note: It is imageNamed: not imageName: ,I am not sure how this get compiled
Try this code and performSelector:withObject:afterDelay:: in this way
[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];
});
i hope it helps you

pause catransition and resume

I used the code below to make transition
CATransition *transition = [CATransition animation];
transition.duration = duration ;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
transitioning = YES;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
fromUIView.hidden = YES;
toUIView.hidden = NO;
UIImageView *tmp = toUIView;
toUIView = fromUIView;
toUIView = tmp;
but I hope to pause at transition progress 0.3 to do something, then resume the transition to progress 0.6 do something and resume.
is it possible ?
I've never worked with CATransition before, but I think you can do this with NSTimer, GCD, or performSelector:withObject:afterDelay:.
For instance:
double delayInSeconds = 0.3;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self doSomething];
});
where doSomething is
-(void)doSomething{
[self pauseLayer:thisLayer];
//do something
[self resumeLayer:thisLayer];
}

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);
}}

core motion moving two things with the accerlerometer

I'm trying to move two buttons around using the accelerometer in xcode. I have the first button working, when I call startDrifting, but when I call startDrifting2 for the second button, nothing is happening with the second button. I tried calling startAccelerometerUpdatesToQueue for the startDrifting2, but then the first one stopped working. any ideas?
-(void) startDrifting:(UIButton *) button
{
[self.motionManager startAccelerometerUpdatesToQueue:[[[NSOperationQueue alloc] init] autorelease] withHandler:^(CMAccelerometerData *data, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
CGRect labelFrame = button.frame;
labelFrame.origin.x += data.acceleration.x * MOTION_SCALE;
if (!CGRectContainsRect(self.view.bounds, labelFrame))
labelFrame.origin.x = button.frame.origin.x;
labelFrame.origin.y -= data.acceleration.y * MOTION_SCALE;
if (!CGRectContainsRect(self.view.bounds, labelFrame))
labelFrame.origin.y = button.frame.origin.y;
button.frame = labelFrame;
} );
}];
}
-(void) startDrifting2:(UIButton *) button
{
CMAccelerometerData *data = [self.motionManager accelerometerData];
dispatch_async(dispatch_get_main_queue(), ^{
CGRect labelFrame = button.frame;
labelFrame.origin.x += data.acceleration.x * MOTION_SCALE * 1;
if (!CGRectContainsRect(self.view.bounds, labelFrame))
labelFrame.origin.x = button.frame.origin.x;
labelFrame.origin.y += data.acceleration.y * MOTION_SCALE * 1;
if (!CGRectContainsRect(self.view.bounds, labelFrame))
labelFrame.origin.y = button.frame.origin.y;
button.frame = labelFrame;
});
}
Try setting the delegate to itself and then give it back to the main delegate of the app. When you hit the next button try and reassign the delegate back in the other method.

cocos2d problem - layer not updating quick enough

i am adding overlays to a cocos2d layer in preparation for a screenshot...
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCScene* currentScene = [[CCDirector sharedDirector] runningScene];
GameLayer* topLayer = (GameLayer *)[currentScene.children objectAtIndex:0];
CCSprite *middleBackground = [CCSprite spriteWithFile:#"mid_bg.png"];
middleBackground.position = ccp(winSize.width * 0.5,winSize.height * 0.55);
[topLayer addChild:middleBackground z:3 tag:111];
CCSprite *bottomBackground = [CCSprite spriteWithFile:#"bot_bg.png"];
bottomBackground.position = ccp(winSize.width * 0.5,winSize.height * 0.1);
[topLayer addChild:bottomBackground z:3 tag:112];
CCSprite *topBackground = [CCSprite spriteWithFile:#"top_bg.png"];
topBackground.position = ccp(winSize.width * 0.5,winSize.height * 0.94);
[topLayer addChild:topBackground z:3 tag:113];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
CCSprite *topBackground2 = [CCSprite spriteWithFile:#"statusCover2.png"];
topBackground2.position = ccp(winSize.width * 0.5,winSize.height * 0.992);
[topLayer addChild:topBackground2 z:3 tag:114];
[[topLayer popoverController] dismissPopoverAnimated:YES];
then
[UIImage imageWithCGImage:UIGetScreenImage()];
However the later is not taking the overlays into account. It is taking a screenshot of the layer before the overlays are added.
Is there anyway I can tell the cocos2d scene/layer to update the frame NOW?
Because cocos2d is asynchronous you can't tell it to do something like that. However, you can schedule a callback to happen in two seconds and have that callback do the screen capture.
[self schedule: #selector(screenshot:) interval:2.0];
then define the selector like this:
-(void) screenshot:(id)sender {
[UIImage imageWithCGImage:UIGetScreenImage()];
[self unschedule:#selector(screenshot:)];
}
don't forgot to unschedule it.