NSView not rotating around center

458 views Asked by At

I'm trying to rotate a NSView around its center. But even if I change the anchorPoint, the NSView continue to rotate around its top left corner. Just a precision : I'm working on OSX 10.8.5.

Thanks in advance for your help.

Here is my code :

// myView.m
- (id)initWithFrame:(NSRect)rect
{
    if(self = [super initWithFrame:(NSRect)rect])
    {
        self.frame = rect;
        [self setWantsLayer:YES];
        self.layer.backgroundColor = [NSColor whiteColor].CGColor;
        self.layer.borderColor = [NSColor grayColor].CGColor;
        self.layer.borderWidth = 1.0;

        NSView *aView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 50, 50)];
        [aView setWantsLayer:YES];
        aView.layer.backgroundColor = [NSColor redColor].CGColor;
        aView.layer.anchorPoint = CGPointMake(0.5, 0.5);

        [self addSubview:aView];

        CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotateAnimation.byValue = [NSNumber numberWithFloat:2*M_PI];
        rotateAnimation.duration = 4;
        rotateAnimation.repeatCount = INFINITY;

        [aView.layer addAnimation:rotateAnimation forKey:@"rotationAnimation"];
    }
}

EDITED 30-11-2018 : I managed to get the centered rotation using the layers :

// myView.m
- (id)initWithFrame:(NSRect)rect
{
    if(self = [super initWithFrame:(NSRect)rect])
    {
        self.frame = rect;
        [self setWantsLayer:YES];
        self.layer.backgroundColor = [NSColor whiteColor].CGColor;
        self.layer.borderColor = [NSColor grayColor].CGColor;
        self.layer.borderWidth = 1.0;

        NSView *aView = [[NSView alloc] init];
        [aView setWantsLayer:YES];
        aView.layer.backgroundColor = [NSColor redColor].CGColor;
        aView.layer.bounds = CGRectMake(0, 0, 50, 50);
        aView.layer.frame = CGRectMake(0, 0, 50, 50);
        aView.layer.anchorPoint = CGPointMake(0.5, 0.5);
        aView.layer.position = CGPointMake(0, 0);

        [self.layer addSublayer:aView.layer];

        CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotateAnimation.byValue = [NSNumber numberWithFloat:2*M_PI];
        rotateAnimation.duration = 4;
        rotateAnimation.repeatCount = INFINITY;

        [aView.layer addAnimation:rotateAnimation forKey:@"rotationAnimation"];
    }
}
1

There are 1 answers

0
Christian Krueger On

If you want to rotate a view you can do:

-(void)rotateByCenter:(NSView*)aView {

[aView setWantsLayer:YES];
aView.layer.anchorPoint = CGPointMake(0.5, 0.5);
aView.layer.position = CGPointMake(aView.frame.origin.x + aView.frame.size.width/2.,aView.frame.origin.y + aView.frame.size.height/2.) ;

CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotateAnimation.byValue = [NSNumber numberWithFloat:2*M_PI];
rotateAnimation.duration = 20;
rotateAnimation.repeatCount = INFINITY;

[aView.layer addAnimation:rotateAnimation forKey:@"rotationAnimation"]; }