Transition behavior using transitionFromView and transitionWithView

后端 未结 7 578
既然无缘
既然无缘 2020-12-05 07:27

I am attempting to create a transition between two subviews (view1 and view2). When a button is pressed I want view1 (front) to flip and show view2 (back). I have tried both

7条回答
  •  -上瘾入骨i
    2020-12-05 08:07

    Here's my working solution stub, a simple thing that took 2 hours, damn: we got 1 button to flip things, a UIView called panel that holds the 2 views I want to swap with a flip animation.

    -(void)viewDidLoad{
        [super viewDidLoad];
        UIButton *btnFlip = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnFlip.frame = CGRectMake(10, 10, 50, 30);
        [btnFlip setTitle:@"flip" forState:UIControlStateNormal];
        [btnFlip addTarget:self action:@selector(flip) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btnFlip];
    
        panel = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 300,300)];
        panel.backgroundColor = [UIColor darkGrayColor];
        [self.view addSubview:panel];
    
        panelBack = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 280, 200)];
        panelBack.tag = 1;
        panelBack.backgroundColor = [UIColor brownColor];
        [panel addSubview:panelBack];
    
        panelFront = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 280, 200)];
        panelFront.tag = 2;
        panelFront.backgroundColor = [UIColor whiteColor];
        [panel addSubview:panelFront];
    
        displayingFront = TRUE;
    }
    
    -(void)flip{
    
        [UIView transitionWithView:panel 
        duration:0.5
        options:(displayingFront ? UIViewAnimationOptionTransitionFlipFromRight : UIViewAnimationOptionTransitionFlipFromLeft)
        animations:^{ 
            if (displayingFront) {
                //panelFront.hidden=TRUE;
                //panelBack.hidden = FALSE;
                [panelFront removeFromSuperview];
                [panel addSubview:panelBack];
            }else{
                //panelFront.hidden=FALSE;
                //panelBack.hidden = TRUE;
                [panelBack removeFromSuperview];
                [panel addSubview:panelFront];
            }
         }
         completion:^(BOOL finished){
             displayingFront = !displayingFront;
         }];
    }
    

提交回复
热议问题