我正在向我正在开发的纸牌游戏添加一些基本动画。 (我的第一个 iPhone 应用程序。)
我正在创建一个自定义 UIView 类“AnimationContainer”,它从 image1 翻转到 image2,同时从 rect1 移动到 rect2。我的最终目的是让最多四个容器同时进行转换。
我遇到的问题是动画没有显示 image1...所以只出现翻转过渡的后半部分。
但是,如果我首先通过触摸“重置”来重置动画,那么一切都会正常工作。换句话说,如果我一次又一次地按“翻转”,我只能得到一半的过渡......但如果我先按“重置”,那么一次翻转一切都会完美运行。
那么,如何让动画正确重置呢?
下面是代码、屏幕截图,还有完整的链接:项目 Zip 文件 700k http://www.robsteward.com/cardflip.zip.
- (void)displayWithImage1 { //RESET button calls this
self.frame = rect1;
[image2 removeFromSuperview];
[self addSubview:image1];
[self setNeedsDisplay]; //no help: doesn't force an update before animation
}
- (void)runTheAnimation { //FLIP button calls this
[self displayWithImage1]; //<---this is what the reset button calls
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:transition forView:self cache:NO];
self.frame = rect2;
[image1 removeFromSuperview];
[self addSubview:image2];
[UIView commitAnimations];
}
Thanks!
您需要传递一个绘图循环,以便在执行动画之前重绘视图。此代码是“绘制此内容,当下一个事件循环出现时,执行其他操作”的示例。在 UI 代码中执行此操作并不罕见。您的第一个解决方法是尝试同样的事情,但方式要复杂得多。
- (void)_runTheAnimation {
// Moved here from -runTheAnimation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:transition forView:self cache:NO];
self.frame = rect2;
[image1 removeFromSuperview];
[self addSubview:image2];
[UIView commitAnimations];
}
- (void)runTheAnimation { //FLIP button calls this
[self displayWithImage1];
[self performSelector:@selector(_runTheAnimation) withObject:nil afterDelay:0.0];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)