简体   繁体   中英

Core Animation and drawRect:

I have a UITableViewCell drawn using drawRect: . I'm drawing an shape in drawRect and would like to animate this shape (using some parameter) when using the cell goes into edit mode.

However, I couldn't find how to use animated parameters within drawRect. Could someone point me to the right documentation or is it impossible?

Thanks!

Look into using CAShapeLayer for making shapes, it has an animatable property path .

CAShapeLayer* shape = [CAShapeLayer layer];
// Changing this will implicitly animate to new path
shape.path = [UIBezierPath bezierPathWithOvalInRect:someRect].CGPath;
shape.fillColor = [UIColor redColor].CGColor;

For more control over the animation, use CATransaction .

You're not clear on what kind of shape and what kind of animation. If you just need a basic shape such as a square, rectangle, or circle, you could avoid overriding drawRect and just use a CALayer--adding it to your cell's layer hierarchy. For other shapes, you can do the same with a shape layer.

You should consider drawRect for drawing view contents not for animating. It's fine if you want to render the view with drawRect, but if you're just going to move the shape, animate the view's center or the layer's position instead.

If you want to use Core Animation with drawRect (I would recommend against it), you will probably have to animate a custom property that you'll use in your drawRect, but I'm betting what you're trying to do can be done simpler without drawRect. Layers are your friend and can help you in a lot of situations. Maybe elaborate on what you're trying to do and someone can offer you a more complete solution.

Best Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM