iOS 绘图到屏幕

发布于 2024-11-14 16:50:34 字数 3089 浏览 2 评论 0原文

我正在使用核心图形在 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();
    }
}

I am working on drawing to the iOS screen using core graphics, and I managed to be able to draw on the screen, but from the tutorial I followed, the drawing screen covers everything else in my nib file. Can anyone help me figure out how to confine the drawing area to within the view I created in my nib? I've been stuck on this for a while.

The tutorial I followed was this:
http://www.ifans.com/goto/http://www.touchrepo.com/SampleCode/TEST_DRAW_APP.zip

My code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2024-11-21 16:50:34

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

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

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

I've had success with extending a UIView in a sub class that I reference from my xib in interface builder. That way I can see the view in interface builder, move it and change it's properties from interface builder. I do it as follows but but you can do this in any order you like:

  1. Create a new UIView class instance for your view that you will later link in interface builder.
  2. In your new view class override the drawRect method and do your drawing there. Remember to get a reference to the graphics context so that you can draw with it. I see you use the UIGraphicsGetCurrentContext() method often. It's better to assign this to a variable to avoid overhead and improve drawing performance if you're doing complex things.
  3. In Interface Builder select the view controller you want to put your view in and drag a new view onto your view controller from the Object Library. Resize the blank box as required.
  4. Lastly select your view in interface builder and go the the 'Identity Inspector' tab. Choose the class you created earlier under the Custom Class -> Class drop down (if you can't find it here don't worry, restart Xcode and start try again, this happens sometimes).
  5. While your view will look blank in interface builder it should draw when you run it in the frame you set for the view.

I see you're drawing when the user touches with the view controller. These touch events can still be handled in your main view controller class. To handle this you can change some properties in your subview and call 'setNeedsDisplay' on your sub view to ensure the sub view's draw method is called on the next display refresh.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文