翻转和替换 UIView

Flipping and replacing UIView

我在视图控制器中添加了一个 UIVIew 作为子视图,我正在尝试将其翻转并替换为其他视图。

[UIView transitionFromView:self.testBlueTicket toView:self.testOrangeTicket duration:2 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
        
}];

问题是整个 viewcontroller 都在翻转,而不仅仅是我想要翻转和替换的特定视图。

我已经搜索了文档,但未能找到任何特定的解决方案。任何建议将不胜感激。

使用 [UIView transitionFromView... 动画父视图。

因此,将您的 testBlueTickettestOrangeTicket 视图嵌入到 容器 视图中。

这是一个简单的例子:

@interface FlipViewController ()

@property (strong, nonatomic) UIView *testBlueTicket;
@property (strong, nonatomic) UIView *testOrangeTicket;
@property (strong, nonatomic) UIView *containerView;

@end

@implementation FlipViewController

- (void)viewDidLoad {
    
    _containerView = [UIView new];
    _testBlueTicket = [UIView new];
    _testOrangeTicket = [UIView new];
    
    _containerView.translatesAutoresizingMaskIntoConstraints = NO;
    _testBlueTicket.translatesAutoresizingMaskIntoConstraints = NO;
    _testOrangeTicket.translatesAutoresizingMaskIntoConstraints = NO;
    
    _containerView.backgroundColor = [UIColor systemYellowColor];
    _testBlueTicket.backgroundColor = [UIColor systemBlueColor];
    _testOrangeTicket.backgroundColor = [UIColor systemOrangeColor];
    
    // respect safe area
    UILayoutGuide *g = [self.view safeAreaLayoutGuide];
    
    [_containerView addSubview:_testOrangeTicket];
    [_containerView addSubview:_testBlueTicket];
    
    [self.view addSubview:_containerView];
    
    [NSLayoutConstraint activateConstraints:@[
        
        [_containerView.widthAnchor constraintEqualToConstant:200.0],
        [_containerView.heightAnchor constraintEqualToConstant:260.0],
        [_containerView.centerXAnchor constraintEqualToAnchor:g.centerXAnchor],
        [_containerView.centerYAnchor constraintEqualToAnchor:g.centerYAnchor],
        
        [_testBlueTicket.widthAnchor constraintEqualToAnchor:_containerView.widthAnchor multiplier:1.0],
        [_testBlueTicket.heightAnchor constraintEqualToAnchor:_containerView.heightAnchor multiplier:1.0],
        [_testBlueTicket.centerXAnchor constraintEqualToAnchor:_containerView.centerXAnchor],
        [_testBlueTicket.centerYAnchor constraintEqualToAnchor:_containerView.centerYAnchor],
        
        [_testOrangeTicket.widthAnchor constraintEqualToAnchor:_containerView.widthAnchor multiplier:1.0],
        [_testOrangeTicket.heightAnchor constraintEqualToAnchor:_containerView.heightAnchor multiplier:1.0],
        [_testOrangeTicket.centerXAnchor constraintEqualToAnchor:_containerView.centerXAnchor],
        [_testOrangeTicket.centerYAnchor constraintEqualToAnchor:_containerView.centerYAnchor],
        
    ]];
    
    _testOrangeTicket.hidden = YES;
    
    UITapGestureRecognizer *t = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap)];
    [self.view addGestureRecognizer:t];
}

- (void)didTap {
    
    UIView *fromView = self.testBlueTicket.isHidden ? self.testOrangeTicket : self.testBlueTicket;
    UIView *toView = self.testBlueTicket.isHidden ? self.testBlueTicket : self.testOrangeTicket;

    [UIView transitionFromView:fromView
                        toView:toView
                      duration:2
                       options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews
                    completion:^(BOOL finished) {
        
    }];
    
}

@end

请注意,它正在使用 UIViewAnimationOptionShowHideTransitionViews,因此“票证”视图保留在视图层次结构中。

为了看得更清楚,更改票证视图的一些限制(例如 multiplier: 宽度 and/or 高度限制)......然后你会看到 .systemYellow 容器视图动画,嵌入了工单视图。