Xcode/iPad animateWithDuration background/foreground - xcode

I have a looped animation in a UIViewController which is called in the viewDidAppear function:
[UIView animateWithDuration:8.0 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
_introImage.center = CGPointMake(INTRO_IMAGE_X_END, INTRO_IMAGE_Y);
}
completion:^(BOOL finished){
_introImage.center = CGPointMake(INTRO_IMAGE_X_START, INTRO_IMAGE_Y);
}];
When I put the app into the background and bring it back to the foreground the animation stops. Why is this and what could I do to restart the animation?

You could put it in it's own method and call that method from viewDidAppear, and also in applicationDidBecomeActive, which is in your AppDelegate.m
-(void)animate {
[UIView animateWithDuration:8.0 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
_introImage.center = CGPointMake(INTRO_IMAGE_X_END, INTRO_IMAGE_Y);
}
completion:^(BOOL finished){
_introImage.center = CGPointMake(INTRO_IMAGE_X_START, INTRO_IMAGE_Y);
}];
}
In order to fire the method from "viewDidAppear" call the method with a single line , like this:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self animate];
}
And to fire the animation from the app delegate when the app becomes active:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self.viewcontroller animate];
}
This is all assuming you might want to use viewDidAppear for other things as well. Separating the animation into it's own method lets you call it exclusively, instead of running everything inside of viewDidAppear.

Related

How to stop animation of [UIView animateKeyframesWithDuration] with UIViewKeyframeAnimationOptionRepeat option

I want to add a heartbeat effect to my view , one way to do is use UIView's animation method as follow :
- (void)heartBeating
{
UIView *panel;
NSTimeInterval during = 0.8;
[UIView animateKeyframesWithDuration:during delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeLinear|UIViewKeyframeAnimationOptionRepeat
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.4 animations:^{
panel.transform = CGAffineTransformMakeScale(1.2, 1.2);
}];
[UIView addKeyframeWithRelativeStartTime:0.4 relativeDuration:0.6 animations:^{
panel.transform =CGAffineTransformIdentity ;
}];
} completion:^(BOOL finished) {
}];
}
the problem is : if i want to stop the animation in an action , for example , a Stop butotn tapped , how I can do .
I know I can realize the same effect by create CAKeyFrameAnimation and add it to the view' CALayer .But I want to know how to do with the UIView's animation method. Thanks.
Try
[panel1.layer removeAllAnimations];
Hope this will help you.

Display Image To Extend Splash Screen Using Storyboards in iPhone SDK

I have this app that has tabs and each tab's root view controller is a navigation controller. I want to display my splash screen or launch image while the app is busy parsing JSON and loading it up in my core data stack and remove it from superview using a fade out animation. I tried this code to make it work but I'm stuck since the image is only displayed on the view inside the navigation controller. What I wanted is to view it full screen with the status bar on top like what you see when launching an app.
_launchImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
if (kFourInchDevice) {
_launchImageView.image = [UIImage imageNamed:#"Default-568h#2x.png"];
}
[self.view addSubview:_launchImageView];
[self.view bringSubviewToFront:_launchImageView];
And this is my code when dismissing it:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(launchImageFadeAnimationDidFinished)];
_launchImageView.alpha = 0.0;
[UIView commitAnimations];
- (void)launchImageFadeAnimationDidFinished
{
[_launchImageView removeFromSuperview];
}
Any ideas on how to do this? Thanks!
I think you are having problem of hiding NavBar in splash screen isnt it ?
Just Add one extra ViewController in your story board.
Then under Utilities ( left side bar ) under Attribute Tab - View Controller make VC initial View Controller.
Call following in your View Did Load
- (void) hideNavBar {
UINavigationBar *navBar = self.navigationController.navigationBar;
navBar.hidden = TRUE;
}
Then when you you finish your json parser -
#pragma mark - Delegates
#pragma mark JSON Request
-(void) connectionReady; {
NextVC *viewController = [self.storyboard
instantiateViewControllerWithIdentifier:#"NextVC"];
[self.navigationController pushViewController:viewController
animated:YES];
}
Then finally in NextVC
- (void) showNavBar {
UINavigationBar *navBar = self.navigationController.navigationBar;
navBar.hidden = FALSE;
}

fade in an image in xcode automatically

Hello I am able to fade an image out automatically. How would I manage to fade the image IN automatically.
Here is my code.
.h
#property (assign) IBOutlet UIImageView *cbizFadeImage;
.m
- (void)viewDidLoad {
//Fade Image Out
CGRect aboutNicheImageFrame = cbizFadeImage.frame;
aboutNicheImageFrame.origin.y = self.view.bounds.size.height;
// iOS4+
[UIView animateWithDuration:0.5
delay:2.0
options: UIViewAnimationCurveEaseOut
animations:^{
cbizFadeImage.alpha = 0;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
}
Ok I was able to fade an image in by changing the CurveEaseOut to CurveEaseIn. However now I would like to fade the image out. So the image would first ease in then It would then ease out. Would I use an if statement?
I was able to use the UIViewAnimationCurveEaseInOut.
Instead of UIViewAnimationCurveEaseInOut, use this for options:
options:UIViewAnimationOptionCurveEaseOut
I think what your really asking for is UIViewKeyframeAnimationOptionAutoreverse Used like this...
Image.alpha = 0;
[UIView animateKeyframesWithDuration:5.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.9 relativeDuration:0.0 animations:^{
Image.alpha = 1;
}];
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{
Image.alpha = 0;
}];
} completion:nil];

What is the best way to wait for a loop of UIView animations to finish?

I'm trying to loop over multiple UIViews and perform animation on each, but I'd like to know when all of the animations have finished. What is the best way to call a function when the loop of animations has finished? Alternately, is there a way to wait until all have finished?
I tried using setAnimationDidStopSelector, but it doesn't fire. In this case, I'm clearing some "chips" off a game board by having them fade out then get removed (NumberedChipView is a subclass of UIView). Play cannot continue until the chips are off the board.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop)];
// clear chips
for (HexagonalTile *aTile in tilesToClear) {
NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];
[UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationCurveLinear animations:^{
chipView.alpha = 0.0;
}
completion:^(BOOL finished){
if (finished) {
[chipView removeFromSuperview];
[self.chipViews removeObject:chipView];
}
}];
}
[UIView commitAnimations];
I also tried CATransaction to no avail:
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[self animationFinished];
}];
// clear chips
for (HexagonalTile *aTile in tilesToClear) {
NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];
[UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationCurveLinear animations:^{
chipView.alpha = 0.0;
}
completion:^(BOOL finished){
if (finished) {
[chipView removeFromSuperview];
[self.chipViews removeObject:chipView];
//NSLog(#"clearing finished");
}
}];
}
[CATransaction commit];
Update:
I now can get the selector to fire, however it doesn't wait for the loop of animations to finish.
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:)];
Your for loop is firing off all the animations with the same delay and same duration, so they will all start and finish at the same time.
Your completion method is going to be called once for each of your animations.
You could rewrite your code slightly to set a counter to zero before entering the loop, and increment it after each step. When the index is less than the array count, pass a nil completion block. Once the index == the array count, you're on the last item, so pass in a completion block with the code that you want to invoke once the last animation completes.
Note that you can use this index approach to make each animation start at a different time, by increasing the delay amount based on the index value:
delay = .3 + (0.5 * index);
Here's what ended up working for me. I realized that the UIView animateWithDuration methods weren't being associated to the begin/commit section. Rather, the begin/commit sections mark a region of "implicit" animation operations, so I could just set the properties within the animation and it would be implicitly animated.
For example, within the animation below, I simply set the alpha property of the chipView to "0.0" and the chipView (a UIView) is implicitly animated to disappear.
[UIView beginAnimations:#"tilescleared" context:nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDelay:0.3];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
for (HexagonalTile *aTile in tilesToClear) {
NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];
chipView.alpha = 0.0;
}
[UIView commitAnimations];
Then, in my animationDidStop method, I do the "cleanup" and remove and chips from the superview. This animationDidStop method only gets fired when all chipView animations from the loop have finished, which is the tricky thing I was trying to figure out.
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([animationID isEqualToString:#"tilescleared"]
&& [finished boolValue]) {
for (HexagonalTile *aTile in self.animationInfo.tilesToClear) {
NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];
[chipView removeFromSuperview];
[self.chipViews removeObject:chipView];
}
}
}

xCode ios5: How to fade out a label text after an interval?

I have a little iOS5 app that shows images. I click on the image to show its information. I'd like the information to fade away after a few seconds. Is there a good method to do this?
I can always implement another button action but this would be neater..
Thanks!
Use either NSTimer or performSelector:withObject:afterDelay. Both methods require you to call a separate method that will actually do the fading out which should be fairly straightforward.
Example:
NSTimer
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(fadeOutLabels:) userInfo:nil repeats:NO];
performSelector:withObject:afterDelay:
/* starts the animation after 3 seconds */
[self performSelector:#selector(fadeOutLabels) withObject:nil afterDelay:3.0f];
And you will call the method fadeOutLabels (or whatever you want to call it)
-(void)fadeOutLabels
{
[UIView animateWithDuration:1.0
delay:0.0 /* do not add a delay because we will use performSelector. */
options:UIViewAnimationCurveEaseInOut
animations:^ {
myLabel1.alpha = 0.0;
myLabel2.alpha = 0.0;
}
completion:^(BOOL finished) {
[myLabel1 removeFromSuperview];
[myLabel2 removeFromSuperview];
}];
}
Or you can use the animation block to do all of the work:
-(void)fadeOutLabels
{
[UIView animateWithDuration:1.0
delay:3.0 /* starts the animation after 3 seconds */
options:UIViewAnimationCurveEaseInOut
animations:^ {
myLabel1.alpha = 0.0;
myLabel2.alpha = 0.0;
}
completion:^(BOOL finished) {
[myLabel1 removeFromSuperview];
[myLabel2 removeFromSuperview];
}];
}

Resources