您可能知道,iOS 7 主屏幕可以执行视觉操作,例如,当用户移动设备时,背景也会移动。我如何在我的应用程序中做类似的事情?假设我有两个 ImageView 。一个在另一个之上。类似的事情如何实现?谢谢。
最佳答案
您可以在UIMotionEffectGroup
的帮助下设置UIInterpolatingMotionEffect
x和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/