动画一个UIView的CoreGraphics中/内容的drawRect [英] Animate a UIView's CoreGraphics/drawRect content

查看:146
本文介绍了动画一个UIView的CoreGraphics中/内容的drawRect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能一个动画的的UIView CoreGraphics中的内容?

Is it possible to animate a UIView's CoreGraphics content?

说我有叫的UIView MyView的实现的drawRect:方法,像这样:

Say I have a UIView subclass called MyView that implements the drawRect: method like so:

- (void) drawRect: (CGRect) rect {
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(c, someColor);
    CGContextSetLineWidth(c, someWidth);
    CGContextMoveToPoint(c, leftOfMyUIView, topOfMyUIView);
    CGContextAddLineToPoint(c, leftOfMyUIView, bottomOfMyUIView);
    CGContextStrokePath(c);
}

在换言之 - 它绘制的垂直线向下我UIView的子类的左侧。有没有一种简单的方法,使该行从左侧移动到视图右侧到现在这个动画UIView的?我会把它想'漂'从左至右。

In other words - it draws a vertical line down the left hand side of my subclass of UIView. Is there an easy way to now animate this UIView so that the line moves from the left side to the right side of the view? I would like it to 'drift' from left to right.

需要明确的是 - 的原因,我不会进入我不能移动/动画 MyView的的实例。我能想到使用最好的解决办法是增加一个新的的UIView 子类。像 LineView东西延伸的UIView 然后进行LineView我想画,利用自身的填充线的确切尺寸的drawRect 方法,然后到 MyView的添加 LineView 的实例作为一个子视图。这将让我用一个动画块动画 LineView 对象的位置,但似乎过于复杂,实现这么简单的东西。

To be clear - for reasons I will not go into I cannot move/animate the instance of MyView. The best solution I can think of using is to add a new UIView subclass. Something like LineView extends UIView and then make LineView the exact dimensions of the line I want to draw, fill it using its own drawRect method and then add an instance of LineView to MyView as a subview. This would allow me to animate the position of the LineView object using an animation block, but seems overly complicated to achieve something so simple.

推荐答案

基于您的评论将有数百LineView实例,它可能是最好使用CALayer的子类,而不是UIView的子类。 UIView的对象给你的事件处理,并在iOS每个UIView的拥有的CALayer处理其渲染。如果你不希望/需要事件的各条线跟踪,所有这些UIView的实例可能是不必要的开销。您可以访问父的UIView的层,并根据需要添加行子层。

Based upon your comment that there will be hundreds of LineView instances, it may be best to use CALayer subclasses as opposed to UIView subclasses. UIView objects give you event handling, and on iOS each UIView owns a CALayer to handle its rendering. If you don't want/need event tracking for the individual lines, all those UIView instances are probably needless overhead. You can access the parent UIView's layer, and add your line sublayers as needed.

如果我正确地理解你的问题,应该没有必要CoreGraphics中。您可以设置线路层的backgroundColor来处理填充。或者,如果行不会是直的,则可以使用CAShapeLayer呈现线的路径。

If I understand your problem correctly, there should be no need for CoreGraphics. You can set the line layers' backgroundColor to handle the fill. Or if the lines will not be straight, you can use CAShapeLayer to render the paths of the lines.

这篇关于动画一个UIView的CoreGraphics中/内容的drawRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆