Reputation: 1074
I applied a parallax effect to a view using this function I found in another question:
const static CGFloat kCustomIOS7MotionEffectExtent = 10.0;
- (void)applyMotionEffects:(UIView *)YOUR_VIEW
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
return;
}
UIInterpolatingMotionEffect *horizontalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalEffect.minimumRelativeValue = @(-kCustomIOS7MotionEffectExtent);
horizontalEffect.maximumRelativeValue = @( kCustomIOS7MotionEffectExtent);
UIInterpolatingMotionEffect *verticalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
verticalEffect.minimumRelativeValue = @(-kCustomIOS7MotionEffectExtent);
verticalEffect.maximumRelativeValue = @( kCustomIOS7MotionEffectExtent);
UIMotionEffectGroup *motionEffectGroup = [[UIMotionEffectGroup alloc] init];
motionEffectGroup.motionEffects = @[horizontalEffect, verticalEffect];
[YOUR_VIEW addMotionEffect:motionEffectGroup];
}
It worked fine, but what I noticed after logging the view's frame is that it doesn't change. How did they do that? Is there a particular reason? It makes it a lot easier to continue development but it just doesn't make sense, because the actual's view position changes as the parallax effect happens.
Upvotes: 2
Views: 491
Reputation: 23634
I believe this is because as with most animations, UIInterpolatingMotionEffect
uses the presentationLayer
of your view's layer
(the model layer) to do its animation. The attributes are typically applied to the model layer once an animation actually completes (and in this case, completion is actually just putting it back to starting position when the motion effect ends, so the actual model layer never changes).
Try checking the properties of the presentation layer.
CALayer *presentationLayer = [myView.layer presentationLayer];
CGRect frame = presentationLayer.frame;
CGPoint position = presentationLayer.position;
Upvotes: 3