ios 在 addSubView 中添加从左到右的滑入动画(过渡)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22078862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:36:00  来源:igfitidea点击:

Add Slide In from Left to Right animation(transition) in addSubView

iosiphoneobjective-cuiviewanimationtransition

提问by Maheswaran Ravisankar

I Try to imitate the NavigationViewController, very similar to default MailAppin iPhone

我尝试模仿NavigationViewController,非常类似于 default MailAppiniPhone

When clicking on a Mail summary, it should slide-inthe mail details, and when BACKbutton is clicked, Mail Summary view is to be slided-inback again.

当邮件总结一下,应该滑入邮件的详细信息,并在返回按钮被点击,邮件摘要视图被滑动式回来。

This is what I have to animate the transition from Summary To Detail (removeFromSuperView)

这是我必须为从摘要到详细信息的过渡设置动画 ( removeFromSuperView)

CGRect temp = self.view.frame;
temp.origin.x = 300;
[UIView animateWithDuration:0.5
                      delay:0.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     self.view.frame = temp;
                 }completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

This is what I have to animate the transition from Detail To Summary (addSubview)

这就是我必须为从详细信息到摘要 ( addSubview)的过渡设置动画

    CATransition *transition = [CATransition animation];
    transition.duration = 0.5;
    transition.type = kCATransitionFromRight;
    transition.subtype = kCATransitionFade;
    [parentView.layer addAnimation:transition forKey:nil];
    [parentView addSubview:myVC.view];

Now, that My First part of code is working well! Where-as the second part, I am just able to achieve the Fade-in animation. How can I bring in the slide transition?

现在,我的第一部分代码运行良好!其中-作为第二部分,我只能实现淡入动画。如何引入幻灯片过渡?

回答by Maheswaran Ravisankar

I just need to choose kCATransitionPushtype for my CATransition

我只需要kCATransitionPush为我选择类型CATransition

    CATransition *transition = [CATransition animation];
    transition.duration = 0.5;
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [parentView.layer addAnimation:transition forKey:nil];

    [parentView addSubview:myVC.view];

Update for Swift:

Swift 更新:

    let transition = CATransition()
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromLeft
    parentView.layer.add(transition, forKey: nil)
    parentView.addSubview(myVC.view)

回答by Andy

Or you could check out : HorizontalSlide Segue (just google it I cant get the link, on mobile) its a great sliding transition that is simply a segue that you add from view A to B....

或者你可以看看:Horizo​​ntalSlide Segue(只是谷歌它我无法获得链接,在移动设备上)它是一个很棒的滑动过渡,它只是你从视图A添加到B的一个segue......

On a side note, if this is in a nav controller, a push or a pop should have a slide animation by default...

附带说明一下,如果这是在导航控制器中,则默认情况下推送或弹出应具有幻灯片动画...