iOS绘图到屏幕

iOS绘图到屏幕,ios,core-graphics,draw,paint,Ios,Core Graphics,Draw,Paint,我正在使用核心图形绘制iOS屏幕,我设法能够在屏幕上绘制,但从我遵循的教程来看,绘图屏幕涵盖了nib文件中的所有其他内容。有人能帮我找出如何将绘图区域限制在我在笔尖中创建的视图内吗?我已经在这上面呆了一段时间了 我遵循的教程是: 我的代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. //Set te

我正在使用核心图形绘制iOS屏幕,我设法能够在屏幕上绘制,但从我遵循的教程来看,绘图屏幕涵盖了nib文件中的所有其他内容。有人能帮我找出如何将绘图区域限制在我在笔尖中创建的视图内吗?我已经在这上面呆了一段时间了

我遵循的教程是:

我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    //Set text from previous
    text.text = [[GameManager sharedManager] inText];

    drawImage = [[UIImageView alloc] initWithImage:nil];
    drawImage.frame = self.view.frame;
    [self.view addSubview:drawImage];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20;

    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) 
    {
            drawImage.image = nil;
            return;
    }

    if(!mouseSwiped) 
    {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

我已经成功地在一个子类中扩展了UIView,我从interface builder中的xib中引用了该子类。这样我就可以在interface builder中查看视图,移动它,并从interface builder中更改其属性。我按如下方式操作,但您可以按任意顺序操作:

  • 为视图创建一个新的UIView类实例,稍后将在interface builder中链接该实例
  • 在新视图类中,重写drawRect方法并在那里进行绘图。记住获取对图形上下文的引用,以便可以使用它进行绘制。我看到您经常使用UIGraphicsGetCurrentContext()方法。如果要处理复杂的事情,最好将其指定给变量,以避免开销并提高绘图性能
  • 在Interface Builder中,选择要将视图放入其中的视图控制器,然后从对象库中将新视图拖动到视图控制器上。根据需要调整空白框。
  • 最后,在interface builder中选择您的视图并转到“Identity Inspector”选项卡。在“自定义类->类”下拉列表中选择您之前创建的类(如果您在这里找不到它,请不要担心,重新启动Xcode并重试,这种情况有时会发生)
  • 虽然您的视图在interface builder中看起来是空白的,但当您在为视图设置的框架中运行它时,它应该绘制
  • 当用户触摸视图控制器时,我看到您正在绘图。这些触摸事件仍然可以在主视图控制器类中处理。要处理此问题,您可以更改子视图中的某些属性,并对子视图调用“setNeedsDisplay”,以确保在下次显示刷新时调用子视图的draw方法