How to replace UIAccelerometer with CMMotionManager?

前端 未结 1 437
[愿得一人]
[愿得一人] 2021-01-14 17:20

I\'m new to iOS development.

I follow the tutorial from Ray Wenderlich to create a little location based AR app. However, the tutorial uses an AR Toolkit which has n

相关标签:
1条回答
  • 2021-01-14 17:37

    Try this link: https://www.inkling.com/read/learning-ios-programming-alasdair-allan-2nd/chapter-9/the-core-motion-framework

    I'm learning a few tidbits that translate some-what with the UIAccelerometer

    i.e.

    [self setAccelometerManager [UIAccelerometer sharedAccelerometer]];
    

    could become

    [self.motionManager = [[CMMotionManager alloc] init];
    

    Setting manual update intervals like

    [[self accelerometerManager] setUpdateInterval: 0.25];
    

    you can have

    self.motionManager.accelerometerUpdateInterval = 0.25;
    

    and releasing the delegate

    self.accelerometerManager.delegate = nil;
    

    would now be

    [self.motionManager stopDeviceMotionUpdates];
    

    Also from the link, I ended up doing something like this:

    motionManager = [[CMMotionManager alloc] init];
    
    motionManager.accelerometerUpdateInterval  = 1.0/10.0; // Update at 10Hz
    
    if (motionManager.accelerometerAvailable) {
        queue = [NSOperationQueue currentQueue];
        [motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            switch (currentOrientation) {
                case UIDeviceOrientationLandscapeLeft:
                    viewAngle = atan2(accelerometerData.acceleration.x, accelerometerData.acceleration.z);
                    break;
                case UIDeviceOrientationLandscapeRight:
                    viewAngle = atan2(-accelerometerData.acceleration.x, accelerometerData.acceleration.z);
                    break;
                case UIDeviceOrientationPortrait:
                    viewAngle = atan2(accelerometerData.acceleration.y, accelerometerData.acceleration.z);
                    break;
                case UIDeviceOrientationPortraitUpsideDown:
                    viewAngle = atan2(-accelerometerData.acceleration.y, accelerometerData.acceleration.z);
                    break;  
                default:
                    break;
            }
            [self updateCenterCoordinate];
        }];
    
    }
    
    0 讨论(0)
提交回复
热议问题