How to implement pendulum animation based on accelerometer in ios

583 Views Asked by At

Hello Everyone i am implementing accelerometer based pendulum animation. But it gives strange behaviour . below is my code. Please help me to find where i am wrong.

#import "DemoViewController.h"

@interface DemoViewController ()

@end

@implementation DemoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIAccelerometer* theAccel=[UIAccelerometer sharedAccelerometer];
    theAccel.updateInterval=1/50.0f;
    theAccel.delegate=self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
    CGFloat x= acceleration.x;
    self.imPendulum.transform=CGAffineTransformRotate(self.imPendulum.transform,-x);
}
@end
1

There are 1 best solutions below

0
On

Well, first of all, you'll want to create an anchor point for where the fulcrum of your pendulum is, so it swings rather than spins.

Try that first. Then, I imagine you need to create a coefficient (or more nuanced formula) to convert acceleration.x into CGFloat x.

Perhaps most importantly, you are not animating here, you are performing a transform, that's very different. It simply moves an item TO a new spot, it doesn't move it gently from point A to point B. For that, you'll want to put your transform inside an animation block.

If you try the above 3 things, you should be hot on the trail and then it will just be details.