最佳实践:将视图动画显示到iPhone上

时间:2023-01-07 23:07:59

If I'm looking to use Core Animation to fade a view in and out of display -- is it good practice to have the UIView in the same NIB as the view that it is being drawn into? Should I build the view in another NIB file and load it in from there? I'm looking to animate three small views into display simultaneously upon a user action. I wanted to construct the views in IB. Looking for a best practice type response here.

如果我想使用Core Animation来淡出显示的视图 - 将UIView放在与被绘制的视图相同的NIB中是一种好习惯吗?我应该在另一个NIB文件中构建视图并从那里加载它吗?我希望在用户操作时同时将三个小视图动画显示。我想在IB中构建视图。在这里寻找最佳实践类型的回应。

3 个解决方案

#1


You can have the three views as separate UIViews inside a single XIB, or lay out the three views within one single view (otherwise, you'll have to position them in code). Set their initial alpha values to 0.0, then fading them in is as easy as:

您可以将三个视图作为单个XIB内的单独UIViews,或者在一个视图中布置三个视图(否则,您必须将它们放在代码中)。将它们的初始alpha值设置为0.0,然后淡入它们就像下面这样简单:

[UIView beginAnimations:@"fade" context:nil];
[UIView setAnimationDuration:1.0];
myViewOne.alpha = 1.0;
myViewTwo.alpha = 1.0;
myViewThree.alpha = 1.0;
[UIView commitAnimations];

I wouldn't put each of the three views in separate XIBs if you're planning on displaying them all together. I usually only break stuff up like that if they're parts of separate "screens" which will never be shown together.

如果您计划将它们全部显示在一起,我不会将三个视图中的每一个放在单独的XIB中。如果它们是单独的“屏幕”的一部分,我通常只会破坏它们,这些屏幕永远不会一起显示。

#2


Apple recommends one XIB per UIViewController/UIView combination. This is because if you place several ViewCOntrollers and Views in the same XIB, they are all loaded at the same time and use up memory for all the elements of the XIB, even if just one is on screen.

Apple建议每个UIViewController / UIView组合使用一个XIB。这是因为如果在同一个XIB中放置多个ViewCOntrollers和Views,它们将同时加载并占用内存以用于XIB的所有元素,即使屏幕上只有一个。

The recommended method is to use MainWindow.xib to have proxys to each of the xibs (UIViewController/UIView combos) you plan on loading.

推荐的方法是使用MainWindow.xib为您计划加载的每个xib(UIViewController / UIView组合)提供代理。

If you are just loading subviews, as above, you can construct them in the same xib, or build them programatically in viewDidLoad or viewWillAppear (which will be called after the xib is loaded with your "basic" view.

如果您只是加载子视图,如上所述,您可以在同一个xib中构建它们,或者在viewDidLoad或viewWillAppear中以编程方式构建它们(将在xib加载“基本”视图后调用它们。

#3


I think interface builder should be used as much as possible... so I recommend that each view be its own xib file... remember you can build a hierarchy of classes that extend from UIViewController if there is alot of overlap in functionality between the views.

我认为应该尽可能多地使用界面构建器...所以我建议每个视图都是它自己的xib文件...记住你可以构建一个从UIViewController扩展的类层次结构,如果在它们之间有很多重叠的功能观点。

#1


You can have the three views as separate UIViews inside a single XIB, or lay out the three views within one single view (otherwise, you'll have to position them in code). Set their initial alpha values to 0.0, then fading them in is as easy as:

您可以将三个视图作为单个XIB内的单独UIViews,或者在一个视图中布置三个视图(否则,您必须将它们放在代码中)。将它们的初始alpha值设置为0.0,然后淡入它们就像下面这样简单:

[UIView beginAnimations:@"fade" context:nil];
[UIView setAnimationDuration:1.0];
myViewOne.alpha = 1.0;
myViewTwo.alpha = 1.0;
myViewThree.alpha = 1.0;
[UIView commitAnimations];

I wouldn't put each of the three views in separate XIBs if you're planning on displaying them all together. I usually only break stuff up like that if they're parts of separate "screens" which will never be shown together.

如果您计划将它们全部显示在一起,我不会将三个视图中的每一个放在单独的XIB中。如果它们是单独的“屏幕”的一部分,我通常只会破坏它们,这些屏幕永远不会一起显示。

#2


Apple recommends one XIB per UIViewController/UIView combination. This is because if you place several ViewCOntrollers and Views in the same XIB, they are all loaded at the same time and use up memory for all the elements of the XIB, even if just one is on screen.

Apple建议每个UIViewController / UIView组合使用一个XIB。这是因为如果在同一个XIB中放置多个ViewCOntrollers和Views,它们将同时加载并占用内存以用于XIB的所有元素,即使屏幕上只有一个。

The recommended method is to use MainWindow.xib to have proxys to each of the xibs (UIViewController/UIView combos) you plan on loading.

推荐的方法是使用MainWindow.xib为您计划加载的每个xib(UIViewController / UIView组合)提供代理。

If you are just loading subviews, as above, you can construct them in the same xib, or build them programatically in viewDidLoad or viewWillAppear (which will be called after the xib is loaded with your "basic" view.

如果您只是加载子视图,如上所述,您可以在同一个xib中构建它们,或者在viewDidLoad或viewWillAppear中以编程方式构建它们(将在xib加载“基本”视图后调用它们。

#3


I think interface builder should be used as much as possible... so I recommend that each view be its own xib file... remember you can build a hierarchy of classes that extend from UIViewController if there is alot of overlap in functionality between the views.

我认为应该尽可能多地使用界面构建器...所以我建议每个视图都是它自己的xib文件...记住你可以构建一个从UIViewController扩展的类层次结构,如果在它们之间有很多重叠的功能观点。