【问题标题】:iOS drawing to screeniOS绘图到屏幕
【发布时间】:2011-06-08 20:45:42
【问题描述】:

我正在使用核心图形在 iOS 屏幕上绘图,并且我设法能够在屏幕上绘图,但是从我遵循的教程中,绘图屏幕涵盖了我的 nib 文件中的所有其他内容。谁能帮我弄清楚如何将绘图区域限制在我在笔尖中创建的视图内?我已经被困了一段时间了。

我遵循的教程是这样的: http://www.ifans.com/goto/http://www.touchrepo.com/SampleCode/TEST_DRAW_APP.zip

我的代码:

- (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();
    }
}

【问题讨论】:

    标签: ios core-graphics draw paint


    【解决方案1】:

    我已经成功地在一个子类中扩展了 UIView,我在界面生成器中从我的 xib 中引用了该子类。这样我就可以在界面构建器中看到视图,移动它并从界面构建器更改它的属性。我这样做如下,但您可以按您喜欢的任何顺序执行此操作:

    1. 为您的视图创建一个新的 UIView 类实例,稍后您将在界面构建器中链接该实例。
    2. 在您的新视图类中覆盖drawRect 方法并在那里进行绘图。请记住获取对图形上下文的引用,以便您可以使用它进行绘制。我看到你经常使用 UIGraphicsGetCurrentContext() 方法。如果您正在做复杂的事情,最好将其分配给一个变量以避免开销并提高绘图性能。
    3. 在 Interface Builder 中,选择要放入视图的视图控制器,然后从对象库中将新视图拖到视图控制器上。根据需要调整空白框的大小。
    4. 最后在界面生成器中选择您的视图,然后转到“身份检查器”选项卡。在 Custom Class -> Class 下拉菜单中选择您之前创建的类(如果在这里找不到,请不要担心,重新启动 Xcode 并重新开始尝试,有时会发生这种情况)。
    5. 虽然您的视图在界面构建器中看起来是空白的,但当您在为视图设置的框架中运行它时,它应该会绘制出来。

    当用户触摸视图控制器时,我看到您正在绘图。这些触摸事件仍然可以在您的主视图控制器类中处理。要处理这个问题,您可以更改子视图中的一些属性并在子视图上调用“setNeedsDisplay”,以确保在下次显示刷新时调用子视图的绘制方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      相关资源
      最近更新 更多