objective-c 如何在两个以上的 UIViews 之间进行翻转动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/238654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:51:59  来源:igfitidea点击:

How to do a flip animation between more than two UIViews?

iphoneobjective-ccocoa-touch

提问by rksprst

I have animation code in a class that extends the UIView:

我在扩展 UIView 的类中有动画代码:

// Start Animation Block
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];

//int nextView = currentView + 1;
// Animations
[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//And so I would do: [[self superview exchangeSubviewAtIndex:currentView withSubviewAtIndex:nextView];
//Doesn't work because curentView is not global... with each instance of a class it's reset to what it's instanciated to. 
//currentView++;

// Commit Animation Block
[UIView commitAnimations];

This animation code works fine for two views. But I am trying to do this for many views. I tried by adding in a global variable (nextview index and currentView index), however this doesn't work since each custom view class has it's own instance and these variables aren't shared.

此动画代码适用于两个视图。但是我正在尝试针对许多视图执行此操作。我尝试添加一个全局变量(nextview 索引和 currentView 索引),但是这不起作用,因为每个自定义视图类都有自己的实例并且这些变量不共享。

Does anyone know how I can accomplish what I want to do?

有谁知道我怎样才能完成我想做的事?

Thanks!

谢谢!

Edit: I would be able to do this, if I could use [self superview] to access the index of the currentview and of the nextview. Are there some methods to do this?

编辑:如果我可以使用 [self superview] 访问当前视图和下一个视图的索引,我将能够做到这一点。有没有一些方法可以做到这一点?

回答by Max Stewart

Apple have a pretty good tutorial on view transitions here. Other than the way they implement them you can also look at using a UINavigationController which manages a bunch of views and transitioning between them.

Apple 有一个关于视图转换的非常好的教程here。除了他们实现它们的方式之外,您还可以使用 UINavigationController 来管理一堆视图并在它们之间进行转换。

Essentially you need to manage the transition in the superview rather than the view - the superview can know who it's currently displaying and who's next.

本质上,您需要在超级视图而不是视图中管理过渡 - 超级视图可以知道它当前显示的是谁以及下一个显示的是谁。

回答by Noah Witherspoon

If you have pointers to the views you're trying to switch between, just call -removeFromSuperview on the "from" view and -addSubview:(the "to" view) on the superview. The transition doesn't rely on any particular method call; more or less any form of the "swap two views" approach will be animated properly.

如果您有指向要在它们之间切换的视图的指针,只需在“来自”视图上调用 -removeFromSuperview 并在超级视图上调用 -addSubview:(“to”视图)。转换不依赖于任何特定的方法调用;或多或少任何形式的“交换两个视图”方法都将正确动画。

回答by Matt James

For those who find this question who are supporting iOS 4.0 and higher, Apple has provided a built-in API for this task:

对于那些发现此问题且支持 iOS 4.0 及更高版本的人,Apple 已为此任务提供了内置 API:

See: transitionFromView:toView:duration:options:completion:

请参阅:transitionFromView:toView:duration:options:completion:

回答by Nick Stinemates

I think a Linked List of views might be what you should think about implementing.

我认为链接列表视图可能是您应该考虑实施的内容。