【问题标题】:Basic drawing with Quartz 2D on iPhone在 iPhone 上使用 Quartz 2D 进行基本绘图
【发布时间】:2010-05-05 17:28:45
【问题描述】:

我的目标是制作一个在触摸屏幕时绘制点的程序。这是我目前所拥有的:

头文件:

#import <UIKit/UIKit.h>


@interface ElSimView : UIView 
{
  CGPoint firstTouch;
  CGPoint lastTouch;
  UIColor *pointColor;
  CGRect *points;
  int npoints;
}

@property CGPoint firstTouch;
@property CGPoint lastTouch;
@property (nonatomic, retain) UIColor *pointColor;
@property CGRect *points;
@property int npoints;

@end

实现文件:

//@synths etc.

- (id)initWithFrame:(CGRect)frame 
{
    return self;
}


- (id)initWithCoder:(NSCoder *)coder
{
  if(self = [super initWithCoder:coder])
  {
    self.npoints = 0;
  }
  return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  firstTouch = [touch locationInView:self];
  lastTouch = [touch locationInView:self];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  lastTouch = [touch locationInView:self];  
  points = (CGRect *)malloc(sizeof(CGRect) * ++npoints);
  points[npoints-1] = CGRectMake(lastTouch.x-15, lastTouch.y-15,30,30);
  [self setNeedsDisplay];
}

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

- (void)drawRect:(CGRect)rect 
{
  CGContextRef context = UIGraphicsGetCurrentContext();

  CGContextSetLineWidth(context, 2.0);
  CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
  CGContextSetFillColorWithColor(context, pointColor.CGColor);

  for(int i=0; i<npoints; i++)
    CGContextAddEllipseInRect(context, points[i]);
  CGContextDrawPath(context, kCGPathFillStroke);
}


- (void)dealloc {
    free(points);
    [super dealloc];
}


@end

当我加载它并单击一些点时,它会正常绘制第一个点,然后绘制下一个点以及随机椭圆(甚至不是圆形)。

另外我还有一个问题:究竟drawRect什么时候被执行?

【问题讨论】:

  • drawRect 在视图第一次加载和你调用 setNeedsDisplay 时被调用
  • 除了 -drawRect: 问题,您的确切问题是什么? Stack Overflow 不是为代码审查而设计的,而是为回答特定问题而设计的。
  • 一般问题是:“为什么它不起作用?”。更具体的问题是“我应该将添加新点的部分放在哪里才能使其工作”?
  • 感谢您的澄清。我知道你现在遇到了什么问题。

标签: iphone quartz-graphics


【解决方案1】:

在 -touchesEnded:withEvent: 中,您有以下代码:

points = (CGRect *)malloc(sizeof(CGRect) * ++npoints);
points[npoints-1] = CGRectMake(lastTouch.x-15, lastTouch.y-15,30,30);

您正在为points 重新分配一个数组,但不会复制您之前的任何点。这导致您使用随机未初始化的内存值而不是保存的 CGRects。相反,请尝试以下操作:

CGRect *newPoints = (CGRect *)malloc(sizeof(CGRect) * ++npoints);
for (unsigned int currentPoint = 0; currentPoint < (npoints - 1); currentPoint++)
{
    newPoints[currentPoint] = points[currentPoint];
}
free(points);
points = newPoints;

points[npoints-1] = CGRectMake(lastTouch.x-15, lastTouch.y-15,30,30);

您之前还泄漏了 points 数组,因为在为其内容分配新数组之前没有释放它。

至于-drawRect:被调用的时候,是在iPhone OS显示系统需要重新缓存一个UIView的内容时触发的。正如 Daniel 所说,这通常发生在初始显示时以及每当您在 UIView 上手动调用 -setNeedsDisplay 时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多