首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用UIView更新自定义drawRect?

如何用UIView更新自定义drawRect?
EN

Stack Overflow用户
提问于 2011-05-31 21:40:11
回答 1查看 10.8K关注 0票数 2

我正在尝试在学习的同时构建一些iOS应用程序,并且在理解正确的方法时遇到了一些困难。

我目前拥有的是一个视图,它是UIView的一个子类。这是清楚的,我想用它作为绘图表面。它会放在其他的东西上面,比如描图纸。

用户应该能够点击一个点,然后另一个点,一条线应该画之间的两个点。我得到了触摸数据,我得到了点,并且我能够从drawRect内部画一些东西:最初。

问题是我不知道以后该怎么更新。当所有的东西都加载到drawRect: is calle时,它就会划出一条很好的线。但是我如何使它画新的东西或修改已经根据用户正在做的事情绘制的东西。我知道我需要调用setNeedsDisplay,但不知道如何将数据放到视图中来绘制内容。

我读过许多教程/例子,它们都停在“重写drawRect:然后画一些东西.完成了!”。如何将数据向下传递到视图中,告诉它“嘿,重新绘制这些内容并添加这条新行”。

还是我走错路了?

编辑:,我将尝试更好地解释我的设置。

我有个风投。在这个VC的视图中,我在底部有一个工具栏。其余的区域被2视图占据。一个是持有参考图像的图像视图。一个是位于顶部的清晰的定制视图(描图纸)。他们单击工具栏上的一个按钮,该按钮打开手势识别器。他们点击屏幕,我收集数据,关闭手势识别器,希望画一条线。除了绘图部分之外,我已经把所有的工作都做好了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-31 22:07:35

你走在正确的轨道上。听起来你需要跟踪这些点。

下面是一些示例代码。

LineDrawView.h

代码语言:javascript
运行
复制
#import <UIKit/UIKit.h>
@interface LineDrawView : UIView 
{
    NSMutableArray *lines;
    CGPoint pointA, pointB;
    BOOL activeLine;
}
@end

LineDrawView.m

代码语言:javascript
运行
复制
#import "LineDrawView.h"
@implementation LineDrawView
- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self.backgroundColor = [UIColor blackColor];
        lines = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void)dealloc 
{
    [lines release];
    [super dealloc];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    CGPoint point = [[touches anyObject] locationInView:self];
    if ([lines count] == 0) pointA = point;
    else pointB = point;
    activeLine = YES;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    pointB = [[touches anyObject] locationInView:self];
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    pointB = [[touches anyObject] locationInView:self];
    [lines addObject:[NSArray arrayWithObjects:[NSValue valueWithCGPoint:pointA], [NSValue valueWithCGPoint:pointB], nil]];
    pointA = pointB;
    pointB = CGPointZero;
    activeLine = NO;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(c, 2);
    CGContextSetLineCap(c, kCGLineCapRound);
    CGContextSetLineJoin(c, kCGLineJoinRound);
    CGContextSetStrokeColorWithColor(c, [UIColor grayColor].CGColor);
    for (NSArray *line in lines)
    {
        CGPoint points[2] = { [[line objectAtIndex:0] CGPointValue], [[line objectAtIndex:1] CGPointValue] };
        CGContextAddLines(c, points, 2);
    }
    CGContextStrokePath(c);
    if (activeLine)
    {
        CGContextSetStrokeColorWithColor(c, [UIColor whiteColor].CGColor);
        CGPoint points2[2] = { pointA, pointB };
        CGContextAddLines(c, points2, 2);
        CGContextStrokePath(c);
    }
}
@end
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6193888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档