开发者

iphone accelerometer: problem rotating image

开发者 https://www.devze.com 2023-02-27 02:52 出处:网络
Hi everyone I\'m french so scue me for my english. So I have a problem.What I\'m doing is when I turn device to left or right with accelerometer an image rotate in the opposite direction of the rotati

Hi everyone I'm french so scue me for my english. So I have a problem.What I'm doing is when I turn device to left or right with accelerometer an image rotate in the opposite direction of the rotation of the device it create a cool effect but when I rotate the device with accelerometer the image is rotating but it is always trembling, vibrating the movement is not smooth. What can I do ? here is the code:

#import "QuartzCore/QuartzCore.h"

#define CONST_fps 100.
#define CONST_map_shift 0.05

@implementation MapViewRotationViewController

- (void)viewDidLoad {

[super viewDidLoad];

    // accelerometer settings
    开发者_高级运维[[UIAccelerometer sharedAccelerometer] setDelegate:self];
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / CONST_fps)];
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{

    static CGFloat ZZ = 0.;

    CGFloat z = (atan2(acceleration.x, acceleration.y) + M_PI);


    if (fabsf(ZZ - z) > CONST_map_shift)

    {

        viewToRotate.layer.transform = CATransform3DMakeRotation(ZZ=z, 0., 0., 10.);

    }

}

@end


You need to do a trick that is called high pass filter. It filters trembling, only significant changes will pass.

You can google it, or search on StackOverflow. For example, here: How do you implement a Highpass filter for the IPhone accelerometer?

0

精彩评论

暂无评论...
验证码 换一张
取 消