为什么我的 CATransitions 不起作用?

发布于 2024-10-08 20:31:07 字数 902 浏览 8 评论 0原文

我使用以下代码通过 CATransition 在视图之间切换。

CATransition *applicationLoadViewIn = [CATransition animation];

    [applicationLoadViewIn setDuration:20];

    [applicationLoadViewIn setType:kCATransitionPush];

    [applicationLoadViewIn setSubtype:kCATransitionFromTop];

    [applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];

    ViewToSwitchTo *myviewcontroller = [[ViewToSwitchTo alloc] init];

    [self.view.layer addAnimation:applicationLoadViewIn forKey:kCATransitionPush];

    [self.view addSubview:myviewcontroller.view];

它主要按照我想要的方式运行。它像它应该的那样从顶部推动,但由于某种原因它的行为很奇怪。首先,我要切换到的视图开始像它应该的那样从底部进入,但由于某种原因,我要切换的视图以低不透明度出现在它的顶部,因此您可以看到它们两个。但是,您还会看到传入的视图,在其自身和另一个视图之上向上移动了大约 100 像素,同样具有低不透明度。就在转换的中间点之前,一切正常,您只能看到进入的视图和离开的视图,做它们应该做的事情。但在中间点之后不久,我要切换到的视图出现在其最终目的地,位于我要切换的视图下方,并且我要切换的视图的不透明度已降低。

这是怎么回事?

I am using the following code to switch between views with CATransition.

CATransition *applicationLoadViewIn = [CATransition animation];

    [applicationLoadViewIn setDuration:20];

    [applicationLoadViewIn setType:kCATransitionPush];

    [applicationLoadViewIn setSubtype:kCATransitionFromTop];

    [applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];

    ViewToSwitchTo *myviewcontroller = [[ViewToSwitchTo alloc] init];

    [self.view.layer addAnimation:applicationLoadViewIn forKey:kCATransitionPush];

    [self.view addSubview:myviewcontroller.view];

It functions mostly how I want it to. It pushes from the top like it should, however it for some reason acts strangely. First, the view I am switching to starts coming in from the bottom like it should, but for some reason, the view that I am switching FROM appears over the top of it with low opacity, so you see both of them. However, you also see the view that is coming in, shifted maybe 100 pixels upwards, on top of itself and the other view, once again with low opacity. Just before the halfway point of the the transition, everything works fine, you only see the view that is coming in and the view going out, doing what they should be doing. But slightly after the halfway point, the view I am switching to appears in its final destination, under the view I am switching from, and the view I am switching from has been reduced in opacity.

What is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦纸 2024-10-15 20:31:07

我真的不明白你想要什么,但希望这段代码能够帮助实现,如果没有,那么告诉你更多关于你的 CATransition

-(void) startAnimation {
    fullImageView.alpha = 0.50;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pulseAnimationDidStop:finished:context:)];
    fullImageView.alpha = 1.0;
    [UIView commitAnimations];
}

- (void)pulseAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if(hasFocus) {
        [self startAnimation];
    } else {
        fullImageView.alpha = 1.0;
    }
}

-(void) setHasFocus:(BOOL)_hasFocus {
    hasFocus = _hasFocus;
    if(hasFocus) {
        [self startAnimation];
    }
}

I really not understand what you want but hope this code will help to implement ,if not then tell more about you CATransition

-(void) startAnimation {
    fullImageView.alpha = 0.50;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pulseAnimationDidStop:finished:context:)];
    fullImageView.alpha = 1.0;
    [UIView commitAnimations];
}

- (void)pulseAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if(hasFocus) {
        [self startAnimation];
    } else {
        fullImageView.alpha = 1.0;
    }
}

-(void) setHasFocus:(BOOL)_hasFocus {
    hasFocus = _hasFocus;
    if(hasFocus) {
        [self startAnimation];
    }
}
回忆躺在深渊里 2024-10-15 20:31:07

你也可以使用这种方式

CATransition *applicationLoadViewIn = [CATransition animation];
    [applicationLoadViewIn setDuration:1.5];
    [applicationLoadViewIn setType:kCATransitionPush];
    [applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    //[applicationLoadViewIn setRepeatCount:114];

    productVC=[[PoductViewController alloc]initWithNibName:@"PoductViewController" bundle:[NSBundle mainBundle]];

    [[productVC.view layer] addAnimation:applicationLoadViewIn forKey:kCATransitionPush];
    [self.view addSubview:productVC.view];

,但我建议你使用导航控制器代替

[self.view addSubview:productVC.view];

仅仅使用

[self.navigationController pushViewController:productVC animated:YES];

如果你使用这个,将没有背景视图,你将看到当前视图的白屏。但是如果你添加了子视图,那么你需要手动删除子视图。

You can use this way as well

CATransition *applicationLoadViewIn = [CATransition animation];
    [applicationLoadViewIn setDuration:1.5];
    [applicationLoadViewIn setType:kCATransitionPush];
    [applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    //[applicationLoadViewIn setRepeatCount:114];

    productVC=[[PoductViewController alloc]initWithNibName:@"PoductViewController" bundle:[NSBundle mainBundle]];

    [[productVC.view layer] addAnimation:applicationLoadViewIn forKey:kCATransitionPush];
    [self.view addSubview:productVC.view];

But I suggest you to use Navigation Controller in place of

[self.view addSubview:productVC.view];

just use

[self.navigationController pushViewController:productVC animated:YES];

If you use this, there will be no background view, you will see white screen of the present view. But if you add subview, then you need to manually remove subview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文