简体   繁体   中英

Rotate UIView with offset

I am writing a MonoTouch iOS application.

I have a group of 4 UIButtons on my main View. They are more or less in a horizontal row. I want to rotate them by an angle (between -90 and +90 degrees) as a group with the right most button being the "anchor" for the group of buttons.

I can't figure out how to do this.

One thing I tried was I put these buttons inside of a UIView and then set the Transform to a CGAffineTransform.MakeRotation, but it rotates around the center of the UIView. I also tried making this parent UIView be wider than it needed to be so that the right most button in the group was in the center of the UIView. This worked for the rotation, but then the large blank section that was the right half of the parent UIView covers up other buttons that are on my main View that I need to be visible.

So my question is how do rotate UIButtons of a UIView where the rotation is around a point that is NOT the center of the UIButton or UIView?

You can change the AnchorPoint of the view's layer, along with the Position to compensate for the shift due to setting the AnchorPoint. For example, say you had a view named ButtonGroup containing the buttons. The following code would rotate them 90 degrees about the lower left corner of the view:

ButtonGroup.Layer.Position = new PointF(ButtonGroup.Frame.Left, ButtonGroup.Frame.Bottom);
ButtonGroup.Layer.AnchorPoint = new PointF (0,1);            
ButtonGroup.Layer.AffineTransform = CGAffineTransform.MakeRotation((float)Math.PI/-2);

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