[英]How to animate transition between two views without using two view controllers?
I am trying to simulate a deck of cards, where a swipe over the screen makes the next card enter the display from the right, covering the card that is currently visible. 我正在尝试模拟一副纸牌,其中在屏幕上滑动可使下一张纸牌从右侧进入显示,覆盖当前可见的纸牌。 I know how to program this using two view controllers and custom segues, but I want to keep it simple.
我知道如何使用两个视图控制器和自定义序列进行编程,但是我想保持简单。 I therefore want to stay within one and the same view controller, copy the current view into a temporary view, draw the new view, and then animate the new view to drift in from the right and cover the old one.
因此,我想留在一个相同的视图控制器中,将当前视图复制到一个临时视图中,绘制新视图,然后为新视图设置动画,以从右侧向后漂移并覆盖旧视图。 Here's my attempt:
这是我的尝试:
- (IBAction)SwipeLeft:(id)sender
{ UIView *sv = self.view;
[self Layout]; // this is where the next card is drawn
UIView *dv = self.view;
dv.center = CGPointMake(sv.center.x + sv.frame.size.width, sv.center.y);
sv.center = CGPointMake(sv.center.x, sv.center.y);
[UIView animateWithDuration:0.3
animations:^
{ dv.center = CGPointMake(sv.center.x, sv.center.y);
sv.center = CGPointMake(sv.center.x - sv.frame.size.width, sv.center.y);
}];
NSLog(@"Swipe left");
}
When I run this code, I do see the new card enter the screen appropriately, however, the 'old' view dissappears immediately and turns into a black screen before the animation starts. 当我运行此代码时,我确实看到新卡已正确进入屏幕,但是,“旧”视图立即消失,并在动画开始之前变成黑屏。
How can I correct this? 我该如何纠正?
Clearly this code does not work, as both pointers point at the same memory location. 显然,此代码不起作用,因为两个指针都指向相同的存储位置。 I decided to solve my problem in a different way.
我决定以其他方式解决我的问题。 Rather than trying to copy a View object, I create a screen shot, copy the UIImage into a View object, cover the screen under the copy of the old screen when I draw the new screen and than animate that View object.
我创建屏幕快照,而不是尝试复制View对象,而是将UIImage复制到View对象中,在绘制新屏幕时覆盖旧屏幕副本下的屏幕,而不是对该View对象进行动画处理。 Probably a smarter solution.
可能是更智能的解决方案。 It works great.
效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.