[英]Xcode iPhone accelerometer image
I'm French so excuse me for my English. 我是法国人,请原谅我的英语。 So I use an accelerometer, but in my code the accelerometer works for a view and not for an image.
所以我使用加速度计,但在我的代码中,加速度计适用于视图而不适用于图像。 What I want to do is to use this accelerometer for an image instead of a view.
我想要做的是将此加速度计用于图像而不是视图。 How can I do this?
我怎样才能做到这一点? Here is the code:
这是代码:
#define CONST_fps 100.
#define CONST_map_shift 0.01
#define kFilteringFactor 0.1
UIAccelerationValue rollingX, rollingY, rollingZ;
@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
{
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));
rollingZ = (acceleration.z * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));
float accelX = acceleration.x - rollingX;
float accelY = acceleration.y - rollingY;
float accelZ = acceleration.z - rollingZ;
static CGFloat ZZ = 0.;
CGFloat z = (atan2(rollingX, rollingY) + M_PI);
if (fabsf(ZZ - z) > CONST_map_shift)
{
viewToRotate.layer.transform = CATransform3DMakeRotation(ZZ=z, 0., 0., 1.);
}
}
@end
You don't need to have a view as callback instead you can choose any class to conform to protocol UIAccelerometerDelegate . 您不需要将视图作为回调,而是可以选择任何类来符合协议UIAccelerometerDelegate 。 Just declare your class in the header file as
只需在头文件中声明您的类为
@interface MyClass : NSObject <UIAccelerometerDelegate> {
// my members
}
In MyClass.m you have to implement method 在MyClass.m中,您必须实现方法
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// do what you like
}
But if you are not forced to support iOS version 3.x, you should consider using CoreMotion API. 但是,如果您不被迫支持iOS 3.x版,则应考虑使用CoreMotion API。 See Event Handling Guide for iOS / Motion Events for more information and samples.
有关更多信息和示例,请参阅iOS / Motion事件处理指南 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.