I wrote the following code :
Code:
[MyImage setHighlighted:true];
Sleep(1);
[MyImage setHighlighted:false];
i need to highlight my image for one second and after that get it back , but X-Code when arrive to [MyImage setHighlighted:true] do not any thing ( i know it do it ) and after that Sleep executed and main thread of application goes to sleep for 1 second and after that the last line executed , but my question is why the first line do not update the UI and i can,t see any thing in user interface .
In first line of code the Sleep method do not executed and main thread is in normal state and it should set my image to highlight and after that goes to sleep for 1 second and after that set my image to normal state , but why not ?
I know that i can use NSTimer to do it but why the following code do not work ?
Thanks .
jackslash explains well what is happening.
You can change your code into something like this:
[image setHighlighted:true];
[self performSelector:#selector(aMethodThatChangesTheHighlight) withObject:self afterDelay:1];
Finally note that freezing the main thread is generally a bad idea.
changes to the screen are only made once your method returns. Changes are then all coalesced and made at once. In this case as you are sleeping the main thread your method does not return until you un highlight the image. After your method returns UIKit looks to see if it needs to draw any changes. As you have undone your changes before your method returned there is nothing to do so no change happens on the display.
The best thing to do would be to see if you can use the UIView class animation methods.
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationCurveEaseInOut
animations:^{
//animation code goes here
//note that you have to change something that UIView considers to be animatable
//e.g.
self.myView.alpha = 0;
} completion:^(BOOL done){
//code to execute once the animation has completed goes here
}];
I like sch's answer better, but if you wanted to use animation as jackslash suggested, you would have to make the animation change something visual so that the completion block does not run immediately. Not even adding a "delay" will fix that.
MyImage.alpha = 0.99f;
[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{
MyImage.alpha = 1.0f;
[MyImage setHighlighted:YES];
} completion:^(BOOL finished) {
[MyImage setHighlighted:NO];
}];
Related
I have a UIView animation block which uses the UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoReverse option so it keeps going, but at some points I need to stop the animation but more importantly stop it and have the view return to its original position.
I have tried [dot1.layer removeAllAnimations]; but that stops it in its tracks without returning it to its original position, it stops mid animation.
I have tried stopping it then just running a second animation to return it to its correct frame, but that can be a bit jerky as I don't know how far it needs to go with what duration etc.
So is it possible to simply have the animation stop after it's completed its last cycle rather than mid way?
You should try this:
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^
{
view.frame = CGRectMake(0, 100, 200, 200);
} completion:^(BOOL finished)
{
if(! finished) return;
}];
And to stop the animation use this:
[view.layer removeAllAnimations];
I've tried the accepted solution and it didn't work for me. The animations just stopped after one run. Digging around on the net leads me to believe that [self.view.layer removeAllAnimations] is the recommended way to stop animations (another method can be seen in the following link as the asker's comment to the accepted solution
Stop an auto-reverse / infinite-repeat UIView animation with a BOOL / completion block).
The way that worked for me was the one given by BooRanger (even jrturton accepted it). I set a boolean property called stopAnimations in the class and initialise its value to NO. Whenever I want to stop the animations, I set stopAnimations to YES and then call [self.view.layer removeAllAnimations] in the next line. Then in the completion block, I do the following.
if(self.stopAnimations){
self.finishAnimations = NO;
return;
}
Hope this helps the someone. :)
I'm wandering what is the correct way to do "long" actions in response to user events. For example, I have this slide to cancel block that animates itself off screen over 0.5 seconds. The [self coreDataAction] may take about 0.3 seconds itself.
I want to ensure that the action completes once the user sees the end of the animation ( I do not want the user to accidentally navigate to a different controller or close the app thinking that the action is done).
Where should I put the [self coreDataAction]; in this case? Above the block, within the block or in the completion block?
//should I put it here?
CGPoint slideToCancelCenter = slideToCancel.view.center;
[UIView animateWithDuration:0.5 animations:^{
self.goToSleepButton.center = slideToCancelCenter;
[UIView setAnimationDuration:0.5];
CGPoint sliderCenter = slideToCancel.view.center;
sliderCenter.y += slideToCancel.view.bounds.size.height;
slideToCancel.view.center = sliderCenter;
//should I put it here?
// [self coreDataAction];
} completion:^(BOOL finished) {
//should I put it here?
} ];
A better way to handle this might be to animate the view on-screen then start the coreDataAction in the completion handler. Once the coreDataAction method execution is complete you can call a method to animate the slide to cancel view off-screen.
Assuming [self coreDataAction] executes on the main thread, I would say you should put it on the first line to ensure that the method is complete by the time the animation is done.
i noticed some strange behavior. When i start a animation and change the View (the view will not dismissed!), the completion handler never get called.
[UIView animateWithDuration:0.1f
delay:0.0f
options:UIViewAnimationCurveEaseOut
animations:^(void){
[myView setHidden:YES];
myLabel.alpha = 0.0f;
someOtherView.frame = CGRectMake(130, bubbleBigRect.origin.y, 61, 65);
[button setHidden:YES];
}
completion:^(BOOL finished){
NSLog(#"Complete %d",finished);
[imageVIew setImage:[UIImage imageNamed:#"myPng.png"]];
}];
}
is there any solution for this?
Don't know where to write it, but I get the same thing that you have, but in my case the completion block was sometimes called. It might be same thing.
I found out that if in the animation block has nothing was animate- for example, if you set alpha=0 to uiview that is alpha is already 0, or you set content offset to UIScrollView to the same content offset (like in my case), the completion block not called.
Put this in your animation block, and put everything you want to do on completion in YOUR_SELECTOR method. You can now do whatever you want with your view and still have a way of executing something on completion!
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(YOUR_SELECTOR)];
In my case the view which I was animating has its frame set to (0,-568,320,568) and after animation I forget to change the frame to its required position i.e. (0,0,320,568) so the completion block was not being called. Changing the frame of the animated view did the job for me. So I can say if for instance the view has nothing to show(as the frame was not set in visible region). The completion block may not called.
Move your call to [myView setHidden:YES] from the animations block to the completion block. I think that will probably help. You can still set the alpha of myView to 0 during the animation block (if you want it to fade out), or before the entire call to -[UIView animateWithDuration:delay:options:animations:completion:] if you want it to disappear right away.
I have connected the two methods below to separate buttons in my UI but have noticed that after pressing the "VERSION 1" button that I could not press the button again until the animation duration within the method had ended. My understanding was that the animation uses its own thread so as not to block the main application.
// VERSION 1
-(IBAction)fadeUsingBlock {
NSLog(#"V1: Clicked ...");
[myLabel setAlpha:1.0];
[UIView animateWithDuration:1.5 animations:^{
[myLabel setAlpha:0.0];
}];
}
The older style version (below) does allow the button to be repressed before the animation timer ends, simply resetting the timer to start again. Should these both work the same, am I missing something or has there been a change in operation between 3.2 and 4?
// VERSION 2
-(IBAction)fadeUsingOld {
NSLog(#"V2: Clicked ...");
[myLabel setAlpha:1.0];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[myLabel setAlpha:0.0];
[UIView commitAnimations];
}
Cheers gary
Animating with blocks doesn't block the main thread. I think the behavior you're seeing is because, by default, user interaction is disabled duration animation with the new block calls. You can override this by passing UIViewAnimationOptionAllowUserInteraction (calling animationWithDuration:delay:options:animations:completion), like this:
-(IBAction) fadeUsingBlock {
NSLog(#"V1: Clicked ...");
[myLabel setAlpha:1.0];
[UIView animateWithDuration:1.5
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[myLabel setAlpha:0.0];
}
completion:nil];
}
For animateWithDuration:, the class reference doesn't say anything about threading, so I am not sure.
For beginAnimations:context: and commitAnimation:, yeah, they run in a separate thread
UIView class Reference.
Some of the property changes to view objects can be animated—for example, setting the frame, bounds, center, and transform properties. If you change these properties in an animation block, the changes from the current state to the new state are animated. Invoke the beginAnimations:context: class method to begin an animation block, set the properties you want animated, and then invoke the commitAnimations class method to end an animation block. The animations are run in a separate thread and begin when the application returns to the run loop. Other animation class methods allow you to control the start time, duration, delay, and curve of the animations within the block.
In an iPhone application, I try to catch animation endings using setAnimationDidStopSelector. I try to suspend code execution until animation ends. I have tried this; set a global BOOL variable, set it to TRUE before commiting animation and after commiting animations waited using a while loop. In the setAnimationDidStopSelector, set the BOOL variable to FALSE and hope while loop to break. But unluckily this did not work, the code did not even fall into setAnimationDidStopSelector (I check that with some trace outputs). EDIT: If that BOOL variable handling is not added, code runs into the handler method.
The code where animation takes place is below:
self.AnimationEnded=FALSE;
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
// do sth.
[UIView commitAnimations];
while(![self AnimationEnded]);
Also this is the code of handler:
- (void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
printf("abc\n"); fflush(stdout);
self.AnimationEnded=true;
}
What do you suggest?
In iOS 4, you can set a completion block instead of using an animation delegate and handlers. This is a simpler way of taking action when your animation has ended. I recommend using it if you aren't supporting pre-iOS 4 devices.
Your example changes to:
self.animationEnded = NO;
[UIView animateWithDuration:2
animations:^{ /* Do something here */ }
completion:^(BOOL finished){
printf("abc\n");
fflush(stdout);
self.animationEnded = YES;
}];
See +UIView animateWithDuration:animations:completion: at the iOS developer site for more.
You have to call setAnimationDelegate:to designate the object that you want the selector called upon when the animation stops. Assuming that the method that sets your flag to FALSE is in the same class as the one where you are creating the animation this will be self. For details see the UIView class reference.
Try this:
__block BOOL done = NO;
[UIView animateWithDuration:0.3 animations:^{
// do something
} completion:^(BOOL finished) {
done = YES;
}];
// wait for animation to finish
while (done == NO)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
// animation is finished, ok to proceed
The animation will not start until this loop finishes. This loop will not finish until the animation starts.
while(![self AnimationEnded]);
Whatever you want to do after the animation needs to go in the animationDidStop method.