Анимация Objective-c с проблемой нестабильности ограничений для конечного ограничения - PullRequest
0 голосов
/ 03 мая 2018

Я разрабатываю приложение для IOS с использованием target-c. Я столкнулся с проблемой нестабильности при анимации ограничений представления. Анимация работает хорошо в отношении конечной позиции кадра, но случайным образом конечное (правое) ограничение опускается для первого анимационного блока (левое ограничение никогда не достигает -20 постоянного значения), однако ведущие (левые), верхнее и нижнее ограничения работают как положено.

Пожалуйста, посмотрите видео, которое я загрузил. На первом касании анимация работает должным образом, но на втором касании возникает проблема. Пожалуйста, совет.

Видео, показывающее проблему

- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
// 1
UIView *containerView = transitionContext.containerView;
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

// 2
[containerView addSubview:toViewController.view];

if([toViewController isKindOfClass:[ImageViewController class]]){
    ImageViewController *vcImage = (ImageViewController*)toViewController;
    vcImage.constraintLeft.constant = 20;
    vcImage.constraintTop.constant = self.selectedCardFrame.origin.y + 60.0;
    vcImage.constraintRight.constant = 20.0;
    vcImage.constraintBottom.constant = 0;//self.CollectionView.bounds.size.height - (self.selectedCardFrame.origin.y + self.selectedCardFrame.size.height);
    [toViewController.view layoutIfNeeded];

    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [toViewController.view layoutIfNeeded];
    [UIView animateWithDuration:duration animations:^{
        //First animation block
        vcImage.constraintRight.constant = -20.0;
        vcImage.constraintLeft.constant = -20.0;
        vcImage.constraintTop.constant = -20.0;
        vcImage.constraintBottom.constant = 0;
        [toViewController.view layoutIfNeeded];
        //toViewController.configureRoundedCorners(shouldRound: false)
    } completion:^(BOOL finished) {
        //Second animation block
        [UIView animateWithDuration:duration animations:^{
            vcImage.constraintLeft.constant = 0;
            vcImage.constraintTop.constant = 0.0;
            vcImage.constraintRight.constant = 0;
            vcImage.constraintBottom.constant = 0;
            [toViewController.view layoutIfNeeded];
            //toViewController.configureRoundedCorners(shouldRound: false)
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:finished];
        }];
    }];
}

}

Обновлен код после комментария @Sh_Khan.

- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
// 1
UIView *containerView = transitionContext.containerView;
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

// 2
[containerView addSubview:toViewController.view];
if([toViewController isKindOfClass:[ImageViewController class]]){
    ImageViewController *vcImage = (ImageViewController*)toViewController;
    NSTimeInterval duration = [self transitionDuration:transitionContext];

    [UIView animateWithDuration:0 animations:^{
        vcImage.constraintLeft.constant = 20;
        vcImage.constraintTop.constant = self.selectedCardFrame.origin.y + 60.0;
        vcImage.constraintRight.constant = 20.0;
        vcImage.constraintBottom.constant = 0;

        [toViewController.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:duration animations:^{
            //First animation block
            vcImage.constraintRight.constant = -20.0;
            vcImage.constraintLeft.constant = -20.0;
            vcImage.constraintTop.constant = -20.0;
            vcImage.constraintBottom.constant = 0;
            [toViewController.view layoutIfNeeded];
            //toViewController.configureRoundedCorners(shouldRound: false)
        } completion:^(BOOL finished) {
            //Second animation block
            [UIView animateWithDuration:duration animations:^{
                vcImage.constraintLeft.constant = 0;
                vcImage.constraintTop.constant = 0.0;
                vcImage.constraintRight.constant = 0;
                vcImage.constraintBottom.constant = 0;
                [toViewController.view layoutIfNeeded];
                //toViewController.configureRoundedCorners(shouldRound: false)
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:finished];
            }];
        }];
    }];
}

}

1 Ответ

0 голосов
/ 05 мая 2018

Перед настройкой imageView попробуйте переориентировать фотографию. Что-то подсказывает мне, что при просмотре изображения трудно определить ориентацию изображения. У меня это было однажды во время перехода, и я объяснил, почему вы наблюдаете такое поведение лишь иногда. Я немного заржавел с Objective C, но сделайте это и используйте его перед установкой изображения imageView.

-(UIImage*)normalizeImage:(UIImage*)currentImage {
    if (currentImage.imageOrientation == UIImageOrientationUp){
        return currentImage;
    }

    UIGraphicsBeginImageContextWithOptions(currentImage.size, NO, currentImage.scale);
    [currentImage drawInRect:CGRectMake(0, 0, currentImage.size.width, currentImage.size.height)];
    UIImage *_newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return _newImage;
}
...