Search code examples
iphoneopengl-esuiscrollviewcore-animationquartz-graphics

Curving/warping views with CoreAnimation or OpenGL for carousel effect


Right now I'm populating a UIScrollView with a series of views. The views need to be warped to make the UIScrollView appear like a carousel. In other words when the user scrolls it needs to be like a circle. I've never done anything quite like this before, but I'm assuming CoreAnimation is out of the question and OpenGL needs to be used. If this is possible with CoreAnimation or Quartz then I really just need a sample on how to warp the views and I can figure the rest out myself but I'm not familiar with OpenGL.

alt text


Solution

  • In my experience, it is a bit tricky to get the values right. The way to give a view perspective is by manipulating it's layer transform. I have used the following method to achieve the transfor for a similar effect:

    -(CATransform3D)makeTransformForAngle:(CGFloat)angle from:(CATransform3D)start{
    
        CATransform3D transform = start;
    
    
        // the following two lines are the key to achieve the perspective effect
    CATransform3D persp = CATransform3DIdentity;
        persp.m34 = 1.0 / -1000;
    
        transform = CATransform3DConcat(transform, persp);
            transform = CATransform3DRotate(transform,angle, 0.0, 1.0, 0.0);
        return transform;
    }
    

    This was done to create a "flip page" animation, so you may need to adapt. To use it, do the following:

    flip_page.layer.transform = [self makeTransformForAngle:angle from:CATransform3DIdentity];
    

    where flip_page is a UIView. Cheers!