Example from Mac OS X:
Mac OS X中的示例:
[[aView animator] setFrame:NSMakeRect(100.0,100.0,300.0,300.0)];
I have tried something similar in UIKit, but it seems the animator method is not there for UIView. So there's no "implicit" animation?
我在UIKit中尝试了类似的东西,但似乎动画方法不适用于UIView。那么没有“隐含”的动画?
2 个解决方案
#1
The iPhone supports implicit animation, though it does not use an animator object, it is built directly into UIView. Try this instead:
iPhone支持隐式动画,虽然它不使用动画对象,但它直接构建在UIView中。试试这个:
[UIView beginAnimations:nil context:nil];
[aView setFrame:NSMakeRect(100.0,100.0,300.0,300.0)];
[UIView commitAnimations];
The exact details are documented here.
这里记录了确切的细节。
Every view is always layer backed as well, so you don;t have to turn on layers in order to get a layer for explicit animation.
每个视图也始终都是图层支持,因此您不必打开图层以获得用于显式动画的图层。
#2
Turns out iPhone OS does indeed support implicit animations on CALayer as defined in the Core Animation Programming Guide from Apple. On Mac OS X, a layer becomes animatable when it backs an NSView by setting:
事实证明,iPhone OS确实支持CALayer上的隐式动画,如Apple的核心动画编程指南中所定义。在Mac OS X上,通过设置以下内容,当图层支持NSView时,该图层将变为可动画:
[view setWantsLayer:YES];
On iPhone OS, simply set the layer delegate to the UIViewController-derived class that hosts the UIView-derived object you would like to animate. For example, if you have a property defined for a UILabel called label, inside of the UIViewController-derived class that hosts it, the code would look something like this (an example of an event handler for a UIButton Touch Up Inside event):
在iPhone OS上,只需将图层委托设置为UIViewController派生类,该类承载您想要设置动画的UIView派生对象。例如,如果您为UILabel定义了一个名为label的属性,则在托管它的UIViewController派生类中,代码看起来像这样(UIButton Touch Up Inside事件的事件处理程序示例):
-(void) buttonPressedToAnimateTheLabel:(id)sender
{
CALayer *labelLayer = self.label.layer;
labelLayer.delegate = self;
layer.position = CGPointMake(labelLayer.position.x, labelLayer.position.y + 50);
}
The label will then animate in the exact same manner as it would on Mac OS X just by changing the position.
然后,只需更改位置,标签就会以与在Mac OS X上完全相同的方式进行动画处理。
Some notes about this:
一些关于此的说明:
-
it is definitely the UIViewController that is the delegate, not the UIView. Making a UIView the delegate causes the app to blow up.
肯定是UIViewController是委托,而不是UIView。制作代表UIView会导致应用程序爆炸。
-
the delegate needs to be set in a method called after the UIViewController-derived class has completely finished loading, i.e. in an event handler for a control hosted by the class. I tried adding it in initWithNibName:bundle:, awakeFromNib, viewDidLoad, and viewWillAppear:animated and it did not work. Setting the delegate in the first two methods caused the label not to animate; it just changed positions immediately. Adding it in the last two caused the label to disappear altogether.
委托需要在UIViewController派生类完全加载后调用的方法中设置,即在由类托管的控件的事件处理程序中设置。我尝试在initWithNibName中添加它:bundle:,awakeFromNib,viewDidLoad和viewWillAppear:animated并且它不起作用。在前两种方法中设置委托导致标签不动画;它只是立即改变立场。在最后两个中添加它会导致标签完全消失。
-
after the delegate is set once in an event handler, it will be good for the remainder of the lifetime of the UIViewController-derived class. Changing the layer properties in other event handlers will cause them to animate without setting the delegate for the layer again.
在委托在事件处理程序中设置一次后,它将适用于UIViewController派生类的生命周期的剩余时间。更改其他事件处理程序中的图层属性将使它们动画,而无需再次为图层设置委托。
-
when the UIViewController-derived class loses focus, the delegate must be set to nil in the viewWillDisappear:animated method, or else the application will blow up
当UIViewController派生类失去焦点时,必须在viewWillDisappear:animated方法中将委托设置为nil,否则应用程序将爆炸
There must be some place to set the delegate one time only so it isn't set multiple times as a result of a event handler being called, but I have yet to find that place. At least it works this way and setting it multiple times as a result of the event handler being called multiple times seems harmless enough.
必须有一些地方只设置一次委托,因此不会因为调用事件处理程序而多次设置,但我还没有找到那个地方。至少它以这种方式工作,并且由于多次调用事件处理程序而多次设置它似乎无害。
It would certainly be nice if this was all documented in the Core Animation Programming Guide. I spent days trying to figure this out! Hope this saves all the trouble of hunting it down!
如果核心动画编程指南中都记录了这一点,那肯定会很好。我花了好几天试图解决这个问题!希望这可以省去狩猎的所有麻烦!
#1
The iPhone supports implicit animation, though it does not use an animator object, it is built directly into UIView. Try this instead:
iPhone支持隐式动画,虽然它不使用动画对象,但它直接构建在UIView中。试试这个:
[UIView beginAnimations:nil context:nil];
[aView setFrame:NSMakeRect(100.0,100.0,300.0,300.0)];
[UIView commitAnimations];
The exact details are documented here.
这里记录了确切的细节。
Every view is always layer backed as well, so you don;t have to turn on layers in order to get a layer for explicit animation.
每个视图也始终都是图层支持,因此您不必打开图层以获得用于显式动画的图层。
#2
Turns out iPhone OS does indeed support implicit animations on CALayer as defined in the Core Animation Programming Guide from Apple. On Mac OS X, a layer becomes animatable when it backs an NSView by setting:
事实证明,iPhone OS确实支持CALayer上的隐式动画,如Apple的核心动画编程指南中所定义。在Mac OS X上,通过设置以下内容,当图层支持NSView时,该图层将变为可动画:
[view setWantsLayer:YES];
On iPhone OS, simply set the layer delegate to the UIViewController-derived class that hosts the UIView-derived object you would like to animate. For example, if you have a property defined for a UILabel called label, inside of the UIViewController-derived class that hosts it, the code would look something like this (an example of an event handler for a UIButton Touch Up Inside event):
在iPhone OS上,只需将图层委托设置为UIViewController派生类,该类承载您想要设置动画的UIView派生对象。例如,如果您为UILabel定义了一个名为label的属性,则在托管它的UIViewController派生类中,代码看起来像这样(UIButton Touch Up Inside事件的事件处理程序示例):
-(void) buttonPressedToAnimateTheLabel:(id)sender
{
CALayer *labelLayer = self.label.layer;
labelLayer.delegate = self;
layer.position = CGPointMake(labelLayer.position.x, labelLayer.position.y + 50);
}
The label will then animate in the exact same manner as it would on Mac OS X just by changing the position.
然后,只需更改位置,标签就会以与在Mac OS X上完全相同的方式进行动画处理。
Some notes about this:
一些关于此的说明:
-
it is definitely the UIViewController that is the delegate, not the UIView. Making a UIView the delegate causes the app to blow up.
肯定是UIViewController是委托,而不是UIView。制作代表UIView会导致应用程序爆炸。
-
the delegate needs to be set in a method called after the UIViewController-derived class has completely finished loading, i.e. in an event handler for a control hosted by the class. I tried adding it in initWithNibName:bundle:, awakeFromNib, viewDidLoad, and viewWillAppear:animated and it did not work. Setting the delegate in the first two methods caused the label not to animate; it just changed positions immediately. Adding it in the last two caused the label to disappear altogether.
委托需要在UIViewController派生类完全加载后调用的方法中设置,即在由类托管的控件的事件处理程序中设置。我尝试在initWithNibName中添加它:bundle:,awakeFromNib,viewDidLoad和viewWillAppear:animated并且它不起作用。在前两种方法中设置委托导致标签不动画;它只是立即改变立场。在最后两个中添加它会导致标签完全消失。
-
after the delegate is set once in an event handler, it will be good for the remainder of the lifetime of the UIViewController-derived class. Changing the layer properties in other event handlers will cause them to animate without setting the delegate for the layer again.
在委托在事件处理程序中设置一次后,它将适用于UIViewController派生类的生命周期的剩余时间。更改其他事件处理程序中的图层属性将使它们动画,而无需再次为图层设置委托。
-
when the UIViewController-derived class loses focus, the delegate must be set to nil in the viewWillDisappear:animated method, or else the application will blow up
当UIViewController派生类失去焦点时,必须在viewWillDisappear:animated方法中将委托设置为nil,否则应用程序将爆炸
There must be some place to set the delegate one time only so it isn't set multiple times as a result of a event handler being called, but I have yet to find that place. At least it works this way and setting it multiple times as a result of the event handler being called multiple times seems harmless enough.
必须有一些地方只设置一次委托,因此不会因为调用事件处理程序而多次设置,但我还没有找到那个地方。至少它以这种方式工作,并且由于多次调用事件处理程序而多次设置它似乎无害。
It would certainly be nice if this was all documented in the Core Animation Programming Guide. I spent days trying to figure this out! Hope this saves all the trouble of hunting it down!
如果核心动画编程指南中都记录了这一点,那肯定会很好。我花了好几天试图解决这个问题!希望这可以省去狩猎的所有麻烦!