使用陀螺仪滚动图像时遇到问题

2023-11-26

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

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


你可以尝试以下方法吗: 这会将错误处理添加到您的代码中,因为陀螺仪可能会返回错误,并且这可能会返回 >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);
}];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用陀螺仪滚动图像时遇到问题 的相关文章

随机推荐

  • 如何使用 Hibernate 注释在连接表上创建索引?

    我使用 Hibernate 注释有如下关系 这是我尝试过的 public class Job OneToMany cascade CascadeType ALL JoinTable name jobs resource locations
  • 使用 pandas read_csv 时出现内存错误

    我正在尝试做一些相当简单的事情 将一个大的 csv 文件读入 pandas 数据帧 data pandas read csv filepath header 0 sep DELIMITER skiprows 2 该代码要么失败 并显示Mem
  • 最小成本流 - R 中的网络优化

    我正在尝试实施一个 最低成本网络流量 交通问题解决方案R 我知道这可以使用类似的东西从头开始实现lpSolve 不过我发现有一个方便的igraph实施 最大流量 这样一个预先存在的解决方案会方便很多 但我找不到最小成本的等效函数 有没有一个
  • 如何让 JTable 标题显示整个列名而不是缩短它们?

    我有一个 JTable 其中填充了一个扩展 AbstractTableModel 的类 问题是列太短而无法显示列名称 我已经关闭了 Jtable AutoResizeMode 属性 但缩短仍然发生 我该如何解决这个问题 您可以使用桌柱调节器
  • 从 Bash 目录中读取文件名

    我需要编写一个脚本 从目录中读取所有文件名 然后根据文件名 例如 如果它包含 R1 或 R2 它将连接所有包含的文件名 例如名称中的 R1 谁能给我一些如何做到这一点的提示 我唯一能做的就是 bin bash FILES path to t
  • 使用 boto3 列出具有特定应用程序标签的自动缩放组名称

    我试图获取应用程序标记值为 CCC 的自动缩放组 名单如下 gweb prd dcc eap w2 gweb prd dcc emc gweb prd dcc ems CCC dev ccc wer CCC dev ccc gbg CCC
  • Facebook Graph API 覆盖显式设置的隐私设置

    我在为我的应用程序代表用户创建的帖子设置隐私时遇到问题 问题是 所有帖子的隐私值都被 Graph API 设置为 ALL FRIENDS 即使我明确将隐私值设置为 EVERYONE 这是我用来提交的代码 query message urle
  • iOS启动后台线程

    我的 iOS 设备中有一个小型 sqlitedb 当用户按下按钮时 我从 sqlite 获取数据并将其显示给用户 我想在后台线程中执行此获取部分 以免阻塞 UI 主线程 我这样做 self performSelectorInBackgrou
  • 具有本地作用域的 PHP 常量

    在 PHP 中是否可以有局部作用域的常量 是的 请提供一个小例子 是的 但只使用一个类 class Foo const BAR hello world print Foo BAR 关于 Kalium 的评论 如果你使用 PHP 5 3 你确
  • 何时使用 URL/会话/cookie 变量?

    我做了很多 php 和 javascript 但我认为这是一个相对与语言无关的问题 是否有关于何时使用以下各项的最佳实践 网址变量 会话变量 cookies 我了解其中一些的固有局限性can t确实如此 但似乎它们的用途有时也可以重叠 并且
  • C++11 中的动态对齐内存分配

    posix memalign and aligned malloc在 Windows 上允许动态分配对齐的内存块 C 11中有类似的东西吗 据我所知 alignas关键字仅适用于静态分配的对象 这取决于您需要什么对齐方式 对于任何 alig
  • 超越 Python 中的工厂

    从 Java 转向 Python 后 我被告知工厂不是 Pythonic 的 因此 我正在寻找 a Python 方法来执行如下操作 我过于简化了我的目标 这样我就不必描述我的整个程序 这非常复杂 我的脚本将读取人名 以及有关他们的一些信息
  • Rails 3:belongs_to、has_one 和迁移

    我是 Rails 新手 并且有 Django 背景 我已经接受了这样一个事实 模型和数据库模式在 Rails 在线 Django 中是分开的 然而 我仍在处理迁移问题 我的问题相当简单 如何使用迁移向模型添加关系 例如 我有Artist a
  • 在 matplotlib 中反转(不是反转)颜色图?

    有没有什么好的方法invert 不是反向 这很简单 请参阅在 matplotlib 中反转颜色图 matplotlib 中的颜色图 即 我想拍摄一张 png 图像 其中具有以下形式的颜色元组 1 1 0 1 并使用 matplotlib 颜
  • 如何在 Aurelia 入门应用程序(导航应用程序)中使用 JQuery UI 组件

    我可以按照入门教程中提供的步骤运行 Aurelia 应用程序 他们在骨架应用程序中使用了引导导航栏 是否可以在 Aurelia 应用程序中使用 JQuery UI 组件 如果是 请解释一下如何实现这一目标 提前致谢 是的 这是可能的 我做了
  • 在 Python 3.4 中导入表格时出现问题

    我刚刚安装了 python 的 tabulate 以便在终端中对输出进行制表 当我尝试将 tabulate 导入 python 3 4 时 它给我一个错误 ImportError No module named tabulate 但是 每当
  • VS.NET:项目引用与程序集引用

    关于从其他项目引用我们的公共代码库哪个更好 通过项目还是通过程序集 存在一些争论 我赞成引用该项目 特别是因为我们有自动化的单元测试 可以证明通用代码可以完成所需的工作 另一个阵营的想法是锁定这些项目 每月只发布一次程序集或类似的东西 然后
  • 导航栏隐藏时的假状态栏颜色

    我遇到了状态栏和导航栏的问题 默认情况下导航栏UINavigationController延伸到状态栏后面并为其着色 左屏幕截图 当使用搜索栏时 我隐藏导航栏 这会导致状态栏不着色 Apple 的邮件应用程序不存在此问题 除了创建一个带有背
  • 设置enforce_available_locales = true后,Rails I18n验证弃用警告

    你把这样的东西放在config application rb I18n enforce available locales true config i18n load path Dir Rails root join config loca
  • 使用陀螺仪滚动图像时遇到问题

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