简体   繁体   English

同一UIViewController中的UIView之间的过渡

[英]Transitions between UIView in the same UIViewController

I want to transit from UIView to another in the UIViewController when I press a button 我想在按下按钮时从UIView过渡到UIViewController中的另一个对象

I write this code: 我写这段代码:

- (IBAction)changeView:(id)sender {

    CATransition* transition = [CATransition animation];
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    transition.duration = 1.0f;
    transition.type =  @"cube";
    transition.subtype = @"fromTop";
    [self.view1.layer removeAllAnimations];
    [self.view1.layer addAnimation:transition forKey:kCATransition];


}

this code will transit the view1(UIview) to itself. 这段代码会将view1(UIview)传递给它自己。 how can I make it to transit to another UIView (eg view2). 如何使它过渡到另一个UIView(例如view2)。 also where can I put the another UIview (view2) in the storyboard 还可以在哪里将另一个UIview(view2)放在情节提要中

Have you try like this? 你有这样尝试吗?

  self.view1.alpha = 0.0;

 [UIView beginAnimations:@"flip" context:nil];

 [UIView setAnimationDelegate:self];

 [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

 [UIView setAnimationDuration:1.0f];

  self.view2.alpha = 1.0;   

  [UIView commitAnimations];

Have a look at this code snippet it's much more elegant. 看看这个代码片段,它更加优雅。 Also checkout Apple's documentation for more fancy UIViewAnimationOptions 另请查阅Apple文档以获取更多精美的UIViewAnimationOptions

- (IBAction)changeView:(id)sender
{
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        //Remove View-1 from the main view
        [view1 removeFromSuperView];
        //Add the new view to your current view
        [self.view addSubView:view2];
    } completion:^(BOOL finished) {
        //Once animation is complete
    }];
}

Ensure that the two views are a strong property of your view controller. 确保这两个视图是视图控制器的强大属性。

If you have two UIView for example take firstView and secondView and add it to UIViewController on same rect. 例如,如果您有两个UIView,请使用firstViewsecondView并将其添加到同一矩形上的UIViewController中。 and set hiddden property to secondView. 并将hiddenden属性设置为secondView。

-(IBAction)flipFirstViewToSecondView{
secondView.hidden= NO;
    [UIView transitionWithView:firstView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{

        [UIView transitionWithView:secondView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{

        } completion:^(BOOL finished) {
            secondView.hidden = NO;
            firstView.hidden = YES;
        }];

    } completion:^(BOOL finished) {

    }];
}

Thanks. 谢谢。 just let me know if you have any problem to integrate it. 如果您有任何集成问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM