ios - 如何实现用户移动iOS设备时背景变化视点?

标签 ios core-motion motion

您可能知道,iOS 7 主屏幕可以执行视觉操作,例如,当用户移动设备时,背景也会移动。我如何在我的应用程序中做类似的事情?假设我有两个 ImageView 。一个在另一个之上。类似的事情如何实现?谢谢。

最佳答案

您可以在UIMotionEffectGroup的帮助下设置UIInterpolatingMotionEffectx和y排序。我只是在我的演示中测试了一段代码,我把它放在这里检查一下:-

- (void)viewDidLoad
{
        [super viewDidLoad];
        CGRect frame = self.view.bounds;
        for (NSInteger i=0; i<1; i++) {
            CGFloat left = (frame.size.width-100)/2+i%2*10;
            CGFloat top = (frame.size.height-100)/2+i%3*10;
            UIImageView *demoView = [[UIImageView alloc] initWithFrame:CGRectMake(left, top , 100, 100)];

            [demoView setImage:[UIImage imageNamed:@"FA4nH.jpg"]];

           // demoView.backgroundColor = [UIColor colorWithRed:i/12.0f green:i/18.0f blue:i/24.0f alpha:0.8];
            [self.view addSubview:demoView];
            UIInterpolatingMotionEffect *xAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
            UIInterpolatingMotionEffect *yAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];

            left += i*50;
            top += i *50;
            xAxis.minimumRelativeValue = @(-left);
            xAxis.maximumRelativeValue = @(left);
            yAxis.minimumRelativeValue = @(-top);
            yAxis.maximumRelativeValue = @(top);
            UIMotionEffectGroup *motionEffectGroup = [[UIMotionEffectGroup alloc] init];
            motionEffectGroup.motionEffects = @[xAxis, yAxis];
            [demoView addMotionEffect:motionEffectGroup];
        }

}

将其 checkin 到运行良好的真实设备中,我在我的一个演示项目中对此进行了测试。希望对您有帮助。

关于ios - 如何实现用户移动iOS设备时背景变化视点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22397971/

相关文章:

ios - 长时间触摸时增加跳跃高度,直到最大高度

ios - 从 Google 云端硬盘仅下载 82 kb 0f 3 MB?

raspberry-pi - 树莓派摄像头无法打开视频设备

ffmpeg - 如何从 Raspberry Pi 上的 Action 中获取 .mp4 视频?

ios - ATS 域异常(exception)的通配符?

jquery - 全屏覆盖打开时防止 body 在 iPhone 上滚动

ios - 在 iphone sdk 的屏幕中移动对象

swift - Apple Watch 和 Swift 在使用 CoreMotion 计算设备运动时崩溃

iOs 8、核心运动和骑行事件

iphone - 如何区分 iPhone 中的运动加速度与振动和噪音