iOS app glitches every time the label value changes - ios

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.

Related

How to set position for imageView animated with timer when "imageView.translatesAutoresizingMaskIntoConstraints = true"

I have checked all over for a solution to one problem:
I have set an imageView to move with a timer, but I have another timer counting seconds, and every time the second-timer goes off, the imageView spawns in it's original position.
Here is the post where I found my solution:
Swift: timer resetting the view
Here it suggests that I use "imageView.translatesAutoresizingMaskIntoConstraints = true", which actually works fine. But I have one problem. If I can't use Auto Layout, how do I tell the imageView where to spawn when the timer hasn't begun?
Because "imageView.translatesAutoresizingMaskIntoConstraints = true" basically kills my Auto Layouts, so it spawns about 30-40 pixels away from the middle, which it is set to do.
I don't find any use in showing my code, since it has got to do with the "imageView.translatesAutoresizingMaskIntoConstraints = true" command.

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.

UIWebView Content Offset

I want to show a google.maps url into a UIWebView in my app but the page comes up in different point from this that i want. I tried everything of many questions here such as set contentOffset of webview.scrollview or contentInset or scrollsToTop or even with javascript commands but neither of them works.So this is the problem:
and this is the expected result that i would like to have:
I want my webpage to come up on top and not in the middle.
url: url
This has nothing to do with content offsets etc. - it is the page itself which scrolls down right after it finished loading. When loading the URL in Mobile Safari, you will get the exact same behaviour and can even observe the scrolling animation.
Unfortunately, there is nothing you can do to prevent it, except extracting the scroll code from the html the server delivers before loading it in the webview. This would be tedious and you shouldn't do it anyway, since this will likely break your app once changes are made by google to the inner workings of that page.
The only solution I can think of right now is to scroll the page back to top programmatically after it has scrolled down. I've tried it with the url you provided and it acutally works. To do this, implement webViewDidFinishLoad: in your webview delegate like so:
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSTimer *scrollTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(scrollBackToTop) userInfo:nil repeats:NO]; // Better make the timer an ivar
}
and implement a method like:
-(void)scrollBackToTop {
NSString* javascript = [NSString stringWithFormat:#"window.scrollTo(0, 0);"];
[theWebView stringByEvaluatingJavaScriptFromString:javascript];
}
This means of course that your webview will first scroll down visibly only to jump back to top, which is rather ugly. So you may want to hide the webview behind an opaque view containing an activity indicator or the like until the webview has loaded and finished all of the scrolling action...
Be aware though that, while less likely and less lethally, this may break as well. When I tried my solution, a 2 second delay was sufficient. If google slows down the animation in the future though, 2 seconds might not be enough and your scrollBackToTop method fires early. Since the java script will not cancel the scroll in progress, it won't scroll back to top at all.
Edit:
Maybe I have something better for you...
Do the following in your webViewDidFinishLoad: method:
NSString* javascript = [NSString stringWithFormat:#"document.getElementById('panel').style.position='fixed'; document.getElementById('panel').style.top='50px';"];
[webView stringByEvaluatingJavaScriptFromString:javascript];
This will completely suppress the scrolling animation. But it comes at a cost: If the user scrolls the webview manually now, the form fields remain fixed in position, while the rest of the page is scrolling normally.
So you need to undo the hack, by doing this:
NSString* javascript = [NSString stringWithFormat:#"document.getElementById('panel').style.position='absolute'; document.getElementById('panel').style.top='0px';"];
[theWebView stringByEvaluatingJavaScriptFromString:javascript];
You need to execute the second step with a delay again, after the scroll script has finished, otherwise the page will do the full scroll. For me, a 1.2 second delay did the trick.
You might want to prevent the user from scrolling during those 1.2 seconds by disabling scrolling on the webviews scrollview until the timer fires.
So there's still room for improvement, but this approach will probably make your app feel more responsive, compared to hiding the webview until it has scrolled back and forth...

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.

Here's The CA Animation Sequencing Sitch

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];
}

Resources