iOS 7 parallax effect in my view controller

前端 未结 9 2082
终归单人心
终归单人心 2020-11-30 16:27

I\'m developing an app for iOS 7 in Objective-C. I\'ve got a screen in my app with a few buttons and a pretty background image. (It\'s a simple xib with UIButtons

9条回答
  •  不知归路
    2020-11-30 16:42

    Here is an easy category to integrate the effect on iOs7+ :

    NSString *const centerX = @"center.x";
    NSString *const centerY = @"center.y";
    
    //#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    
    @implementation UIView (TLMotionEffect)
    
    - (void)addCenterMotionEffectsXYWithOffset:(CGFloat)offset {
    
    //    if(!IS_OS_7_OR_LATER) return;
        if(floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) return;
    
        UIInterpolatingMotionEffect *xAxis;
        UIInterpolatingMotionEffect *yAxis;
    
        xAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:centerX type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
        xAxis.maximumRelativeValue = @(offset);
        xAxis.minimumRelativeValue = @(-offset);
    
        yAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:centerY type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
        yAxis.minimumRelativeValue = @(-offset);
        yAxis.maximumRelativeValue = @(offset);
    
        UIMotionEffectGroup *group = [[UIMotionEffectGroup alloc] init];
        group.motionEffects = @[xAxis, yAxis];
    
        [self addMotionEffect:group];
    }
    
    @end
    

    https://github.com/jvenegas/TLMotionEffect

提交回复
热议问题