【问题标题】:Moving an object without animating it in an animation block移动对象而不在动画块中对其进行动画处理
【发布时间】:2010-11-16 08:24:26
【问题描述】:

我正在为一堆按钮设置动画,因此它们会移动到屏幕的左侧,然后它们应该会神奇地移动到屏幕的右侧。我通过调用来做到这一点:

[NSTimer scheduledTimerWithTimeInterval:0.20 target:self selector:@selector(moveTheButtons) userInfo:nil repeats:YES];

在 viewDidLoad 中。

他们很高兴地向左移动,但随后又向右侧移动。我只是希望它们消失并重新出现在屏幕外。因为我是新来的,所以我认为因为在 commitAnimations 调用之后它不会动画。我认为问题在于 moveTheButtons 函数返回后动画实际上是“提交”,但我想不出一种优雅(或标准)的方式来解决这个问题。

如何在不设置动画的情况下将 UIButton 移出屏幕,最好还是在 moveTheButtons 函数中?

正如您可能推断的那样,我是 iPhone 上的动画新手,所以如果您发现任何其他错误,请随时给我一些指示。

-(void)moveTheButtons{
NSLog(@"moveTheButtons");

[UIView beginAnimations:@"mov-ey" context:cloudA];
[UIView setAnimationDuration: .20];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
cloudA.center = CGPointMake(cloudA.center.x-pos.x,cloudA.center.y);
[UIView commitAnimations];


if(cloudA.center.x < -100){
    //I don't want to animate this bit. 
    cloudA.center = CGPointMake(550,cloudA.center.y);
}

//NSLog(@"cloudA.center.x %f", cloudA.center.x);
}

【问题讨论】:

    标签: iphone cocoa-touch uiview core-animation


    【解决方案1】:

    您可以使用+[UIView setAnimationsEnabled:] 方法暂时关闭动画块中属性的隐式动画。这在许多用例中都非常有用。

    做这样的事情:

    -(void)moveTheButtons {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration: .20];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        CGPoint center = CGPointMake(cloudA.center.x - pos.x, cloudA.center.y);
        if (center.x < -100) {
            [UIView setAnimationsEnabled:NO];
            cloudA.center = CGPointMake(550, center.y);
            [UIView setAnimationsEnabled:YES];
        } else {
            cloudA.center = center;
        }
        [UIView commitAnimations];
    }
    

    作为旁注;除非您实际使用响应委托方法的委托,否则无需为动画指定名称或上下文。只需传递nilNULL,就像我在此示例中所做的那样。

    【讨论】:

    • 感谢有关上下文和名称的提示!沃伦首先回答,所以他得到了正确的勾号,但我非常感谢您的回答和建议。
    【解决方案2】:

    在动画块之前预先计算点,反转顺序并返回。

    在所有情况下,您都会无条件地向引擎提交动画。

    -(void)moveTheButtons{
    NSLog(@"moveTheButtons");
    
    CGPoint mypoint = CGPointMake(cloudA.center.x-pos.x,cloudA.center.y);
    
    if(cloudA.center.x < -100){
        //I don't want to animate this bit. 
        cloudA.center = CGPointMake(550,cloudA.center.y);
        return;
    }
    
    
    [UIView beginAnimations:@"mov-ey" context:cloudA];
    [UIView setAnimationDuration: .20];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    cloudA.center = mypoint;
    [UIView commitAnimations];
    
    }
    

    使用 return 而不是 if/else 有一个风格要点,但您可以形成自己的意见。

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多