我有一个UIVIew
添加为视图控制器中的子视图,我试图翻转并将其替换为其他视图。
[UIView transitionFromView:self.testBlueTicket toView:self.testOrangeTicket duration:2 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
}];
问题是整个视图控制器都在翻转,而不仅仅是我想要翻转和替换的特定视图。
我已经搜索了文档,没有能够找到任何特定的解决方案。如有任何建议,不胜感激。
使用[UIView transitionFromView...
使父视图动画化。
因此,在容器视图中嵌入testBlueTicket
和testOrangeTicket
视图。
下面是一个简单的例子:
@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:
对宽度和/或高度的约束)…然后,您将看到.systemYellow
容器视图动画,并嵌入票证视图。