【问题标题】:having issue with scrolling image with gyroscope使用陀螺仪滚动图像时遇到问题
【发布时间】:2014-08-13 23:48:00
【问题描述】:

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

 @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);
          }
   }];

这是一种动画!此代码在其他设备上运行良好!有什么帮助吗?谢谢

【问题讨论】:

  • 您的 iPad 陀螺仪在其他应用程序中是否正常运行?我曾经遇到过类似的问题,证明是硬件故障引起的
  • 您的 iPad 运行的是 iOS 8 beta 1 还是 2?我在运行这些测试版的其他应用程序中遇到了陀螺仪的大问题
  • @Chrene 不,这是 iOS 7 !
  • 您是否尝试过使用另一台 iPad?也许很明显,但这是确定您是否遇到硬件问题的一种非常好的方法
  • 您能否记录您的陀螺仪数据 - 您的代码认为它 >= 0.1 NSLog(@"%f",gyroData.rotationRate.y);

标签: ios objective-c iphone ipad


【解决方案1】:

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

@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);
}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多