ios 加速计效果实现

在ios7中引入了运动效果,它可以把设备加速计上面所发生的事件同UIKit的值关联起来。开发者只需要创建UIMotionEffect子类的实例(目前系统只提供了一种UIMotionEffect子类,就是UIInterpolationMotionEffect),我们可以通过它来实现效果,但是要注意,加速计在真机上才有效果,在模拟器上是没有效果的。

简单的代码如下:

- (void)startMotionEffects
{
    UIInterpolatingMotionEffect *motionEffectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    UIInterpolatingMotionEffect *motionEffectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    motionEffectX.minimumRelativeValue = @-150;
    motionEffectX.maximumRelativeValue = @150;
    motionEffectY.minimumRelativeValue = @-150;
    motionEffectY.maximumRelativeValue = @150;
    UIMotionEffectGroup *motionEffectGroup = [[UIMotionEffectGroup alloc] init];
    motionEffectGroup.motionEffects = @[motionEffectX,motionEffectY];
    [_imageView addMotionEffect:motionEffectGroup];
}

你可能感兴趣的:(iOS)