Why are my iOS events firing in the wrong order? - iphone

I'm working an iPhone app called RealBaseball. If you're familiar with RealSoccer, it's pretty similar. It will allow you to play baseball with your iPhone with friends around the world.
When you hit the iPhone screen with your conductive bat, it should first detect the touch event then detect the acceleration so it knows exactly when you hit it, and how hard and how fast.
This is where I'm running into some technical difficulties. I have the standard touchesBegan and didAccelerate listeners. The problem is that didAccelerate is firing before touchesBegan. Clearly, something is wrong since it shouldn't start accelerating before the bat touches it. Could I be missing setup code?
I need to fix this, because if the timing is wrong, the batting machine on the other end will be laggy, and it won't be any fun.
Any advice you can provide would be appreciated.
EDIT: I added some NSLog calls with formatted [[NSDate date] timeIntervalSince1970]; Here's the output:
didAccelerate: 1364850400.03221
touchesBegan: 1364850400.21486
So you can see it's a small but clear difference.

How are you ensuring the bat is closing the conductive circuit from the phone to your hand? I'd suggest copious use of the material used to make gloves touchscreen friendly. Silver-threaded nylon is recommended here: http://www.howtogeek.com/130477/diy-touch-screen-mod-makes-regular-gloves-smartphone-friendly/
UPDATE: Oh, you might need to amplify the very tiny current that the bat will experience so it can travel all the way to your hand. I'm not an electrician but a portable car battery attached to the bat via the same thread should do the trick.

Related

How to delay the start of a function

so i have built my first application in X Code using swift, and i have also taken the time to add the game centre functionality to it, however my question is, how would i delay the start of my game, in order for game centre to load, as of right now, the game starts, and then the game centre banner appears on top of my game before disappearing.
would it be sufficient to use something like, on the function containing my main game attributes:
NSTimer.scheduledTimerWithTimeInterval()
or
would i be better off adding a splash screen?
if anyone has any other suggestions, that would be helpful.
thanks in advance.
Better to subscribe under GameCenter, listen a complete event and then produce your next initialization. Avoid timers in similar situations, because time of processing depends from many factors, and rely to specific delay is not good idea.
If you are waiting for the game to load, then starting a timer is probably not the best solution, because you don't know how long the loading takes on different devices with different capabilities.
A better idea is to load your game and then have a completion method that is called to start the game when it is ready to be run.

capture movements or keystrokes of a game like (Angry Bird) using touch screen of an iphone

I basically need to find out if there is a way to capture movements or keystrokes of a game such as "Angry Birds" etc using the touch screen of an iPhone and save them to a file on the device.
I'm sure these phones have security issues and don't want native "keystroke logging", but if it's a layer that sits over the other game, it should be ok
Please let me is there any way to achieve the same. Your help would be appreciated. Thanks In Advance
What you are trying to do can be very easily accomplished by using MobileSubstrate, a very powerful framework created by Jay Freeman that allows you to modify virtually any function of any process at runtime. The build environment theos, by Dustin Howett, can be used in place of Xcode, with its Logos preprocessor making all this seemingly complicated mischief a very, very simple task.
Something like this could be accomplished with a very small amount of code.
Example:
%hook UIApplication //Hooks into the UIApplication class.
- (void)sendEvent:(id)arg1{//Hook the sendEvent function, the function which handles all events passed to a UIApplication.
if([arg1 isKindOfClass:%c(UITouchesEvent)]){//Make sure we're not processing other events, such as device rotation.
for(UITouch *touch in [arg1 allTouches])
NSLog(#"Touch recorded: %#", NSStringFromCGPoint([touch locationInView: [[UIApplication sharedApplication] keyWindow]]));
}
%orig;//Call the original function, so the app can still process the events, and we don't eat them all.
}
As you can see, it is actually quite simple.
Unfortunately, jailbroken devices are your only option. Obviously, you won't be able to put anything like this in the App Store. However, Cydia has become a very prominent part of iOS, and success through it can be reached just as easily, if not more easily, than something on the App Store could.

How to handle iPhone 'Low Battery' pop up during a game

I was wondering how low battery alerts can be handled during a game. I used Cocos2D to make my game. I have the game pause when an interrupt such as a text or a call come in. But what about low battery alerts? Are they the same as other interrupts? It does not seem to be.
Is there any way I can detect when it comes up so that I can pause the game?
Thanks for your replies.
You can set up "battery monitoring" and get an event when this happens. Some links with info:
http://mobileorchard.com/new-in-iphone-30-tutorial-series-part-5-battery-monitoring
http://mobiledevelopertips.com/device/display-battery-state-and-level-of-charge.html
You know the event you're worried about is when it hits 20%, so I think you can just check for that amount and pause when it happens.
This is perhaps not a perfect solution, as Apple could decide it should happen at 25%, not 20%.
Also, Apple recommends turning this on only when you really need to know. So it should only be activated during gameplay, not on menus that aren't time sensitive.
Thanks guys. The problem however lied elsewhere. It happened to be a problem with one of the flags im checking upon resume. It should get reset when a user starts a new game, which I failed to do.
Lesson learnt: Be extra careful with global variables :) [I try to avoid them as much as possible]
I appreciate your replies and your answers. Thanks again.

Is it possible to let the iPhone system time run faster for a moment?

Maybe this can solve out the ugly delay with touchesBegan: to touchesMoved:. It is caused artifically by iPhone OS to check if the user really wants to move. In some situations the finger must be tracked without delay, otherwise it just looks ugly. You see that everywhere through iPhone OS. Sliders, Scroll Views.
Maybe accelerating the system time makes the delay smaller.
You can set UIScrollView:delaysContentTouches to be NO, which will solve this.
You really don't want to change the system time to hack out something like this. Do you want apps you're using to screw up your calendar and alarms and your phone log, etc?
I asked a similar question, still open.
I don't think there's a way around it for now, seeing as even the ultra lickable "slide to unlock" slider suffers from the problem.
At this point I think the only solution is try smoothing this initial bump out with a bit of position interpolation.

Initial iPhone virtual keyboard display is slow for a UITextField. Is this hack around required?

I have an app with a UITextField, amongst other things. When the user first taps on the text field, there is a noticeable delay before the virtual keyboard appears. On a 3GS it isn't too obvious, but on an older iPhone the delay can be around 1 second. After that the keyboard always pops up instantly. The delay is only the first time the keyboard pops up after app startup.
It looks like the initial UIKeyboard instantiation takes some time (quite a bit...) but is kept around after that.
I found very little information about this, which surprised me. However I did find this write up of the issue along with a hack-around solution.
http://blog.weareuproar.com/preloading-the-uikeyboard
My question is: is this hack around the only available solution? Is there a way to signal the framework (e.g. via info plist?) to instantiate the keyboard on startup?
No, there is no other (documented) way to do that. And even Apple's built-in apps (such as Maps) suffer from the same problem. You can either go with the hack you linked to or follow Apple's advice to not load stuff in advance before you really need it. By the way, this isn't much of an issue anymore with the iPhone 3Gs and the new iPod touch. The newer and faster devices load the keyboard almost instantly.