4

我想在同一个for循环中将对象绘制到两个单独的 CGLayer,但不确定如何执行此操作。

例如,我想在三个蓝色圆圈后面画三个橙色圆圈,橙色圆圈在一层,蓝色圆圈在另一层。以下代码会将每个圆圈放在前一个圆圈的顶部:

-(void) drawRect:(CGRect)rect {
    UIBezierPath *circle;
    for (int i = 1; i <= 3; i++) {
        // Create an orange circle
        circle = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(CGRectMake(i*50, 80, 50, 50), 0, 0)];
        circle.lineWidth = 4.0f;
        [[UIColor colorWithRed:1.0 green:0.75 blue:0 alpha:1.0] setFill];
        [[UIColor orangeColor] setStroke];
        [circle stroke];
        [circle fill];

        // Create a blue circle
        circle = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(CGRectMake(25 + i*50, 80, 50, 50), 0, 0)];
        circle.lineWidth = 4.0f;
        [[UIColor colorWithRed:0 green:0.5 blue:1.0 alpha:1.0] setFill];
        [[UIColor blueColor] setStroke];
        [circle stroke];
        [circle fill];
    }
}

上面代码的输出

我将如何修改它以使三个橙色圆圈最终orangeLayer位于 a 中的三个蓝色圆圈后面blueLayer?我想这与保存和恢复上下文有关,但我无法真正理解它。

非常感谢!

PS:我意识到我可以简单地使用两个for循环内联来实现正确的效果,但这个例子是为了学习分层的教学目的。谢谢!

4

2 回答 2

0

我构建了 UIView 的自定义子类,并创建了一个函数 makeCircleWithFrame 以使用 UIGraphicsBeginImageContextWithOptions 在视图内绘制一个圆圈,我相信它会解决您的主要问题:

#import "circleView.h"
#import <math.h>

@implementation circleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (!self) return self;

    [self setupViews];

    return self;
}

- (void)awakeFromNib
{
    [self setupViews];
}

- (void)setupViews
{
    [self setBackgroundColor:[UIColor whiteColor]];


    for (int i = 1; i <= 6; i++) {

        UIColor *circleColor;

        //Math function just to set different colors for each circle
        if (fmodf(i, 2) == 0) {
            circleColor = [UIColor colorWithRed:1.0 green:0.75 blue:0 alpha:1.0];
        }
        else {
            circleColor = [UIColor colorWithRed:0 green:0.5 blue:1.0 alpha:1.0];
        }


        UIView *circleView = [self makeCircleWithFrame:(CGRectMake(10*i, 10*i, 100, 100)) andFillColor:circleColor];
        circleView.tag = i;
        NSLog(@"circle %i", i);
    }
}

- (UIView *)makeCircleWithFrame:(CGRect)rect andFillColor:(UIColor *)color {
    // declare UIimageView, not UIView
    UIImageView *customView = [[UIImageView alloc] init];
    customView.frame= self.bounds;

    // create a new contex to draw
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, 0);

    UIBezierPath *grayCircle = [UIBezierPath bezierPathWithOvalInRect:rect];

    grayCircle.lineWidth = 6;
    [color setFill];
    [[UIColor orangeColor] setStroke];
    [grayCircle stroke];
    [grayCircle fill];

    customView.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    [self addSubview:customView];

    return customView;
}

如果您仍有问题或需要更多帮助,请给我反馈。

于 2014-11-02T01:30:00.467 回答
0

为此,您需要部分绘制蓝色或橙色圆圈(取决于您想要在哪个圆圈上)。您可能知道只有两种可能的选择:a)橙色层在顶部 2)蓝色层在上面。我认为,如果您想按颜色对圆圈进行分组(每个圆圈不使用 1 层),您最好:1)使用 1 层进行绘图 2)将贝塞尔路径(代表圆圈)存储在某处 3)在根据您需要的顺序和叠加。

于 2014-11-04T04:33:25.513 回答