Help with Animating and detecting position

Discussion in 'iOS Programming' started by MontyClift, Feb 8, 2009.

  1. MontyClift macrumors newbie

    Sep 2, 2008
    Hi guys,

    I'm just working on a simple proof of concept app at the moment and having a couple of issues that I hope a more experienced dev might be able to help me with.

    Basically the gist is:
    Animate using Core Animation one object around the screen.
    Move a second object around the screen with my fingers.
    So far so good.
    Now the next thing I want to do is detect if the two objects ever meet.
    So the first thing I tried was hooking up a Timer object and firing every 0.01 and detecting whether the two objects intersect. This works fine, except as you might imagine it starts to lag like crazy!
    So changing the timer to fire every 0.2 seems to run a lot more smoothly but it doesn't now detect when the two objects meet as accurately as it should.

    So my next idea was to move the collision detection to a separate thread. Basically a simple while loop... while !collision then detect...
    OK so now the app runs perfectly smoothly except it doesn't actually detect anything. By printing logs I can see that the second thread is only ever seeing my objects at their original positions, before animation.

    I am using the presentationLayer to get the location [[topLeft.layer presentationLayer] frame] but it seems this doesn't work when I am in a second thread. I guess similar to .net background threads are not allowed interact with UI elements directly?

    Does anyone have any ideas/tips to point me in the right direction to achieve what I need?

    Thanks in advance
  2. jnic macrumors 6502a

    Oct 24, 2008
    Exactly right, you need to be in the main thread for UI interaction. You can use performSelectorOnMainThread to access the UI and perform UI updates from other threads.

Share This Page