zoukankan      html  css  js  c++  java
  • IOS 特定于设备的开发:获取和使用设备姿势(通过手机方向控制3d物体显示)

    利用设备的机载陀螺仪可以实现,当你旋转手机屏幕时,里面的画面不会随着视图更新而移动,以平衡物理运动。

    下面例子利用少量简单的几何变换执行该操作。他建立一个运动管理器,订阅设备运动更新,然后基于运动管理器返回的摇晃,前倾和左右摇摆的角度应用图像变换。

    @implementation TestBedViewController
    {
        CMMotionManager *motionManager;
        UIImageView *imageView;
    }
    
    - (void) shutDownMotionManager
    {
        NSLog(@"Shutting down motion manager");
        [motionManager stopDeviceMotionUpdates];
        motionManager = nil;
    }
    
    - (void) establishMotionManager
    {
        if (motionManager)
            [self shutDownMotionManager];
        
        NSLog(@"Establishing motion manager");
        
        // Establish the motion manager
        motionManager = [[CMMotionManager alloc] init];
        if (motionManager.deviceMotionAvailable)
            [motionManager
             startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
             withHandler: ^(CMDeviceMotion *motion, NSError *error) {
                 CATransform3D transform;
                 transform = CATransform3DMakeRotation(motion.attitude.pitch, 1, 0, 0);
                 transform = CATransform3DRotate(transform, motion.attitude.roll, 0, 1, 0);
                 transform = CATransform3DRotate(transform, motion.attitude.yaw, 0, 0, 1);
                 imageView.layer.transform = transform;
             }];
    }
    
    - (void) viewDidAppear: (BOOL) animated
    {
        NSString *imageName = IS_IPAD ? @"iPadArt.png" : @"iPhoneArt.png";
        UIImage *image = [UIImage imageNamed:imageName];
        imageView = [[UIImageView alloc] initWithImage:image];
        imageView.center = RECTCENTER(self.view.bounds);
        [self.view addSubview:imageView];
    }
    
    - (void) loadView
    {
        [super loadView];
        self.view.backgroundColor = [UIColor whiteColor];
    }
  • 相关阅读:
    calendar的用法
    为什么要初始化变量呢
    什么情况下,if()后面的else可以省略不写
    new 对象的时候,括号里有值和无值的区别
    if...else...;if..else if...else
    java-csv导出-导出文件显示的日期格式不正确
    使用Navicat连接MySQL,连接失败(报1064错误)
    MySQL下载安装详解(win10)
    Syntax error, parameterized types are only available if source level is 1.5 or greater
    eclipse ant出错问题
  • 原文地址:https://www.cnblogs.com/haibosoft/p/4179814.html
Copyright © 2011-2022 走看看