简体   繁体   中英

Calculating the CGAffineTransform matrix after rotating the superview

This one is tricky!

I have view A and view B which is a subview of view A. The frames of both views are constant.

I rotate view A by changing the transform-matrix:

viewA.transform = CGAffineTransformMakeRotation(angle);

Now I want to adjust the transform-matrix of view B, so that the visual position and rotation stays the same ie B must not move/rotate while changing the rotation angle of A.

Obviously I need to rotate view B in the opposite direction, but that is not enough.

I tried this but B still wobbles around:

viewB.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(-angle), viewA.bounds.size.width, viewA.bounds.size.height);

Maybe the correct matrix also depends on the frame of viewB ... maybe I need to use sine and cosine ... ?

I know, I could simply make B a sibling view of A, but I don't want to.

嗨,尝试一下,让我知道

viewB.transform=CGAffineTransformInvert(viewA.transform)

Two things that popped into my head.

First, why not remove view B from view A and attach it to the superview of view A (even if its just for the rotation sequence)? It will be visually identical.

My other thought is wrapping the animations in a CATransaction to keep the animations in sync.

I would still recommend not keeping view B on view A and having to run two animations, instead of just one. Is the reason you're keeping view B inside view A, because its clipping based on it's bounds?

This is with layers, but it works the same with views:

[CATransaction begin];
[CATransaction flush];
[CATransaction setAnimationDuration:2.0f];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
CATransform3D transform = CATransform3DMakeRotation(M_PI/4, 0, 0, 1);
outerLayer.transform = CATransform3DConcat(outerLayer.transform, transform);
innerLayer.transform = CATransform3DInvert(outerLayer.transform);
[CATransaction commit];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM