在几行代码之后更改animationDuration

时间:2013-03-04 22:25:04

标签: c4

我想做

  1. 将animationDuration更改为0
  2. 做点什么
  3. 将animationDuration更改为更长的时间(例如1.0f)
  4. 做其他事
  5. ...所有在touchesBegan方法中都没有任何“暂停”。但它似乎不会让我这样做。

    像这样:

    s1.animationDuration = 0.0f;
    s1.center = touchedPoint;
    s1.alpha = 1.0f;
    s1.animationDuration = 1.0f;
    s1.alpha = 0.0f;
    

    此处的完整示例:https://gist.github.com/gregtemp/5086240

    我知道我可以将它移动到touchesEnded方法,但我想避免这样做。

1 个答案:

答案 0 :(得分:1)

在你的问题中,你问的是:

  1. 更新对象的属性
  2. 移动它
  3. 更新同一对象的属性
  4. 淡出它
  5. ...当您触摸屏幕时,它可以重新出现在另一个地方。

    此外,您希望在单个方法中执行此操作...

    我建议采取不同的方法来解决这个问题。

    首先,尝试将形状视为持久性的对象,直到删除或处理它们为止。基本上,您可以将对象视为可以传递给各种方法的东西。

    当您开始这样思考时,您可以使用以下技术来制作您正在寻找的效果:

    #import "C4WorkSpace.h"
    
    @implementation C4WorkSpace 
    
    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        for (UITouch *t in touches) {
            CGPoint touchPoint = [t locationInView:self.canvas];
            [self createObjectAtPoint:touchPoint];
        }
    }
    
    -(void)createObjectAtPoint:(CGPoint)newPoint {
        C4Shape *s = [C4Shape ellipse:CGRectMake(newPoint.x-25,newPoint.y-25,50,50)];
        s.userInteractionEnabled = NO;
        [self.canvas addShape:s];
        [self runMethod:@"fadeAndRemoveShape:" withObject:s afterDelay:0.0f];
    }
    
    -(void)fadeAndRemoveShape:(C4Shape *)shape {
        shape.animationDuration = 1.0f;
        shape.alpha = 0.0f;
        [shape runMethod:@"removeFromSuperview" afterDelay:shape.animationDuration];
    }
    
    @end
    

    这是做什么的:

    1. 获得接触点
    2. 将触摸点传递给创建形状的方法
    3. 将创建的形状传递给淡出的方法
    4. 在画布消失后从画布中移除
    5. 从屏幕中删除后,内容会自动从内存中删除