OGeek|极客世界-中国程序员成长平台

标题: iPhone - 同时动画 2 个 subview [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:17
标题: iPhone - 同时动画 2 个 subview

我有 2 个占据整个屏幕的 subview (状态栏除外)。我们称这个尺寸为“屏幕尺寸”。

我想同时动画:

第二个 View 在开始时可见并在屏幕上。

这是我写的:

- (void) switchViews
{
    if (self.view2Controller == nil) {
        self.view2Controller = [[View2Controller alloc] initWithNibName"View2XIB" bundle:nil];
        self.view2Controller.view.hidden = YES;
        [self.view addSubview:self.view2Controller.view];
    }

    CGRect bigFrame = CGRectInset(self.view.frame, -50, -50);
    CGRect normalFrame = self.view.frame;
    CGRect smallFrame = CGRectInset(self.view.frame, 50, 50);

    self.view2Controller.view.frame = bigFrame;
    self.view2Controller.view.alpha = 0.0;

    [UIView beginAnimations"Anim1" context:nil];
    [UIView setAnimationDuration:5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelectorselector(animationDidStop:finished:context];

    self.view2Controller.view.hidden = NO;
    self.view2Controller.view.frame = normalFrame;
    self.view2Controller.view.alpha = 1.0;

    [UIView commitAnimations];

    // ------------------------------

    [UIView beginAnimations"Anim2" context:nil];
    [UIView setAnimationDuration:5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelectorselector(animationDidStop:finished:context];

    self.view1Controller.view.frame = smallFrame;
    self.view1Controller.view.alpha = 0.0;

    [UIView commitAnimations];
}

当然,我首先尝试将两种动画都放入一个独特的动画中。这不会改变任何事情,这就是我试图将它们分开的原因。

当启动时,view1 立即变为黑色,然后 view2 开始按预期设置动画。但我无法同时运行两个动画。

我该怎么做?



Best Answer-推荐答案


尝试查看基于 block 的动画。这是 iOS 4.0+ 推荐的方法。看看这里的答案:What are block-based animation methods in iPhone OS 4.0?

编辑 试试这样的

//You can do the same thing with a frame
CGPoint newCenter = CGPointMake(100, 100);
[UIView animateWithDuration:2.0
                 animations:^{ 
                     firstView.center = newCenter;
                     secondView.center = newCenter;
                     firstView.alpha = 0.2;
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"All done animating");
                 }];

您放入动画中的任何内容:^{ } 将是您 View 的目标设置。上面我向您展示了如何更改位置以及 alpha。

关于iPhone - 同时动画 2 个 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7603948/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4