2011-12-27 77 views
4

我有一个视图,它包含在我的应用程序的主视图中(屏幕中间的一个正方形)。我写了下面的代码,滑出屏幕的观点的权利,如果你把它滑动到最左边:如何动画UIView离开屏幕到右侧,然后再从左侧重新出现

- (IBAction)swipeLeft:(id)sender 
{ 
    CGRect initCardViewFrame = self.cardView.frame; 
    CGRect movedCardToRightViewFrame = CGRectMake(initCardViewFrame.origin.x + 1000, initCardViewFrame.origin.y, initCardViewFrame.size.width, initCardViewFrame.size.height); 

    [UIView animateWithDuration:0.7 
        animations:^{self.cardView.frame = movedCardToRightViewFrame;} 
        completion:nil]; 
} 

这个伟大的工程,但是我想延长代码,这样一旦平方从屏幕的右侧离开,它从右侧回到中间。我不确定如何在窗口左侧“重新绘制”它,然后将它滑回到中间。我试过重做帧,但动画只显示动画块中的最后一帧移动。我假设我需要将视图从屏幕上移开,然后将其重新绘制在屏幕左侧的外侧,然后将其滑入中间。除非有更直观的方式来做到这一点。

更新:

我下面回答了这个问题,但我不能不认为有更好的方法来做到这一点没有嵌套在彼此内的UIView动画。这是解决这个问题的唯一方法吗?

回答

4

我最终加入以下代码“完成”参数中:

completion:^(BOOL finished) 
{ 
    self.cardView.frame = moveCardToLeftViewFrame; 

    [UIView animateWithDuration:0.4 
        animations:^{self.cardView.frame = initCardViewFrame;} 
        completion:nil]; 
}]; 

注* moveCardToLeftViewFrame是一个的CGRect是放置视图到可视窗口的左侧。所以在它向右滑动之后,它被放置在屏幕的左边,然后滑回到中间。

+0

这是为了得到你想要的东西的最佳方式。嵌套动画确实变得丑陋(缩进地狱!),但这是最简单的方法来做你想做的事情。 – jrturton 2011-12-27 18:00:07

0

可以的情况下,使用此代码:

  1. 视图将走出屏幕外左,获取从右。
  2. 视图将从屏幕熄灭到右侧并从左侧进入。

代码狙击:

- (void)animateViewWithTransformation:(MyEnumAnimationTransformation)animationTransformation withDuration:(CGFloat)duration { 
    CGFloat goOutOffScreenToX, cameInToScreenFromX; 
    switch (animationTransformation) { 
     case goOutToLeftGetInFromRight: 
      goOutOffScreenToX = -1 * [UIScreen mainScreen].applicationFrame.size.width; 
      cameInToScreenFromX = [UIScreen mainScreen].applicationFrame.size.width; 
      break; 
     case goOutToRightGetInFromLeft: 
      goOutOffScreenToX = [UIScreen mainScreen].applicationFrame.size.width; 
      cameInToScreenFromX = -1 * [UIScreen mainScreen].applicationFrame.size.width; 
      break; 
     default: 
      break; 
    } 

    CGRect originViewFrame = self.frame; 
    [UIView animateWithDuration:duration 
        animations:^{[self setX:goOutOffScreenToX];} 
        completion:^(BOOL finished) 
           { 
            [self setX:cameInToScreenFromX]; 
            [UIView animateWithDuration:duration 
                animations:^{[self setX:originViewFrame.origin.x];} 
                completion:nil]; 
           }]; 
} 
相关问题