Here's The CA Animation Sequencing Sitch - ios

I asked a question (Animation Sometimes Doesn't Occur) a while ago about an animation not, apparently, happening sometimes. I got no helpful answers. So, after much research and playing around, I discovered that sometimes the animation that is supposed to happen second, sometimes happens first. The other one does happen, just not when it is expected to happen. The two animations are "flip" and "disappear." As an example, there are four tiles. When one tile is touched, it flips. When a second tile is touched it is supposed to flip. If the tiles match, both tiles are disappeared. If they are not matched, the tiles flip back. The flipback animation has always occurred correctly, so far as I have been able to tell. In other words - the first tile is flipped. The second tile is flipped. The tiles do not match. The two tiles flipback. As for when the tiles match, the first tile flips correctly. Sometimes the second tile flips correctly and then they both disappear (animation group of rotate and shrink). Sometimes the second tile does not flip correctly, but they both disappear before the second tile does its flip. This is not acceptable.
This is what I have tried so far - I have tried NSThread pausing. That didn't work because the whole thread was paused, which didn't let the animation that hadn't occurred occur. I looked into NSTimer - but the method that is being called has two parameters which means I have to do NSInvocation and I haven't figured that out yet - and not sure it would work. Right now, I have put in a flag that marks when flip animations are completed. If this method eventually works, I have to figure out how to account for flipping individual tiles over and back again without upping the completion count. Anyway - using this method, I need to figure out how to tell my second animation to wait until the first animation is completed before running.
What I would ideally like to be able to do is to tell the disappear animation to always wait until the second tile is completely flipped before committing.
What I can't figure out is a) why sometimes the animations happen in the correct order, sometimes they don't and b) when the animations are trying to not happen in the correct order, how to force them to occur in the correct order.
Not really sure what code would be helpful for you to see. Ask and I shall provide!
Here is the code that calls the animations:
-(void)buttonPressed: (UIButton*)buttoni
{
[self flipTiles:(UIButton *)buttoni];
[self twoTilesFlipped];
}
Clearly, the flipTiles animation is called first. What I don't understand is why it sometimes isn't called until after the code that is called in twoTilesFlipped. I thought that programming was about logic and this just doesn't make logical sense to me. Oh, I just had a thought...I am going to put in a NSTimer delay in this code - maybe that will do the trick!

That seems to have done the trick. There are some tweaks that I obviously need to fix now, but at least it works everytime.
This is what I added:
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(twoTilesFlipped)
userInfo:nil
repeats:NO];
so the final code for the method was:
-(void)buttonPressed: (UIButton*)buttoni
{
[self flipTiles:(UIButton *)buttoni];
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(twoTilesFlipped)
userInfo:nil
repeats:NO];
}

Related

iOS - limit on UIView animate?

So I'm making a simple trivia game and I have a timerView that shrinks as time passes. When the user selects an answer, it needs to stop shrinking immediately - it must be very responsive. I give the user 10 seconds per question. Originally I would animate 10 times (with a duration of 1.0f), calling the next "segment" of animation in the completion block of the previous animation. In the completion block I would check to see if the user has tapped an answer, and if so I don't continue the chain. That solution works fine except that it's not very responsive because it's on a per second basis-- user taps an answer at the start of the second segment and the bar has a noticeable continuation.
My solution to THAT problem was to instead have 1000 animation calls with a duration of 0.01f. After doing that, the responsiveness was on point - the view stops animating as soon as I tap an answer -- the issue though, is that it's not actually 10 seconds, it takes more like 20.
So question number 1: what's the smallest time interval animateWithDuration can actually process properly?
Question number 2: is there a better way to accomplish what I'm trying to do accomplish?
ill answer question two: yes there definitely is a better way, have a look at CADisplayLink
use it to shrink your view a little bit each frame, and end the display link when you need to
the most responsive way is: the user taps an answer, you response in the touch callback, remove animations. you can remove animations by CALayer's removeAllAnimations method
Another way to do it is to set the view to shrinking using a single animation with linear timing, and then set the speed of the view's layer to 0 to pause the animation. When you set the speed on the layer to 0 the animation pauses instantly.
This works because under the covers, UIView animation actually creates and installs CAAnimation objects on the view's layers. It's possible to pause and continue an in-flight UIView animation just like you can a CAAnimation.
I have a project called KeyframeViewAnimations (link) on github that allows you to pause, continue, or "scrub" UIView and CAAnimations back and forth with a slider. You could use that technique. The tricky bit will be figuring out how far along the animation is.

iCarousel infinite scroll and index reset

I am working with a carousel in iOS and I am trying to recreate the scrolling banners (featured images) that are present in the iOS app store and iTunes. I have a carousel variable and I would like to get that to scroll exactly like on the apps mentioned above. If you are not familiar with how they scroll, they slide the image and then stop for a period of time.
I have tried carousel.scrollByNumberOfItems(5, duration: 20) which just creates a constant scroll until it is finished. I have played around with different scrolling functions and cannot seem to achieve the desired effect. Again, I am looking for the image to slide, then pause, then slide the next, etc.
My second question is, when the images slide to the end of my images, how can I reset the index if it is passed in as a constant? In other words I am trying to achieve an infinite scrolling effect that will just keep cycling through my images one at a time, similar to the app store.
I am working in swift, but welcome objective c examples as well.
Any help is appreciated!
Thanks!
You can do this pretty easily with an NSTimer to initiate the scrolling.
For example this will scroll every 20 seconds, taking 5 seconds to scroll -
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.scrollTimer=[NSTimer scheduledTimerWithTimeInterval:20 target:self selector:#selector(scrollCarousel) userInfo:nil repeats:YES];
}
-(void) scrollCarousel {
NSInteger newIndex=self.carousel.currentItemIndex+1;
if (newIndex > self.carousel.numberOfItems) {
newIndex=0;
}
[self.carousel scrollToItemAtIndex:newIndex duration:5];
}
as long as your carousel is set to wrap then this will give you what you want.

iOS app glitches every time the label value changes

I am making a game on Xcode and I am having a problem with the scoring mechanism. My game involves a submarine dodging incoming obstacles. I am not using the Open GL Es. My problem arises with the score. I have a timer that runs a scoring method every 1 second (code below)
Scorer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(Scoring) userInfo:nil repeats:YES];
Here is the Scoring method.
- (void) Scoring
{
ScoreNumber = ScoreNumber + 1;
Score.text = [NSString stringWithFormat:#"Score: %i", ScoreNumber];
}
Every time the score goes up one, the game kind of starts back up at the origin points for the submarine. It is like the view is reloading each time the value of the label goes up. If I forward slash // the Score.text part, the game runs fine.
Thanks and I appreciate any help possible. If more information is needed, just say so.
This sounds like a consequence of auto layout. If any views are moved or resized by setting frames instead of adjusting the constraints, the views will reset to the frames determined by their constraints whenever the view has to redraw (like when you update your label). To fix this, you either need to turn off auto layout, or do all your moving by modifying constraints.

array's objects loss in storyboards's scene

I make a game that has 2 scenes (first-welcome screen with "start game" button, second - is the game") Game is simple: head-picture is trying to prevent collions with bullets(represented as uiimageviews in NSMutableArray *bullets) with the help of -(void)touchesMoved:withEvent:. If it collides UIAlertView appears to get user's choise: repeat or go to wellcome-scene. If we go to the game-scene first time then everything is OK.
The problem is when we go to the game-scene next time. the property bullets after initiation in viewDidLoad shows its count as 3(as it should), but latter it shows bullets.count == 0;
I don't know how is it possible - I initiate this array in method that calls only in viewDidLoad. And in the first time everything works properly.
P.S. duaring code I don't use propertyName, only self.propertyName.
P.P.S I suggest reason in [UIView commitAnimations] - that's how I make bullets animation.
If I //hide it. evetything is OK. But without animation it looks poor.
Instead of calling the method in viewDidLoad, try calling it in viewDidAppear to set the count to 3 each time the view appears, because it should only be loaded into memory once, but viewDidAppear is called each time the view will show.

UIScrollView animation problems

I'm using a horizontal UIScrollView that displays photos on the first half of the screen. (Imagine a CGRect (0,0,320,120). The first scrollView is embedded in a second scrollview, which takes all the screen. When I scroll down the page (thus the second scrollView), the first scrollview stop being animated. I programmed a NSTimer to change photo every 3 seconds in the first UIScrollView, but while I'm scrolling the second scrollView, it seems like the animations are being queued. When I release my finger of the screen, there are few photo transitions (i.e: 2 changes if I scrolled without stoping for 6 seconds). In short: how can I make use of blocks (or something else) to continue my animations while I'm scrolling the second scrollView?
My NSTimer (in viewDidLoad):
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(changePage:) userInfo:nil repeats:YES];
Your iOS app has a run loop that continually checks for things like user input. If it is busy detecting touch events, things that are scheduled on that run loop will not get performed. (Similarly, if you block the main thread with e.g. a big server request, you won't get touch events during that time).
Here's how you can get the NSTimer on the right run loop:
First, use timerWithTimeInterval:target:selector:userInfo:repeats: to create a timer not scheduled on a run loop.
Second, use - (void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode to schedule the timer on a run loop. For the mode argument (the run loop type), use NSRunLoopCommonModes to allow the timer to fire without having to wait on the main run loop.
Note that this same effect exists for e.g. performSelectorAfterDelay if done in the main run loop. It also applies to animation completion blocks. I'm not sure if there's an easy way around the animation completion block problem besides using CoreAnimation.

Resources