Moving Images Smoothly With Accelerometer

Discussion in 'iOS Programming' started by ahan.tm, Jun 25, 2013.

  1. ahan.tm macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #1
    Hi All,

    I am trying to have an array of UIImageViews move with an array. I already am collecting accelerometer data, filtering it with a Low-Pass Filter, and moving the images with an animation, yet the movement is not smooth. I am trying to get movement like the stars in the Starbucks app. Any ideas? Should I be moving to core motion???

    Thanks,
    Ahan
     
  2. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #2

    Core motion will give you integrated access to the different sensors on a newer device (accelerometer, gyroscope, GPS, etc.) It won't solve your animation problem.

    The problem is likely with what you do to convert your accelerometer data into moving animation.

    How are you doing your animation? The devil is in the details as they say.
     
  3. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #3
    Here is my code:
    Code:
       CGPoint newCenter = CGPointMake(img.center.x + acceleration.x*xFudge,img.center.y - acceleration.y*yFudge);
            
            [UIView animateWithDuration: .001*xFudge
                                  delay: 0
                                options: (UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
                             animations:^{img.center = newCenter ; img.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);}
                             completion:^(BOOL finished) { }
    I am just using the basic UIView Animations. xFudge, yFudge are variables that are randomly selected from 0 to 1.

    Thanks,
    Ahan
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    If xFudge is in the range 0.0-1.0, then the code shown in red will be a value in the range 0.0 thru 0.001 secs. This places the maximum duration at 0.001 secs, or 1 millisecond, which hardly seems useful.
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    I think the iPhone redisplays it's screen 60 times a second.

    1/60 of a second is about 0.017. (ok, 0.016666666...)

    A duration of .001 is less than 1/10 of a single frame. The system has nothing to work with to create reasonable animations.

    You want an animation duration of 10 frames or more in order to get any kind of sense of motion, and even that is probably too short.

    Think in terms of much longer animation segments. You can use the UIViewAnimationOptionBeginFromCurrentState option to shift an animation to a new destination value while an animation is "in flight."
     
  6. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #6
    Hello All,

    Thanks for all your responses. I have increased animation duration to .25 seconds, and it seems to have improved. Should I move toward Core Animation to create a better animation effect?

    Thanks!
     
  7. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    UIView animation, like you are using, really is Core Animation under the covers. If you drop down to Core Animation you have more options and control, but at the cost of much greated complexity and a much steeper learning curve.

    I try to do as much as possible using view animation like you're doing, and only use Core Animation when I can't get the job done with UIView animation. In your case I would think view animation would be enough.
     

Share This Page