Xcode iPhone 加速计图像

发布于 2024-11-01 21:27:38 字数 1267 浏览 3 评论 0原文

我是法国人,所以请原谅我的英语。所以我使用加速度计,但在我的代码中,加速度计适用于视图而不是图像。我想做的是将这个加速度计用于图像而不是视图。我该怎么做?这是代码:

#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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2024-11-08 21:27:38

您不需要将视图作为回调,而是可以选择任何符合协议的类 UIAccelerometerDelegate。只需在头文件中将您的类声明为

@interface MyClass : NSObject <UIAccelerometerDelegate> {
  // my members
}

在 MyClass.m 中您必须实现方法

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
  // do what you like
}

但如果您不是被迫支持 iOS 版本 3.x,您应该考虑使用 CoreMotion API。请参阅iOS/动作事件的事件处理指南< /a> 了解更多信息和示例。

You don't need to have a view as callback instead you can choose any class to conform to protocol UIAccelerometerDelegate. Just declare your class in the header file as

@interface MyClass : NSObject <UIAccelerometerDelegate> {
  // my members
}

In MyClass.m you have to implement method

- (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. See Event Handling Guide for iOS / Motion Events for more information and samples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文