iOS绘图到屏幕

标签 ios core-graphics draw paint

我正在使用核心图形在 iOS 屏幕上绘图,我设法能够在屏幕上绘图,但是根据我遵循的教程,绘图屏幕覆盖了我的 nib 文件中的所有其他内容。谁能帮我弄清楚如何将绘图区域限制在我在 Nib 中创建的 View 内?我已经坚持了一段时间。

我遵循的教程是这样的: 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();
    }
}

最佳答案

我已经成功地在我从界面生成器中的 xib 引用的子类中扩展了 UIView。这样我就可以在界面生成器中看到 View ,移动它并从界面生成器更改它的属性。我按如下方式进行,但您可以按照您喜欢的任何顺序进行:

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

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

关于iOS绘图到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6285038/

相关文章:

ios - 当 applicationWillResignActive 时从 AppDelegate 暂停计时器

ios - 如何为简单的 iOS 应用获取 WebElement "label"?按标签名称搜索失败,定位器策略无效”

ios - #selector 函数被意外调用

iphone - UIButton 阴影看起来不正确

lua - 忽略 Lua 中的元表?

ios - 如何在关闭 View 时继续通过 avplayer 运行声音

iPhone 在 Quartz 绘图与预烘焙图像方面的性能差异(我猜可以简化为 Quartz 与 Quartz)

ios - CGContextDrawImage 绘制大图像非常模糊

java - ActionListener 不绘图,也不调用函数

ios - 在 iOS 中快速绘制大图像的一小部分