【问题标题】:UIView block animation move view left to right and back to leftUIView 块动画从左到右和从左到左移动视图
【发布时间】:2012-01-30 08:14:49
【问题描述】:

我不想为形状为箭头的视图设置动画,我希望它从左到右、从左到右等等…

下面的这段代码不起作用,我猜我需要一个路径,但不知道如何在 UIView 块动画中做到这一点。

   [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionRepeat animations:^{
    controller.view.frame = frame1;
    controller.view.frame = frame2;

} completion:^(BOOL finished) {

}];

【问题讨论】:

    标签: ios cocoa-touch uiview uiviewanimation


    【解决方案1】:

    你可以使用UIViewAnimationOptionAutoreverse选项,试试下面的代码:

    UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(20.0f, 100.0f, 300.0f, 200.0f)];
    [testView setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:testView];
    
    [UIView animateWithDuration:0.3f
                          delay:0.0f
                        options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                     animations:^{
                         [testView setFrame:CGRectMake(0.0f, 100.0f, 300.0f, 200.0f)];
                     }
                     completion:nil];
    
    [testView release];
    

    它会从右到左,从左到右,……平滑地重复移动。

    【讨论】:

    • TestView 被添加到ViewController 时,你的动画效果很好...但当它被添加到另一个View..如果可能的话请帮助...stackoverflow.com/questions/16148239/…
    • 嗨@CodeJack,它甚至可以在另一个视图上工作,你需要修改的只是将[self.view addSubview:testView];替换为[self addSubview:testView];。就这样。我已经对其进行了测试,并且效果很好。 ;)
    • @Kjuly 我的 UIView 有三个 UIButton。动画视图时不会删除此按钮。它还在那里。 UIView动画时如何移动所有子视图
    • @Thangavel 只需将视图上的按钮添加为子视图。 :) 对于答案,例如,在testView 上添加这些按钮。
    • @Kjuly 我添加按钮是我视图的子视图。我的动画视图是自上而下的。我将框架设置为 UIView 是 [_dropLocationView setFrame:CGRectMake(0.0f, 140.0f, 320.0f, 0.0f)];并将框架设置为 [_dropLocationView setFrame:CGRectMake(0.0f, 140.0f, 320, 120.0f)];动画时。动画时按钮不会随视图一起移动。
    【解决方案2】:

    假设frame1是开始位置,frame2是重复前的结束位置。

    问题在于动画是在运行循环结束时计算的,因此frame1 被忽略,视图被简单地移动到frame2。如果您将frame1 的设置移到块之外,那么它应该可以正常工作。如果您希望动画回放和转发,您需要将其设为 autoreverseUIViewAnimationOptionAutoreverse

    【讨论】:

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