Rotate NSImageView around it's center

Discussion in 'Mac Programming' started by Ides, Nov 14, 2013.

  1. Ides macrumors member

    Joined:
    Mar 27, 2012
    #1
    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:
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    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
     

Share This Page