Search code examples
iphonexcodeuiviewcgaffinetransformskew

Get the current skew angle of an UIView


How can I get the current angle of a skewed image?

The code I use to skew it is:

CGAffineTransform t = CGAffineTransformMake(1.0, 0.0, tan(angle*(M_PI/480)), 1.0, 0.0, 0.0);

Solution

  • Try this:

    CGFloat skew_c = [(NSNumber *)[view valueForKeyPath:@"layer.transform.c"] floatValue];
    

    Keypath is guesstimated, but should work. CGAffineTransformMake docs.

    EDIT:

    Looks like CGAffineTransform is translated into 4x4 CATransform3D matrix.

    transform is property of CALayer of CATransform3D type.

    That means you can acces m11..m44 parameters with calls like:

    CGFloat m11 = [(NSNumber *)[view valueForKeyPath:@"layer.transform.m11"] floatValue];
    ...
    ...
    CGFloat m44 = [(NSNumber *)[view valueForKeyPath:@"layer.transform.m44"] floatValue];
    

    You'd still need to do some calculations to get your skew factor. You can derive equation for your c_factor from this great answer.