【问题标题】:How to animate a view on button click iOS?如何在按钮单击iOS上为视图设置动画?
【发布时间】:2015-01-30 08:05:23
【问题描述】:

通过以下代码,我将在按钮单击时显示一个带有动画的视图。

UIViewController *modalView = self.pageViewController;

[self.view addSubview:modalView.view];

[UIView animateWithDuration:1 animations:^{
    CGRect currentRect = modalView.view.frame;
    currentRect.origin.y = 650.0f;
    currentRect.size.height = 295.0f;
    [modalView.view setFrame:currentRect];
    [modalView.view removeFromSuperview];

}];

[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];

这适用于第一次单击按钮。当我一次又一次地按下按钮时,这不再是动画了。它只是第一次制作动画。有什么办法吗?

提前致谢!

【问题讨论】:

  • 这真的不是办法。您不应该将另一个 viewcontrollers 视图作为子视图添加到另一个 viewcontroller,它会破坏 mvc 模式。转而深入研究 UIViewControllerTransisitions,这是一个很好的教程:teehanlax.com/blog/custom-uiviewcontroller-transitions

标签: ios objective-c animation uiview


【解决方案1】:

原因是modalView.view的框架最初与

不同
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;

所以当改变它的帧动画时就会发生。 但是下次当您执行按钮的操作时,modalView.view 的当前帧和您设置的那个帧是相同的。所以根本没有任何动画。 此外,您应该将 [modalView.view removeFromSuperview]; 从动画块移动到完成块

[UIView animateWithDuration:1 animations:^{
    CGRect currentRect = modalView.view.frame;
    currentRect.origin.y = 650.0f;
    currentRect.size.height = 295.0f;
    [modalView.view setFrame:currentRect];} 
completion:^(BOOL finished) {
    [modalView.view removeFromSuperview];
}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多