为了在执行动画之前重新绘制视图,需要通过一个图形循环。这段代码是“画这个,当下一个事件循环出现时,做另一件事”的例子。在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];
}