在开始动画之前修改iPhone动画容器视图 [英] Modifying an iPhone animation container view before starting animation

查看:78
本文介绍了在开始动画之前修改iPhone动画容器视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am adding some basic animation to a card game I'm working on. (My first iPhone app.)

我创建一个自定义的UIView类AnimationContainer,从rect1翻转到image2,我的最终目的是让最多四个容器同时进行转换。

I am creating a custom UIView class "AnimationContainer", which flips from image1 to image2, while moving from rect1 to rect2. My ultimate intention is to have up to four of these containers doing their transitions simultaneously.

我遇到的问题是动画没有显示image1 ...所以只有翻转过渡的最后一半出现。

The problem I'm having is that the animation isn't showing image1... so only the last half of the flip transition appears.

但是,如果我重置动画首先通过触摸重置,那么一切工作完美。换句话说,如果我一次又一次地按下Flip,我只得到一半的过渡...但如果我先按重置,那么一切都完美地一次翻转。

However, if I reset the animation first by touching Reset, then everything works perfectly. In other words, if I press Flip again and again, I only get half the transition... but if I press Reset first, then everything works perfectly for one flip.

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

Below is the code, a screenshot, and here's a link to the complete: Project Zip File 700k.

- (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];
}

谢谢!

推荐答案

您需要一个绘图循环才能在执行动画之前重绘视图。这个代码是一个画这个,当下一个事件循环来了,做这个其他事情的例子。在UI代码中这样做并不少见。

You need a drawing loop to pass in order to redraw the view before performing the animation. This code is an example of "draw this, and when the next event loop comes around, do this other thing." It's not uncommon to do this in UI code. Your first work-around is attempting the same thing, but in a much more complicated way.

- (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];
}

这篇关于在开始动画之前修改iPhone动画容器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆