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

ipad:用动画移动uiview,然后再移回去

  •  2
  • Joel  · 技术社区  · 14 年前

    我在uiview子类中有以下代码,可以通过动画将其移出屏幕:

    float currentX = xposition; //variables that store where the UIView is located
    float currentY = yposition;
    
    float targetX = -5.0f - self.width;
    float targetY = -5.0f - self.height;
    
    moveX = targetX - currentX; //stored as an instance variable so I can hang on to it and move it back
    moveY = targetY - currentY;
    
    [UIView beginAnimations:@"UIBase Hide" context:nil];
    [UIView setAnimationDuration:duration]; 
    self.transform = CGAffineTransformMakeTranslation(moveX,moveY);
    [UIView commitAnimations];
    

    它工作得很好。我把targetx/y改为0,看看它是否真的移动到了指定的点,它确实移动了。

    问题是当我尝试将视图移回原来的位置时:

    moveX = -moveX;
    moveY = -moveY;
    
    [UIView beginAnimations:@"UIBase Show" context:nil];
    [UIView setAnimationDuration:duration];
    self.transform = CGAffineTransformMakeTranslation(moveX,moveY);
    [UIView commitAnimations];
    

    这使得uiview尽可能地增加了3倍。所以视图通常会从屏幕的右侧移开(这取决于它在哪里)。

    CGaffinetransformaketransflation函数有什么我应该知道的吗?我读了文档,从我所知,它应该做我期望它做的事情。

    有人建议如何解决这个问题吗?谢谢。

    2 回复  |  直到 14 年前
        1
  •  2
  •   kennytm    14 年前

    分配给 transform 会使旧的消失。要返回到原始位置,请将标识转换指定给它。

    self.transform = CGAffineTransformIdentity;
    

    (或者更好,只需更改 .frame 而不是改变 .transform )

        2
  •  2
  •   Paul Lynch    14 年前

    uiview转换不是加法;我建议您改为移回它:

    self.transform = CGAffineTransformIdentity;
    

    文档说(关于更新框架):

    警告:如果此属性不是标识转换,则框架属性的值未定义,因此应忽略。