Is content drawn by drawRect capable of animating UIView animations?

In iOS, if there are two circles, both of which are UIView objects, then their center or frame can be animated using the UIView animation.

But the problem is that the UITreeView container containing these two circles will draw a line connecting the centers of the circles using drawRect . In drawRect I could use CG or UIKit to draw a line, and I decided to use UIBezierPath moveToPoint and addLineToPoint to draw a line, which should actually only be CG calls under, but easier to use.

But when one circle comes to life in another position, the line will not animate with it, so the animation may look somehow strange. Is there any way to make it animated?

I tried animating in the TreeView class instead of inside the UINodeView (circle) class, but that won't help. In fact, if I draw a circle using a random color in the drawRect method, when the circle moves slowly in the animation, the color will not change quickly, which means that its drawRect not called. This is probably just a CALayer in which the cached image is animated.

So, is there a way to make an animated line as well?

Other methods that I can think of are as follows:

1) Use hack to use UIView to hold the line so that its frame can be animated, but what if the circle moves so that the line should be drawn inside the UIView from (0, 0) to (200, 200), but now the line should be tilted in another way, starting from (200, 0) to (0, 200), so in this case, probably, this will not work. (But if I use some kind of transformation to flip the image at the same time, it will probably work, but it seems difficult to consider when to use the transformation)

2) use CADisplayLink to make the animation, and just let it call [treeView setNeedsDisplay]; , in which case the drawRect code will be called. (this may be redundant, and since the entire TreeView background will be removed first, the screen may flash slightly)

Or is there an easier or better way?

+4
source share
1 answer

You can assign your CAShapeLayer path (for example, wrap it in a subclass of UIView, which returns CAShapeLayer as + layerClass). The path is assigned to the CGPath -path property (and note that UIBezierPath provides the CGPath property). Then you have a line in the view that can be animated (e.g. group animation using UIView + beginAnimations .

+1
source

Source: https://habr.com/ru/post/1432806/


All Articles