How can I enable implicit UIView animation?

2019-03-04 07:10发布

Trying to wrap my brain around animations here. Here is my basic situation:

 _________
|Parent   |
|         |
|         |
|  _____  |  _____
| |A    | | |B    |
| |_____| | |_____|
|_________|

where Parent is the parent view and A and B are both UIView instances. I chose to make A and B UIViews because, as I understand it, layers cannot receive user interaction. The animation I want to do is very simple: I simply want A to slide offscreen to the left while B slides in from the right.

I tried something like the following:

CALayer *layer = viewA.layer;
layer.position = CGPointMake(initialLayer.position.x, initialLayer.position.y - 480);

but the property updated immediately with no animation.

I was able to get this working using CABasicAnimation without too much trouble, but I want to learn to use implicit animations for this kind of scenario so I can prototype more quickly in the future. This post seems to suggest that UIViews can't perform implicit animation (at least by default).

My question:

  1. Is there a way to implicitly animate properties on a UIView's layer? How?
  2. If not, is there a better/canonical way to do this that's different from my method?

1条回答
乱世女痞
2楼-- · 2019-03-04 08:00

You can use animateWithDuration:

[UIView animateWithDuration:2.0 animations:^{
    viewA.frame = newFrame;
}];
查看更多
登录 后发表回答