【发布时间】:2013-12-01 02:26:19
【问题描述】:
在我的应用程序中,我在用户点击并拖动时画了一个圆圈。他们点击的地方是圆的中心,当他们拖动时,半径达到他们的手指。一旦你画了一个圆圈,如果你点击几次(不拖动),什么都不会发生,但下次你点击并拖动时,你之前所做的每次点击都会创建圆圈(与最后一个圆圈大小相同的圆圈) .这对我来说没有意义,因为我打电话给touchesBegan:withEvent 的唯一事情是
UITouch *touch = [touches anyObject];
center = [touch locationInView:self];
它应该每次都替换中心,但是当您最终移动手指时,它会在每次点击时绘制一个单独的圆圈。
touchesMoved:
UITouch *touch = [touches anyObject];
endPoint = [touch locationInView:self];
distance = (uses center's attributes);
[self setNeedsDisplay];
touchesEnded:
[self drawBitmap];
绘制位图:
if (!CGPointEqualToPoint(center, CGPointZero) && !CGPointEqualToPoint(endPoint, CGPointZero)) {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
if (!incrementalImage) { // first draw;
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
[[UIColor clearColor] setFill];
[rectpath fill]; // fill it
}
[incrementalImage drawAtPoint:CGPointZero];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, sW);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, o);
CGContextSetLineCap(context, kCGLineCapRound);
CGRect rectangle = CGRectMake(center.x - distance, center.y - distance, distance * 2, distance * 2);
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
绘制矩形
[incrementalImage drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, sW);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, o);
CGContextSetLineCap(context, kCGLineCapRound);
CGRect rectangle = CGRectMake(center.x - distance, center.y - distance, distance * 2, distance * 2);
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
为什么点按后会出现圆圈,但只有在拖动时才会出现?只应在拖动时绘制圆圈……对吗?我几乎 99% 确定问题出在 drawBitmap 上,但我不确定我应该怎么做才能检查它是否只是点击或拖动。另外,如果问题是drawBitmap,为什么我在touchesEnd时看不到圆圈?我只在拖动事件中看到它们。 EDIT 明确一点,我不希望在单击时只在拖动时绘制圆圈。
【问题讨论】:
标签: ios objective-c cocoa-touch uiview drawrect