iOS:UIView 动画,没有动画发生
有一个令人费解的问题。我有一个通用应用程序,在 iPad 和 iPhone 版本之间有很多共享代码。 nib 中有不同的布局,但本质上是相同的视图和视图层次结构 - 一个 UIView 用作两个同级 UITextView 的容器。
带子项的 UIView mainView:
UITextView 段落TextView
UITextView NotesTextView
一个 UITextView 是隐藏的,另一个可见。
以下是我的代码。注释掉的部分是我最初的动画尝试。这在 iPad 上可以正常工作,但在 iPhone 上却不行。未注释的部分是 take 2,使用文档中推荐的方法。未注释的代码在 iPad 或 iPhone 上都不起作用 - 它隐藏/取消隐藏我的视图,但没有任何动画。如果我将代码添加到也会被执行的完成块中,那么它就会做一些事情,而不是动画。
/*
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:mainView cache:YES];
passageTextView.hidden = YES;
notesTextView.hidden = NO;
[UIView commitAnimations];
*/
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationTransitionFlipFromRight;
[UIView transitionWithView:mainView
duration:1.0
options:options
animations:^{ passageTextView.hidden = YES; notesTextView.hidden = NO; }
completion:NULL];
编辑:仍在解决该问题,希望有人提出建议。
其他更新
弄清楚为什么以下内容在 iPhone 中不起作用:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:mainView cache:YES];
passageTextView.hidden = YES;
notesTextView.hidden = NO;
[UIView commitAnimations];
我忽略了将视图连接到 Interface Builder 中的 mainView。调试了几个小时,我现在想检查一下。
但是,我仍然不知道为什么动画块不适用于 iPhone 或 iPad。我尝试了几种方法,但即使显示/隐藏正在工作,我也没有得到任何动画。
Having a puzzling problem. I have a universal app with a lot of shared code between the iPad and iPhone versions. There are different layouts in the nibs but essentially the same views and view hierarchy - one UIView used as a container for two sibling UITextViews.
UIView mainView with children:
UITextView passageTextView
UITextView notesTextView
One UITextView is hidden, the other visible.
The following is my code. The section commented out was my original animation attempt. This worked just as desired on the iPad, but not on the iPhone. The uncommented section is take 2, using the method recommended in the docs. The uncommented code does not work on either the iPad or iPhone - it hides/unhides my views but without any animation. If I add code to the completion block that also gets executed, so it's doing something, just not animating.
/*
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:mainView cache:YES];
passageTextView.hidden = YES;
notesTextView.hidden = NO;
[UIView commitAnimations];
*/
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationTransitionFlipFromRight;
[UIView transitionWithView:mainView
duration:1.0
options:options
animations:^{ passageTextView.hidden = YES; notesTextView.hidden = NO; }
completion:NULL];
Edit: Still working on the problem, hoping someone has a suggestion.
Additional update
Figured out why the following was not working in the iPhone:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:mainView cache:YES];
passageTextView.hidden = YES;
notesTextView.hidden = NO;
[UIView commitAnimations];
I had neglected to wire the view to mainView in Interface Builder. Hours debugging and I just now thought to check that.
But, I still do not know why animation blocks are not working for either the iPhone or iPad. I have tried several approaches but I'm not getting any animation even though the show/hides are working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我认为您使用了错误的动画选项。
将第二个动画选项替换为
UIViewAnimationOptionTransitionFlipFromLeft
(注意Animation
和Transition
之间的Option
)我相信
UIViewAnimationTransitionFlipFromLeft
(这是您代码中的内容)是UIViewAnimationTransition
而不是UIViewAnimationOptions
。I think you are using the wrong animation option.
Replace your second animation option by
UIViewAnimationOptionTransitionFlipFromLeft
(note theOption
betweenAnimation
andTransition
)I believe that
UIViewAnimationTransitionFlipFromLeft
(which is what you have in your code) is aUIViewAnimationTransition
not aUIViewAnimationOptions
.