很难说,所以我上传了一张图片来显示我的问题:http: //i42.tinypic.com/2eezamo.jpg
基本上在drawRect中,我会在手指触摸时从touchesMoved画线,我会调用“needsDisplayInRect”进行重绘。但是我发现第一行已经完成,第二行会清除rect部分,所以之前的一些绘图就没有了。
这是我的实现:
enter code here
-(void) drawRect:(CGRect)rect{
//[super drawRect: rect];
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawSquiggle:squiggle at:rect inContext:context];
}
- (void)drawSquiggle:(Squiggle *)squiggle at:(CGRect) rect inContext:(CGContextRef)context
{
CGContextSetBlendMode(context, kCGBlendModeMultiply);
UIColor *squiggleColor = squiggle.strokeColor; // get squiggle's color
CGColorRef colorRef = [squiggleColor CGColor]; // get the CGColor
CGContextSetStrokeColorWithColor(context, colorRef);
NSMutableArray *points = [squiggle points]; // get points from squiggle
// retrieve the NSValue object and store the value in firstPoint
CGPoint firstPoint; // declare a CGPoint
[[points objectAtIndex:0] getValue:&firstPoint];
// move to the point
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
// draw a line from each point to the next in order
for (int i = 1; i < [points count]; i++)
{
NSValue *value = [points objectAtIndex:i]; // get the next value
CGPoint point; // declare a new point
[value getValue:&point]; // store the value in point
// draw a line to the new point
CGContextAddLineToPoint(context, point.x, point.y);
} // end for
CGContextStrokePath(context);
}
我尝试实现这一点,但似乎它没有按我的预期工作。每次在视图上调用 setNeedsDisplay 时,我都实现了一个视图来绘制一个正方形。我将 CGImageRef 图像和 CGBitmapContextRef 上下文放入 ivar 并在视图初始化时分配它们。
#import "View.h"
@implementation View
#define bitsPerComponent 8
#define bitsPerPixel 4*bitsPerComponent
#define bytesPerPixel 4
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
lastPoint = CGPointMake(50, 50);
size_t width = frame.size.width;
size_t height = frame.size.height;
CFMutableDataRef data = CFDataCreateMutable( NULL , width * height * bytesPerPixel );
CGDataProviderRef provider = CGDataProviderCreateWithCFData( data );
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
/*CGImageRef - ivar */ image = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, width*bytesPerPixel, colorspace, kCGImageAlphaPremultipliedLast, provider, NULL, NO, kCGRenderingIntentDefault);
/*CGBitmapContextRef - ivar */ context = CGBitmapContextCreate(CFDataGetMutableBytePtr(data), width, height, bitsPerComponent, width*bytesPerPixel , colorspace, kCGImageAlphaPremultipliedLast);
CFRelease( data ); // retained by provider I think
CGDataProviderRelease( provider ); // retained by image
CGColorSpaceRelease(colorspace);
}
return self;
}
- (void)drawRect:(CGRect)rect {
NSLog(@"test");
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextFillRect(context, CGRectMake(lastPoint.x, lastPoint.y, 100, 100));
lastPoint = CGPointMake( ((int)lastPoint.x+50) % 380 , ((int)lastPoint.y+50) % 220 );
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 100, 100),image);
}
- (void)dealloc {
[super dealloc];
}
@end