CABasicAnimation игнорируется во время вращения

У меня есть UIView, который в методе layoutSubviews перемещает свои подпредставления в зависимости от ориентации iPad. В методе layoutSubviews у меня есть CABasicAniamtion, который должен анимировать изменение положения подвидов. Для анимации установлена ​​определенная продолжительность, но эта продолжительность игнорируется, и изменение положения происходит немедленно. Я знаю, что анимация запускается, потому что я вижу запуск методов AnimationDidStart и AnimationDidStop. Я знаю, что это как-то связано с CALayers UIView, но я не могу найти в Интернете ничего, чтобы объяснить, как это исправить. Любая помощь будет оценена по достоинству.

    if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
    {
        NSLog(@"Orientation: Portrait");

        //Hide icon

        CABasicAnimation *iconAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        iconAnimation.fromValue = [NSValue valueWithCGPoint:[iconImageView center]];
        iconAnimation.toValue = [NSValue valueWithCGPoint:iconThinPosition];
        iconAnimation.duration = 2.7f;
        iconAnimation.autoreverses = NO;
        iconAnimation.repeatCount = 1;
        iconAnimation.delegate = self;
        [iconImageView.layer addAnimation:iconAnimation forKey:@"position"];
        //[iconImageView setCenter:iconThinPosition];

        [iconImageView.layer setPosition:iconThinPosition];
        //[iconImageView setTransform: CGAffineTransformIdentity];

        CABasicAnimation *textAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        textAnimation.fromValue = [NSValue valueWithCGPoint:[textImageView center]];
        textAnimation.toValue = [NSValue valueWithCGPoint:textThinPosition];
        textAnimation.duration = 2.7f;
        textAnimation.autoreverses = NO;
        textAnimation.repeatCount = 1;
        textAnimation.delegate = self;
        [textImageView.layer addAnimation:textAnimation forKey:@"position"];        
        [textImageView.layer setPosition:textThinPosition];

    }
    else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 
    {
        NSLog(@"Orientation: Landscape");        

        // Show Icon
        CABasicAnimation *iconAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        iconAnimation.fromValue = [NSValue valueWithCGPoint:[iconImageView center]];
        iconAnimation.toValue = [NSValue valueWithCGPoint:iconShownPosition];
        iconAnimation.duration = 2.7f;
        iconAnimation.autoreverses = NO;
        iconAnimation.repeatCount = 1;
        iconAnimation.delegate = self;
        [iconImageView.layer addAnimation:iconAnimation forKey:@"position"];
        [iconImageView.layer setPosition:iconShownPosition];

        CABasicAnimation *textAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        textAnimation.fromValue = [NSValue valueWithCGPoint:[textImageView center]];
        textAnimation.toValue = [NSValue valueWithCGPoint:textShownPosition];
        textAnimation.duration = 2.7f;
        textAnimation.autoreverses = NO;
        textAnimation.repeatCount = 1;
        textAnimation.delegate = self;
        [textImageView.layer addAnimation:textAnimation forKey:@"position"];
        [textImageView.layer setPosition:textShownPosition];
    }
}

person Mike Murphy    schedule 23.09.2010    source источник


Ответы (1)


Интересно, не игнорируется ли это так сильно, поскольку уже выполняется анимационная транзакция, когда вызывается ваш layoutSubviews. Одна вещь, которую вы можете попытаться подтвердить, это переопределить -didRotateFromInterfaceOrientation и вызвать оттуда ваши layoutSubviews. Посмотрите, оживятся ли тогда ваши взгляды.

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//    [self layoutSubviews];
    // Give it a second to settle after the rotation and then call
    // layoutSuviews explicitly.
    [self performSelector:@selector(layoutSubviews) withObject:nil afterDelay:1.0f];
}

Еще одна вещь, о которой следует подумать, это то, что, поскольку вы анимируете только положение слоя UIView, вы можете использовать анимацию UIView вместо явной анимации. Что-то типа:

    if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
    {
        NSLog(@"Orientation: Portrait");

        //Hide icon
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:2.7f];
        [iconImageView setCenter:iconThinPosition];
        [textImageView setCenter:textThinPosition];
        [UIView commitAnimations];

    }
    else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 
    {
        NSLog(@"Orientation: Landscape");        

        // Show Icon
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:2.7f];
        [iconImageView setCenter:iconShownPosition];
        [textImageView setCenter:textShownPosition];
        [UIView commitAnimations];

    }
person Matt Long    schedule 05.10.2010