NSView не вращается вокруг центра

Я пытаюсь повернуть NSView вокруг его центра. Но даже если я изменю anchorPoint, NSView продолжит вращаться вокруг своего верхнего левого угла. Просто уточнение: я работаю над OSX 10.8.5.

Заранее спасибо за помощь.

Вот мой код:

// 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: мне удалось получить центрированное вращение, используя слои:

// 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"];
    }
}

person Jeff Dolphin    schedule 26.11.2018    source источник
comment
Очень полезный пример! Для новичков я хотел бы добавить 2 вещи: 1.) для запуска необходимо добавить библиотеку фреймворка Quartz. 2.) Используя пример кода Джеффа, представление myView должно быть добавлено в любое окно.   -  person Christian Krueger    schedule 22.03.2019


Ответы (1)


Если вы хотите повернуть вид, вы можете сделать:

-(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"]; }
person Christian Krueger    schedule 22.03.2019