1

我正在使用以下代码使用 UIBezierPath 制作画笔。

.h File
@interface MyLineDrawingView : UIView
{
    UIBezierPath *myPath;
    UIColor *brushPattern;
}

@end

.m File
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"0010.png"]];
        myPath=[[UIBezierPath alloc]init];
        myPath.lineWidth=30;
        brushPattern=[UIColor redColor]; 
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [brushPattern setStroke];
    [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
   // [myPath strokeWithBlendMode:kCGBlendModeSaturation alpha:1.0];

    // Drawing code
    //[myPath stroke];
}

#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];   
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];

}

它与UIView. 正如您在上面看到的自定义类继承自UIVIew. 但是当我进行子类UIImageView化而不是UIView我无法绘制任何东西时。Toches 方法被调用,但它在屏幕上什么也没画。有谁知道这有什么问题或我该如何解决?

我想将其更改为UIImageViewfrom的原因UIView是我想设置图像并更改颜色。当我使用UIView和使用

self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"0010.png"]];

图像不适合查看。整个图像只有一部分是可见的。即使我将内容模式更改为UIViewContentModeScaleToFill整个图像也不可见。

4

2 回答 2

2

从 UIImageView 文档:

UIImageView 类经过优化,可以将其图像绘制到显示器上。UIImageView 不会调用 drawRect: 一个子类。如果您的子类需要自定义绘图代码,建议您使用 UIView 作为基类。

总之,在 UIImageView 子类中不能使用 drawRect。将图像视图添加为子视图或将图像绘制为 drawRect 的一部分。

于 2012-04-17T09:43:22.843 回答
0

可以使用以下代码完成:

@interface Canvas : UIImageView {
    CGPoint location;
}

@property CGPoint location;
.m file
@synthesize location;

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

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

    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext();  
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay);


    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self];

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();  
  //  CGContextSetBlendMode(ctx, kCGBlendModeOverlay);

    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;     
}
于 2012-04-18T09:19:18.483 回答