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メソッドが呼び出されますが、画面には何も描画されません。誰かがこれの何が悪いのか知っていますか、またはどうすればこれを解決できますか?

からに変更したいのは、画像を設定して色を変更したいからですUIImageViewUIView使っUIViewて使うとき

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

画像が表示に適合しません。画像全体の一部のみが表示されます。コンテンツモードをに変更してUIViewContentModeScaleToFillも、画像全体が表示されません。

4

2 に答える 2

2

From the UIImageView documentation:

The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.

In short, drawRect can't be used in UIImageView subclasses. Add an image view as a subview or draw the image as part of your drawRect instead.

于 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 に答える