Transition behavior using transitionFromView and transitionWithView

后端 未结 7 563
既然无缘
既然无缘 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条回答
  • 2020-12-05 08:15

    I had the same requirement, and saw many approaches -- from using layers (which ends up very complicated if you just want to deal with a UIView) to suggestions that i needed a "container" to then transition between to sub-views. But if you just want to swithc something front to back, like flipping over a playing card or a game tile, i made a one line change:

    (note: i have an array of UIViews (tiles[x][y]) in a grid (for a game). i have all my imagefilenames in database tables / arrays, which dynamically are loaded into the UIImages of the UIImageViews. The image for the "back" of a card or tile in a UIImage named "tileBackPicImageNM".

    so my code that simply swapped the front image out for an image of the back was:

    [tiles[indexX][indexY] setImage:[UIImage imageNamed:tileBackPicImageNM]];
    

    which works fine.

    but and now, to show a flipping action, it is:

    [UIView transitionWithView:tiles[indexX][indexY] duration:0.3 options:UIViewAnimationOptionTransitionFlipFromLeft 
                        animations:^{  
                            [[indexX][indexY] setImage:[UIImage imageNamed:tileBackPicImageNM]];
                        }
                        completion:NULL];    
    

    I experimented with different "duration" parameters -- subsequently made it a float that I set in the viewDidLoad routine. Faster than 0.3 is almost not visible, and a value of 1 or 2 is very slow...

    This does not involve two sub-views, which is my case is a lot more useful since i didn't want to have an array of containers containing a hierarchy of views, etc... and on new levels etc i always reload the arrays anyway...

    0 讨论(0)
提交回复
热议问题