UIView动画曲线不起作用,无法实现easeinout效果。

5

我被一个非常简单的任务难住了,我想要一个UIView动画来实现缓入/缓出效果,但是尽管使用了正确的animationOption,它始终是线性的。

这里是代码,按照所有账户应该可以工作,它设置在一个UIViewControllerAnimatedTransitioning类中,但是我在之前的自定义segue类中也遇到了同样的问题;

[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^(void) {

        self.dimView.alpha = 1.0;
        self.imageView.frame = self.destinationRect;

    } completion:^(BOOL finished) {

        [self.imageView removeFromSuperview];
        [self.dimView removeFromSuperview];

        [transitionContext completeTransition:YES];

     }];

然而,以下代码的弹簧动画可以工作,尽管我不想这样做。
[UIView animateWithDuration:0.8f delay:0 usingSpringWithDamping:0.7f initialSpringVelocity:2.0f options:UIViewAnimationOptionCurveEaseInOut animations:^(void) {

        self.dimView.alpha = 1.0;
        self.imageView.frame = self.destinationRect;

     } completion:^(BOOL finished){

        [self.imageView removeFromSuperview];
        [self.dimView removeFromSuperview];

        [transitionContext completeTransition:YES];

     }];

我在模拟器和iPad2测试设备上都遇到了同样的问题。 我做错了什么吗? 在动画帧或透明度值方面有什么问题吗?

3个回答

3
你可以尝试这个方法。
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.4f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop)];

self.dimView.alpha = 1.0;
self.imageView.frame = self.destinationRect;

[UIView commitAnimations];

- (void) animationDidStop {
    [self.imageView removeFromSuperview];
    [self.dimView removeFromSuperview];
    [transitionContext completeTransition:YES];
}

希望能对您有所帮助。

谢谢ssantos。起初我以为它解决了问题,但是我的眼睛欺骗了我。依然是线性的。非常奇怪和恼人。 - Daniel Nordh
确实有点奇怪,也许距离太小以至于看不出差别?我建议尝试使用不同的“animationCurves”来创建一个“大型”的动画。 - ssantos

1
你可以使用以下代码:

[self transition:left]; //根据需要调用

enum animationFrom {
    top,
    bottom,
    left,
    right
};

- (void)transition:(NSUInteger)index {
    CATransition *tr=[CATransition  animation];
    tr.duration=0.45;
    tr.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    tr.type=kCATransitionPush;
    tr.delegate=self;
    switch (index) {
        case top:
            tr.subtype=kCATransitionFromBottom;
            break;
        case bottom:
            tr.subtype=kCATransitionFromTop;
            break;
        case left:
            tr.subtype=kCATransitionFromRight;
            break;
        case right:
            tr.subtype=kCATransitionFromLeft;
            break;
        default:
            break;
    }
    [self.view.layer addAnimation:tr forKey:nil];
}

1
默认的动画曲线选项是UIViewAnimationOptionCurveEaseInOut,因此您不需要指定该选项。而UIViewAnimationOptionCurveLinear则需要您明确说明。
您确定您的动画曲线是线性的吗?尝试将两个视图并排动画,将动画持续时间设置为几秒钟。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接