【问题标题】:How to animate the view Left to Right in iPhone?如何在 iPhone 中为视图从左到右设置动画?
【发布时间】:2011-05-22 05:15:00
【问题描述】:

如何使视图从左到右转换动画(类似推送视图)。当我单击按钮时,视图应该从左到右转换。所以请指导我并提供一些示例链接。

谢谢。

【问题讨论】:

    标签: iphone animation uiview


    【解决方案1】:

    假设你想从右边推送 view2 来替换 view1。

    // Set up view2
    view2.frame = view1.frame;
    view2.center = CGPointMake(view1.center.x + CGRectGetWidth(view1.frame), view1.center.y);
    [view1.superview addSubview: view2];
    // Animate the push
    [UIView beginAnimations: nil context: NULL];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDidStopSelector: @selector(pushAnimationDidStop:finished:context:)];
    view2.center = view1.center;
    view1.center = CGPointMake(view1.center.x - CGRectGetWidth(view1.frame), view1.center.y);
    [UIView commitAnimations];
    

    然后(可选)实现此方法以从视图层次结构中删除 view1:

    - (void) pushAnimationDidStop: (NSString *) animationID finished: (NSNumber *) finished context: (void *) context {
        [view1 removeFromSuperview];
    }
    

    在该动画委托方法中,您可能还想释放 view1 并将其引用设置为 nil,具体取决于您是否需要在转换后保留它。

    【讨论】:

    • 我知道这是旧的,但...我试图遵循逻辑。我对这是在做什么感到有点困惑。有人可以解释一下吗?
    • @marciokoko 此代码将 view2 放置在 view1 的右侧,将视图向左滑动动画(以便 view2 替换 view1)并在动画完成后删除 view1。他们称之为“推动过渡”。
    【解决方案2】:

    要从左到右制作动画,您可以使用下面的代码块

        CATransition *transition = [CATransition animation];
        transition.duration = 0.4;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type = kCATransitionPush;
        transition.subtype = kCATransitionFromLeft;
        [self.view.window.layer addAnimation:transition forKey:nil];
        [self presentViewController:YOUR_VIEWCONTROLLER animated:YES completion:nil];
    

    【讨论】:

      【解决方案3】:

      为了简单起见,另一种选择是使用块来制作动画,代码行更少:

      这里是示例

      CGRect viewLeftRect;//final frames for left view
      CGRect viewRightRect;//final frames for right view
      
      [UIView animateWithDuration:0.3f animations:^{
          [viewLeft setFrame:viewLeftRect];
          [viewRight setFrame:viewRightRect];
      } completion:^(BOOL finished) {
          //do what ever after completing animation
      }];
      

      【讨论】:

        【解决方案4】:

        你也可以像这样从右到左添加动画:

            scrAnimation.frame=CGRectMake(248, 175, 500, 414);  //your view start position
        
            [UIView animateWithDuration:0.5f
                                  delay:0.0f
                                options:UIViewAnimationOptionBeginFromCurrentState
                             animations:^{
                                 [scrAnimation setFrame:CGRectMake(0, 175, 500, 414)];   // last position
                             }
                             completion:nil];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多