I have a UIView following the users finger as they move it around inside my app. Sometimes, other things on screen are animating with UIViewAnimation blocks, but this freezes the tracking of their finger, so if they continue moving their finger during the animation, it won't follow. How can i stop the animation from blocking up the main thread?
Try using UIViewAnimationOptionAllowUserInteraction with [UIView animateWithDuration:delay:options:animations:completion:]
you can use the NSObject's method: performSelector:onThread:withObject:waitUntilDone:
More details in Apple NSObject Documentation
If there is any other thing on the screen is animated, that would also be done in main thread. And the current finger tracking would also be done in main thread. So definitely there would be some blocking.
To get rid of that, we can optimize our code using blocks and GCD.
Note that if you link for iOS 5 this problem will go away all by itself. Under iOS 5, block-based animation of a view does not turn off user interaction for other views. This is what Apple should have done in the first place.
Related
Currently I encounter an issue somehow, the application lost all those artistic UI animation for example, page flipping, alert view popup, action sheet slide up and etc. That means all those UI will show up immediately without any transition animation. It looked very weird.
Firstly, the app will run smoothly until something trigger the issue above, and after that only re-run the app or kill the app will stop the problem.
There is no error message or any clue that I can figure out what could be the reason. Have any one of you guy has encountered similar issue as above? Please share with me how am I able to solve the issue above. Thanks.
Animations may get disabled for the entire app whenever an attempt is made to animate views on a background thread, e.g. by calling one of UIView's animateWithDuration:animations: family of class methods from a background thread. Be sure to update your app's UI only from the main thread.
You can check if code is running on the main thread by testing [NSThread currentThread].isMainThread and you can ensure it runs on the main thread like so:
dispatch_async(dispatch_get_main_queue(), ^(void) {
// Your code
});
Alternatively, ensure that you're not calling [UIView setAnimationsEnabled:NO] anywhere, as that will also disable animations for the entire app.
I'm wondering if iOS allows one to do the following:
I have a puzzle game and I've been working on saving data when the user returns to the home screen. To do this, using NSNotificationCenter, I had one of my game classes to observe [UIApplication sharedApplication]'s ApplicationWillResignActive method so when that happens I save my game state. But if the user decides to exit while animations are going on, the game will save a midState where the model values are still changing and that will often cause crashes. My question is if it is possible to somehow delay the saving process (even though it is on the background) until the animations are complete (or some variable equals 1)?
My first idea is to create scheduled event with NSTimer to try to save until everything is set. Any ideas would be appreciated. Thank you
You can use performSelector:withObject:afterDelay:
// Call doSomething after a 1 second delay
[self performSelector:#selector(doSomething) withObject:nil afterDelay:1.0f];
Rather than trying to delay the saving, especially in ApplicationWillResignActive, you should look into how to stop the animation and record the expected final values of the animation. Depending on the method of animation (UIView static methods, block based, 3rd party) there is usually a way to stop them, and since you define what the animation does you should already know the final state.
In the case of block based animations:
How to cancel UIViews block-based animation?
i wonder how i can have continuos and repeating animations on my Views w/o freezing the rest of my app - since my current approaches lead into 100% animation, 0% user interaction possible - which is kind of .. bad.
the animation in general is simple: i want to make an uilabel act like a siren, altering the alpha-property from 1 to 0 and back and so forth.
the basic animation block therefore is:
[UIView animateWithDuration:0.4f
delay:0.0f
options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
animations:^(void){warningMessage.alpha = 0;}
completion:NULL];
i tried to call it in background, in backgroundthreads, even in an async GCD block, each time i loose control of the app - but the animation works fine.
(ok, apple docs say it MUST happen on main thread, but i tried..)
Now again, the question: how to have both of it? is it even possible in that way?
Thanks in advance!
P.S.: Sry if this is quite a repost, did not find a proper solution/question here!
P.P.S: yes, i know that sirens dont alter their alpha propery :P
did you try adding the UIViewAnimationOptionAllowUserInteraction to the options flag?
Well.. doing animations with uikit is for sure easy .. but I would not call it very efficient. You just animate the alpha, but there is much more happening in the background. :)
Animations already run in a different thread.. so starting it on an other thread won't help you.
As a solution I would register a CADisplayLink in your application and change the alpha of the view in it's update function ... that will be much more efficient if you run it all the time.
My app is playing a pretty complex animation. It's like a flipbook.
What I do is: I have a huge loop with selectors, and after every delayed call the next one is called.
Now someone calls the user and the device suddenly shows up this fat green status bar and maybe some big pick-up-the-phone-call overlay. Or: The alarm clock rings, and a big alert sheet appears in front of just about everything.
It would be great to just pause the whole animation in case of ANY interruption. Probably I've also missed like 5 more possible interruptions.
How are you doing that? How do you get notified for all interruptions and then call one single -stopEverything method?
Whenever the app becomes inactive, the UIApplicationWillResignActiveNotification local notification will be posted. In the opposite suituation, the UIApplicationDidBecomeActiveNotification notification will be posted.
Your animation logic can listen to this and respond appropriately. There is an +setAnimationsEnabled: method to kill all current and future animations, but there isn't a documented "global pause" method.
Depending on the animation, you might be better off using CoreAnimation directly by using a single CAKeyframeAnimation on the view's -layer rather than having one animation's completion selector start another animation.
I've been using CALayer animations in my program, primarily to move around CALayers with animation on my screen. My problem is that I don't want to exit the layer-moving (card dealing, as it happens) method until all of the cards are done dealing. How can this be done cleanly?
Presume that my layer-moving method is called by different methods at different times, so it's better to let the normal flow of the program bring me back to the starting place, rather than trying to go back and forth with delegation.
Trying something simple like udelay() didn't work because it appears to block the display of the animation too.
Any ideas? Bonus if it's something I could use to pause for the completion of a built animation too (specifically, the animated dismissal of a modalViewController in what I'm working on now, but it'd be nice to have mechanisms that would regular pause, then continue on when various Animated: things occur).
CAAnimation has a delegate method animationDidStop:finished: and UIView has setAnimationDidStopSelector:. You can pause what you are doing at the beginning of the animation, and use these to resume. For example, if you want to pause a timer, set timerRunning = NO right at the animation code, then timerRunning = YES in the didStop method.