ios – 用陀螺仪滚动图像有问题

ios – 用陀螺仪滚动图像有问题,第1张

概述iPad Air有一个奇怪的问题! ,我的代码在iPad 3,iPad 4,iPhone 5S,iPod 5th Gen上运行良好,但在iPad上,我的图像自动滚动而无需用户旋转设备,这是我的代码: @property (strong, nonatomic) CMMotionManager *motionManager; self.mainScrollView.frame = CGRe iPad Air有一个奇怪的问题!,我的代码在iPad 3,iPad 4,iPhone 5S,iPod 5th Gen上运行良好,但在iPad上,我的图像自动滚动而无需用户旋转设备,这是我的代码:
@property (strong,nonatomic) cmmotionmanager *motionManager;    self.mainScrollVIEw.frame = CGRectMake(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);          }   }];

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

解决方法 你能尝试以下方法吗?
这会将错误处理添加到您的代码中,因为可能会从陀螺仪返回错误,并且这可能返回值> 0.09;在测试时更频繁地使用NSLOG来挑选代码并查看返回的值.
@property (strong,nonatomic) cmmotionmanager *motionManager;self.mainScrollVIEw.frame = CGRectMake(0,self.vIEw.frame.size.height);self.mainScrollVIEw.bounces = NO;self.mainScrollVIEw.userInteractionEnabled = NO;//set up the image vIEwUIImage *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 fastself.motionManager = [[cmmotionmanager alloc] init];self.motionManager.gyroUpdateInterval = 1/60;//this is how fast the image should move when rotate the device,the less the roation required.CGfloat motionMovingRate = 4;//get the max and min offset x valueint 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 GYROelse NSLog(@"error recIEved %@",error);}];
总结

以上是内存溢出为你收集整理的ios – 用陀螺仪滚动图像有问题全部内容,希望文章能够帮你解决ios – 用陀螺仪滚动图像有问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1104824.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存