在开始动画之前修改iPhone动画容器视图

Rob*_*Rob 5 iphone animation cocoa-touch objective-c uikit

我正在为我正在制作的纸牌游戏添加一些基本动画.(我的第一个iPhone应用程序.)

我正在创建一个自定义的UIView类"AnimationContainer",它从image1翻转到image2,同时从rect1移动到rect2.我的最终目的是让这些容器中最多有四个同时进行转换.

我遇到的问题是动画没有显示image1 ...因此只显示翻转过渡的后半部分.

但是,如果我首先通过触摸重置重置动画,那么一切都很完美.换句话说,如果我反复按Flip,我只能获得一半的转换......但是如果我先按下Reset,那么一切都可以完美地完成一次翻转.

那么,我怎样才能使动画正确重置?

下面是代码,截图,这里是完整的链接:Project Zip File 700k.

alt text http://www.robsteward.com/cardflip.jpg

- (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];
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Rob*_*ier 12

在执行动画之前,您需要绘制循环才能重绘视图.这段代码是"绘制它的一个例子,当下一个事件循环到来时,做另外的事情." 在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];
}
Run Code Online (Sandbox Code Playgroud)