iOS 7 parallax effect in my view controller

前端 未结 9 1874
终归单人心
终归单人心 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:48

    Translated to swift in case anyone is lazy. Please vote @veducm answer up if you found this useful

    @IBOutlet var background : UIImageView!
    
    func parallaxEffectOnBackground() {
        let relativeMotionValue = 50
        var verticalMotionEffect : UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y",
            type: .TiltAlongVerticalAxis)
        verticalMotionEffect.minimumRelativeValue = -relativeMotionValue
        verticalMotionEffect.maximumRelativeValue = relativeMotionValue
    
        var horizontalMotionEffect : UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x",
            type: .TiltAlongHorizontalAxis)
        horizontalMotionEffect.minimumRelativeValue = -relativeMotionValue
        horizontalMotionEffect.maximumRelativeValue = relativeMotionValue
    
        var group : UIMotionEffectGroup = UIMotionEffectGroup()
        group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]
    
        self.background.addMotionEffect(group)
    }
    
    0 讨论(0)
  • 2020-11-30 16:49

    @veducm's solution can be a little shorter. The UIMotionEffectGroup for its x and y motion is obsolete if you add the the x and y-axis motionEffects separately.

    UIInterpolatingMotionEffect *motionEffect;
    motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
                                                                   type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    motionEffect.minimumRelativeValue = @(-25);
    motionEffect.maximumRelativeValue = @(25);
    [bgView addMotionEffect:motionEffect];
    
    motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
                                                                   type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    motionEffect.minimumRelativeValue = @(-25);
    motionEffect.maximumRelativeValue = @(25);
    [bgView addMotionEffect:motionEffect];
    
    0 讨论(0)
  • 2020-11-30 16:51

    UIMotionEffect provides a free parallax implementation on iOS 7.

    http://www.teehanlax.com/blog/introduction-to-uimotioneffect/

    https://github.com/michaeljbishop/NGAParallaxMotion lets you just set the parallax intensity.

    0 讨论(0)
提交回复
热议问题