代码之家  ›  专栏  ›  技术社区  ›  Rob

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

  •  5
  • Rob  · 技术社区  · 15 年前

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

    我遇到的问题是动画没有显示image1。。。因此,仅显示翻转过渡的最后一半。

    但是,如果我先通过触摸重置来重置动画,那么一切都会完美工作。换句话说,如果我一次又一次地按“翻转”,我只能得到一半的过渡。。。但如果我先按“重置”,那么一次翻转一切都会正常工作。

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

    alt text

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

    谢谢

    1 回复  |  直到 5 年前
        1
  •  12
  •   Rob Napier    15 年前

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