我正在尝试从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]];
}
为什么我可以从其他类调用此方法?
首先创建类 ' YourView
',它是 UIView 的子类。编写 B 类viewDidLoad
方法的分配代码
- (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]];
}
我认为这对你有帮助。
如果你使用的是UIView或某个子类,你需要重载drawRect方法。因此,在drawRect中,您可以在其他类中调用您的方法。此外,您也可以通过参数传递上下文。