I have a placeholder UIImageView (loaded from a .xib) which I want to load with a picture of the back of a card and then flip to the front image.
[cardView setImage:backCardImage];
[UIView transitionWithView:cardView
duration:1
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[cardView setImage:frontCardImage];
}
completion:NULL];
The problem is that the initial image it starts with is not backCardImage, instead, it uses the no image (i.e. blank image) from my .xib file. If I had the backCardImage loaded in the .xib and only call transitionWithView, I believe it would work properly, but I would really like to have the two calls (setImage, and transitionWithView) in the same method. Is there any way to do this? Do I need to call something between the two?
I tried using UIViewAnimationOptionBeginFromCurrentState, but that doesn't work either.
The cardView won't draw, even if you put in setNeedsDisplay, until the next drawing cycle, so you need to set that image in another method first, or move the animation to another method, and call it with performSelector:withObject:afterDelay:. Even a delay of 0 will work. I don't think the animation looks very good though, because you only see the back image for an instant before the flip (a delay of .1 or .2 seconds looks better I think). Why not set the back image earlier? What's the purpose of a placeholder image in this context?
Related
So the problem is: i have a gif background that was set through a method of extension created by this guy SwiftGif Link
I use method backgroundAnimated.loadGif(name: "giphy") to load current gif and it parses gif onto images then fills it onto arrays with consequent call of animatedImage method of UIImage class.
But the thing is - i want to preserve a state of looping gif when transiting to next ViewController through segue.
For example if i have bird flying over the centre of the screen and then randomly someone presses a button to go to next ViewController, gif resets and begins from the beginning. Help me to find a solution please
I thought of solutions:
Pass coordinates of frame onto other ViewController through prepareForSegue
Problem is - myBackground.frame is always at zero coordinates , so i have no idea how to get the frame it was stopped on last
Start with CGRect initial frame position
Problem is - its not possible to guess position of frame it has stopped when transition button is pressed.
Make one gif background for all ViewControllers (Have no idea how to implement such thing whatsoever)
Do you sure that your myBackground UIImageView is changing frame. I don't think so.
I guess its your gif is changing its key frame. So you need to store the index of the stopped key frame and start your gif from the key frame instead.
I have several images that I want to move sequentially, across the screen, each with a specific delay. I have animated the first image like this:
[UIView animateWithDuration:0.5f
animations:^{
image1.center = CGPointMake(image1.center.x + 10.0f, image1.center.y);
}
completion:nil];
Now I want to move image2 behind image 1. Do I have to add a delay for image2? I mean, I want image2 to follow image1 in moving across the screen. How do I do this?
P.S: The above code moves image1 from left to right, across the screen.
Put the animation method (
animateWithDuration:delay:options:animations:completion:) in the completion block of the first animation. Add whatever delay you want (the delay will be between the end of the first animation and the start of the second).
Anybody have any idea how this is done?
http://youtu.be/r_cII4_aq_A
In general, the idea is awesome, but I would like to know how to do each specific thing. In the video, the pages are being swiped too fast so you can't see, but as you transition from one page to the next, each pixel smoothly transitions to it's new color. Also, it's really cool how the icon gets smaller to a minimum size as you transition away from a screen.
Maybe there's some 3rd party library that provides a protocol and it's relatively easy to implement, but I can't find it. If there's not, I'm thinking it's just one view controller with many views side by side and as you drag your finger, it calculates where each view needs to be..and what color every pixel needs to be.
I imagine you already have it by now. But in case you still have any doubts, I made gist that does the animation: https://gist.github.com/mnmaraes/9536364
Anyways, have fun.
It's a fade animation, so:
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
_backgroundImageView.image = nextImage;
} completion:nil];
should do the trick for the background part.
For the foreground/icon part you can animate the transform to scale and translate the views as they slide in and out.
I am developing an application which needs to decrease the width of imageview to 0 from 320 in a 1hour time. for this i am using following code
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3600];
newRect.size.width = 0;
imgView.frame = newRect;
[UIView commitAnimations];
it is works fine if we stay in same view controller,after animation starts if the user navigate to some other viewcontoller, How to handle the animation.
Along with this i have one more issue- during the animation the image in imageview is looks like shrink as throughout the animation i am using same image ,So i want to change the image in imageView during the animation How can i achieve this.
Is there any other way to do this apart form the animation.
Any Help is Appreciated.
The life cycle of the controllers and views will not allow you to do this so simply. The objects you are using can and will be deallocated by the system if they are not currently needed anymore, so the animation you started is essentially discarded with it.
You will have to store the progress of your animation somewhere, e. g. in a file or a CoreData database to have it persistent across the instantiations of that view. Depending on the exact situation, it might be sufficient to store the start time of the animation once it begins. Then, in viewWillAppear you could load it and calculate how much progress into that one hour has been made and start a new animation from that point. To the user it would appear as if the animation had proceeded in the background.
I have a question regarding Xcode.
I found a tutorial on the net that allowed me to move an image around the screen.
Could you please explain how I can make my button move to the left and back to right with one click (I can't find this anywhere...), and immediately after that run another IBAction that allows me to switch to another subview? (I already have this code naturally...). I tried to add both IBActions in 1 centralized one, but it didn't seem to work :-( It opens in this case immediately the subview without showing me the animation.
What I tried:
The code obtained until now:
-(IBAction) aMove: (id) sender{
if (bMove == NO) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
btnTarget.transform = CGAffineTransformMakeTranslation(-30,0);
[UIView commitAnimations];
bMove = YES;
}else{
btnTarget.transform = CGAffineTransformIdentity;
bMove = NO;
}
}
-(IBAction) aAnimateActivate: (id) sender {
[self aMove:nil];
[self targetOpenView:nil]; //Opens the subview
}
I appreciate your help! Thanks!
concatenate works to combine animations and I just put two blocks of animations (two of the one shown below) after each other... :
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
CGAffineTransform transform = CGAffineTransformConcat(CGAffineTransformMakeScale(1, 1),CGAffineTransformMakeTranslation(10, -50));
btnGuide.transform = transform;
[btnGuide setAlpha:0.0];
[UIView commitAnimations];
Basically implementing this code did the trick... Easy!
However... I needed some additional pieces of code for the further implementation such as:
[self performSelector:#selector(aGuide) withObject:self afterDelay:0.0];
(don't mind the selector name)
and:
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(targetOpenView:) userInfo:nil repeats:NO];
Without looking at all of your program flow nor doing any actual testing (and I find when using Core Animation that the only way to be sure it works right is to code it and see if it does) the problem with the subview opening right away occurs because right after you invoke your "aMove" method to set up the first animation the thread continues with the next line of code, i.e., the [self] targetOpenView:nil statement, which immediately opens the subview and thus doesn't allow the first animation sequence to be shown. There is no pause to wait for the first animation to be completed. The animation, once commited, runs on its own thread while your code continues to run on the current thread (probably the application's main thread). That might not seem to be the most sensible way but you have to think of the code you write as the process to set up an animation that, once committed, is a separate entity that is free to run on its own (beside your code). The advantage to Apple's implementation is that you can set up a whole bunch of different animations which occur at the same time. One of Core Animation's design goals is to take away the need for the programmer to be handling all the starting and stopping of various animations and instead let the animation coordination be done using various methods of delay and duration or providing the means for one animations events to be observed (and acted upon) by other animations.
In order to do the animation(s) the way you want you will need use a method which only allows the second animation to begin once the first is over. One way (assuming that the subview change would be set up as an animation itself) is to use a completion: handler, an animation block that only begins upon completion of the first animation. Another way is to let the two animations "start together" but include a delay: parameter in the second animation that is equal to the length of the first animation. If the subview change is not done with an animation but is just done with code in the main thread then you need to set up a an animation delegate that is called when certain events occur in your animation, one of which is it's completion. Parameter(s) are passed to your delegate to tell you what is occurring and to which animation.
All of this is discussed, with examples, in the Animations section of the View Programming Guide to iOS (about 10 pages that will probably show you almost exactly how to do what you want):
http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html#//apple_ref/doc/uid/TP40009503-CH6
Also, in order to set up the first animation to move the button somewhere and back again you might want to read the subtopic in that same section of the guide mentioned above: Implementing Animations That Reverse Themselves. I think it would be the cleanest way to do what you want.
(FYI, I'm better with the MacOS side of Core Animation than the iOS side but the "why did this happen immediately?" problem you have is a common pitfall when getting up to speed with how it works. Hope this explanation helps.)