Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Ides

macrumors member
Original poster
Mar 27, 2012
95
0
I want to rotate an NSImageView around its center. I use setFrameCenterRotation: to set the angle at which I want it to show. But the problem is, instead of rotating around its center like the documentation says it should, it is rotating around its origin. How can I get it to rotate in place around it's center? The image displayed by the imageview is circular so it should appear that the image hasn't changed it's location, merely rotated. But this is not the case, it is definitely not rotating around the center.

Edit: Also, the imageview's frame origin is changed when I call setFrameCenterRotation:
 

xStep

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
My OS X skills are rusty but I did a rough test and came up with the following using a NSView for the test. I couldn't find anything that worked with the view directly so had to use the layer of the view.

After the view is loaded, you need to reset the anchor point. The docs say it is {0.5, 0.5} by default but they seem to be lying.
Code:
CGRect frame = self.iv.layer.frame;
CGPoint center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
self.iv.layer.position = center;
self.iv.layer.anchorPoint = CGPointMake(0.5, 0.5);
self.iv.layer.borderColor = [NSColor redColor].CGColor;
self.iv.layer.borderWidth = 1.0;

I connected a slider up and did the following.
Code:
- (IBAction)mySlider:(NSSlider *)sender
{
    CGAffineTransform ourTransform= CGAffineTransformMakeRotation( ( [(NSSlider*)sender floatValue] * M_PI ) / 180 );
    [self.iv.layer setAffineTransform: ourTransform];
}

I did something like this a couple of weeks ago with iOS and it was much easier. Apple needs to adapt some of those techniques to OS X. :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.