Search code examples
iosswiftanimationcore-graphics

Animating UIView from Rectangle to Trapezoidal


Please have a look at attached pic

enter image description here

Say I have a UIView as in first image and on user interaction, the rectangle of UIView should shape shift to trapezoidal.

I am exploring my options on how to work on to get this effect. Whether to use core graphics to draw my view or use core animation. But using core animation can a rectangle view be animated to view shape other than rectangle or square?

I can draw a trapezoidal view using core graphic but how to animate the rectangle UIView to a trapezoidal view.

Would be helpful if someone can provide a thought on how to achieve this effect.

Thanks


Solution

  • This should get you started:

    var transform3D = CATransform3DIdentity
    transform3D.m34 = -1.0 / 1000
    transform3D = CATransform3DRotate(transform3D, CGFloat(M_PI) / 4, 0, 1, 0)
    
    UIView.animate(withDuration: 1.5, animations: {
    
        theView.layer.transform = transform3D
    
    })