Question

I'm trying to create an animation for a layer where the bottom of the layer flips up. Think of a view hinged on the top, then the botton animates out and up. Like the cover of a compact. What I want is like the standard flip animation but to only see one side of it. I'm just having trouble structuring the animation.

The problem is I got the animation right accept I can't seem to create the 'hinge' part. That is, the hinge is in the center instead of the top. What am I missing?

Here's the code:

   [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
                CATransform3D frontTransform = CATransform3DIdentity;
                frontTransform.m34 = 1.0 / -850.0; 
                CATransform3D rotation = CATransform3DRotate(frontTransform, M_PI/2, 1.0, 0.0, 0.0);
                frontTransform = CATransform3DTranslate(frontTransform, 0.0, 0.0, menuView.bounds.size.height);
                frontTransform = CATransform3DConcat(rotation, frontTransform);
                menuView.layer.transform = frontTransform;

            }completion:^(BOOL finished){
                if (finished) {
                    [self.window sendSubviewToBack:menuView];
                    CATransform3D backTransform = CATransform3DIdentity;
                    backTransform.m34 = 0.0f; //Must be first before setting any other properties.
                    backTransform = CATransform3DRotate(backTransform, M_PI, 0.0, 0.0, 0.0);
                    backTransform = CATransform3DTranslate(backTransform, 0.0, 0.0, 0.0);
                    menuView.layer.transform = backTransform;
                }
            }];

I'm trying to animate the layer as described above, then send the layer's view behind all other views on completion, and return the layer to it's original state so I can bring it to the front later.

Thanks for the help!

Was it helpful?

Solution 2

Fixed the problem. I had to set both the layer's anchor point and position properties.

menuView.layer.position = CGPointMake(menuView.frame.size.width/2, 0.0);
menuView.layer.anchorPoint = CGPointMake(0.5, 0.0);


[UIView animateWithDuration:0.8 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                menuView.layer.magnificationFilter = kCAFilterNearest;
                menuView.layer.edgeAntialiasingMask = kCALayerBottomEdge;

                CATransform3D backTransform = CATransform3DIdentity;
                backTransform.m34 = 0.0f; 
                backTransform.m43 = 0.0f;
                backTransform = CATransform3DTranslate(backTransform, 0.0, 0.0, 0.0);
                backTransform = CATransform3DRotate(backTransform, M_PI/2, 0.0, 0.0, 0.0);
                menuView.layer.transform = backTransform;                   

            }completion:^(BOOL finished){
                if (finished) {
                    NSLog(@"Position - Show Second: %@", NSStringFromCGPoint(menuView.layer.position));
                    NSLog(@"AnchorP - Show Second: %@", NSStringFromCGPoint(menuView.layer.anchorPoint));
                }
            }];

OTHER TIPS

Have you tried to set CALayer's anchor point to the top? Here at top middle point:

menuView.layer.anchorPoint = CGPointMake(CGRectGetWidth(menuView.bounds)/2, 0.0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top