为绘图应用程序制作图层结构

发布于 2024-09-28 17:30:36 字数 396 浏览 1 评论 0原文

关于我的问题一个较早的问题,我尝试过但失败了创建一个类,其中包含包含 CALayerRef 的 NSMuttableArray 成员变量。有人可以指导我如何做到这一点吗?我想做的基本上是创建 CALayerRef 或 CGLayerRef 或其他什么,将它们推入我的 Layers 变量中,然后,当我需要时它们,获取,使用它们的上下文,最后绘制/隐藏/显示/删除它们。

我向你们求助,伙计们,因为显然,网上几乎没有关于在高级水平上使用图层和 Quartz 的信息。每个人都可以立即使用这些层,不需要管理,也不需要成员变量。

谢谢。

In relation to question an eralier question of mine, I have tried and failed to create a class with a NSMuttableArray member variable holding CALayerRefs. Can someone please guide me on how to do that. What I want to do is basically create CALayerRefs or CGLayerRefs or whatever, push them into my layers variable, and then, when I need them, fetch, use their context and finally draw/hide/show/delete them.

I turn to you, guys, because apparently, there is few to none information on the net on working with layers and Quartz on an advanced level. Everybody uses the layers right away, no management needed, no member variables.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-10-05 17:30:36

这是我在几分钟内编写的一些自定义视图的工作代码,希望对您有所帮助。它创建 10 个绿色层,并每秒将它们动画化到不同的位置。

MBLineLayerDelegate *lineLayerDelegate;
@property (nonatomic, retain) NSMutableArray *ballLayers;

- (void)awakeFromNib
{
    self.ballLayers = [NSMutableArray array];
    lineLayerDelegate = [[MBLineLayerDelegate alloc] init];
    for (NSUInteger i = 0; i < 10; i++) {
        CALayer *ball = [CALayer layer];
        CGFloat x = self.bounds.size.width * (CGFloat)random()/RAND_MAX;
        CGFloat y = self.bounds.size.height * (CGFloat)random()/RAND_MAX;
        ball.frame = CGRectMake(x, y, 20, 20);
        ball.backgroundColor = [UIColor greenColor].CGColor;
        ball.delegate = lineLayerDelegate;
        [self.layer addSublayer:ball];
        [self.ballLayers addObject:ball];
    }

    [self performSelector:@selector(animateBallsToRandomLocation) withObject:nil afterDelay:0];
}

- (void)animateBallsToRandomLocation
{
    for (CALayer *layer in self.ballLayers) {
        CGFloat x = self.bounds.size.width * (CGFloat)random()/RAND_MAX;
        CGFloat y = self.bounds.size.height * (CGFloat)random()/RAND_MAX;
        layer.position = CGPointMake(x, y);
    }
    [self performSelector:@selector(animateBallsToRandomLocation) withObject:nil afterDelay:1];
}

下面是 CALayer 委托绘制一条线的一些代码:

@interface MBLineLayerDelegate : NSObject {
}
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx;
@end

@implementation MBLineLayerDelegate

- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
        CGRect rect = layer.bounds;
        CGContextSaveGState(context);

        CGContextTranslateCTM(context, 0.0, rect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSetAllowsAntialiasing(context, YES);
        CGContextSetShouldAntialias(context, YES);

        CGContextMoveToPoint(context, 0, 0);
        CGContextAddLineToPoint(context, rect.size.width, rect.size.height);

        CGContextRestoreGState(context);
}

@end

Here's some working code for custom view I wrote in few minutes, hope it helps. It creates 10 green layers, and animates them each second to different locations.

MBLineLayerDelegate *lineLayerDelegate;
@property (nonatomic, retain) NSMutableArray *ballLayers;

- (void)awakeFromNib
{
    self.ballLayers = [NSMutableArray array];
    lineLayerDelegate = [[MBLineLayerDelegate alloc] init];
    for (NSUInteger i = 0; i < 10; i++) {
        CALayer *ball = [CALayer layer];
        CGFloat x = self.bounds.size.width * (CGFloat)random()/RAND_MAX;
        CGFloat y = self.bounds.size.height * (CGFloat)random()/RAND_MAX;
        ball.frame = CGRectMake(x, y, 20, 20);
        ball.backgroundColor = [UIColor greenColor].CGColor;
        ball.delegate = lineLayerDelegate;
        [self.layer addSublayer:ball];
        [self.ballLayers addObject:ball];
    }

    [self performSelector:@selector(animateBallsToRandomLocation) withObject:nil afterDelay:0];
}

- (void)animateBallsToRandomLocation
{
    for (CALayer *layer in self.ballLayers) {
        CGFloat x = self.bounds.size.width * (CGFloat)random()/RAND_MAX;
        CGFloat y = self.bounds.size.height * (CGFloat)random()/RAND_MAX;
        layer.position = CGPointMake(x, y);
    }
    [self performSelector:@selector(animateBallsToRandomLocation) withObject:nil afterDelay:1];
}

Here's some code for CALayer's delegate that draws a line:

@interface MBLineLayerDelegate : NSObject {
}
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx;
@end

@implementation MBLineLayerDelegate

- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
        CGRect rect = layer.bounds;
        CGContextSaveGState(context);

        CGContextTranslateCTM(context, 0.0, rect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSetAllowsAntialiasing(context, YES);
        CGContextSetShouldAntialias(context, YES);

        CGContextMoveToPoint(context, 0, 0);
        CGContextAddLineToPoint(context, rect.size.width, rect.size.height);

        CGContextRestoreGState(context);
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文