【问题标题】:iPhone Quartz - Call Drawing Method From Other ClassiPhone Quarts - 从另一个类调用绘图方法
【发布时间】:2012-05-29 11:25:02
【问题描述】:

我正在尝试从Class A 调用绘图方法,例如,位于Class B 的方法,正在调用该方法但没有绘图发生。

- (void)drawIt
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);

    NSString *string = [NSString stringWithString:@"TEXT"];
    [string drawAtPoint:CGPointMake(12, 51) withFont:[UIFont fontWithName:@"Helvetica" size:35.0f]];
}

为什么我可以从其他类调用这个方法?

【问题讨论】:

    标签: iphone ios objective-c quartz-graphics quartz-2d


    【解决方案1】:

    首先创建类'YourView',它是 UIView 的子类。编写分配代码viewDidLoad方法,属于B类

    - (void)viewDidLoad{
     YourView *temp = [[YourView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [self.view addSubview:temp];
    }
    

    在 YourView.m 中实现 - (void)drawRect:(CGRect)rect 方法

    - (void)drawRect:(CGRect)rect
    {
    CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
        NSString *string = [NSString stringWithString:@"TEXT"];
        [string drawAtPoint:CGPointMake(12, 51) withFont:[UIFont fontWithName:@"Helvetica" size:35.0f]];
    }
    

    我认为这对你会有帮助。

    【讨论】:

    • 谢谢,但我不想在初始化后直接绘制...我还希望能够手动绘制和擦除绘图
    【解决方案2】:

    如果您使用的是 UIView 或某些子类,则需要重载 drawRect 方法。因此,在 drawRect 内部,您可以在其他类中调用您的方法。此外,您也可以通过参数传递上下文。

    【讨论】:

    • 谢谢,调用的超类是 UITableView,我正在使用 drawRect 但我不想在对象 init 上进行绘制。
    猜你喜欢
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多