有陀螺仪滚动图像的问题 [英] having issue with scrolling image with gyroscope

查看:124
本文介绍了有陀螺仪滚动图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对iPad Air有一个奇怪的问题! ,我的代码在iPad 3,iPad 4,iPhone 5S,iPod 5th Gen上运行良好,但在iPad上,我的图像自动滚动而无需用户旋转设备,这是我的代码:

I have a strange problem with iPad Air !!! , my code runs fine on iPad 3 , iPad 4 , iPhone 5S , iPod 5th Gen , but on iPad air , my image scrolls automatically without user rotate the device , here is my code :

 @property (strong, nonatomic) CMMotionManager *motionManager;


    self.mainScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    self.mainScrollView.bounces = NO;


    self.mainScrollView.userInteractionEnabled = NO;

    //set up the image view
    UIImage *image= [UIImage imageNamed:@"YOUR_IMAGE_NAME"];
    UIImageView *movingImageView = [[UIImageView alloc]initWithImage:image];
    [self.mainScrollView addSubview:movingImageView];

    self.mainScrollView.contentSize = CGSizeMake(movingImageView.frame.size.width, self.mainScrollView.frame.size.height);


    self.mainScrollView.contentOffset = CGPointMake((self.mainScrollView.contentSize.width - self.view.frame.size.width) / 2, 0);

    //inital the motionManager and detec the Gyroscrope for every 1/60 second
    //the interval may not need to be that fast
    self.motionManager = [[CMMotionManager alloc] init];
    self.motionManager.gyroUpdateInterval = 1/60;

    //this is how fast the image should move when rotate the device, the larger the number, the less the roation required.
    CGFloat motionMovingRate = 4;

    //get the max and min offset x value
    int maxXOffset = self.mainScrollView.contentSize.width - self.mainScrollView.frame.size.width;
    int minXOffset = 0;

    [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
                                withHandler:^(CMGyroData *gyroData, NSError *error) {

         if (fabs(gyroData.rotationRate.y) >= 0.1) {
            CGFloat targetX = self.mainScrollView.contentOffset.x - gyroData.rotationRate.y * motionMovingRate;

             if(targetX > maxXOffset)
                   targetX = maxXOffset;
             else if (targetX < minXOffset)
                   targetX = minXOffset;


             self.mainScrollView.contentOffset = CGPointMake(targetX, 0);
          }
   }];

这是一种动画!此代码在其他设备上正常工作!任何帮助?谢谢

it's kind of animation !!! this code works fine on other devices ! any help ?Thanks

推荐答案

您可以尝试以下方法:
这会将错误处理添加到您的代码中,作为可能会从陀螺仪返回错误,这可能会返回> 0.09的值;在测试时更频繁地使用NSLOG来挑选代码并查看返回的值。

could you try the following: This adds the error handling to your code, as an error may be returning from the gyroscope, and this may return a value >0.09; Use NSLOG more often when testing to pick apart your code and see what values are returning.

@property (strong, nonatomic) CMMotionManager *motionManager;


self.mainScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width,       self.view.frame.size.height);

self.mainScrollView.bounces = NO;


self.mainScrollView.userInteractionEnabled = NO;

//set up the image view
UIImage *image= [UIImage imageNamed:@"YOUR_IMAGE_NAME"];
UIImageView *movingImageView = [[UIImageView alloc]initWithImage:image];
[self.mainScrollView addSubview:movingImageView];

self.mainScrollView.contentSize = CGSizeMake(movingImageView.frame.size.width, self.mainScrollView.frame.size.height);


self.mainScrollView.contentOffset = CGPointMake((self.mainScrollView.contentSize.width - self.view.frame.size.width) / 2, 0);

//inital the motionManager and detec the Gyroscrope for every 1/60 second
//the interval may not need to be that fast
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.gyroUpdateInterval = 1/60;

//this is how fast the image should move when rotate the device, the larger the number, the less the roation required.
CGFloat motionMovingRate = 4;

//get the max and min offset x value
int maxXOffset = self.mainScrollView.contentSize.width - self.mainScrollView.frame.size.width;
int minXOffset = 0;

[self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
                            withHandler:^(CMGyroData *gyroData, NSError *error) {
// IF NO ERROR ---
if(!error){
NSLog(@"No error from Gyroscope %f",gyroData.rotationRate.y);
     if (fabs(gyroData.rotationRate.y) >= 0.1) {
NSLog(@"Moving image");
        CGFloat targetX = self.mainScrollView.contentOffset.x - gyroData.rotationRate.y * motionMovingRate;

         if(targetX > maxXOffset)
               targetX = maxXOffset;
         else if (targetX < minXOffset)
               targetX = minXOffset;


         self.mainScrollView.contentOffset = CGPointMake(targetX, 0);
      }
}
// ERROR returned from GYRO
else NSLog(@"error recieved %@",error);
}];

这篇关于有陀螺仪滚动图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆