2012-03-23 6 views
0

私はiOSを初めて使用しており、学習経験としてこれをやっています。私はこのコードの大半をチュートリアルとその作業から得ましたが、ストロークカラーを追加したかったのです。私のコードはUIColor greenColorのデフォルトストロークで始まりますが、押されたときにストロークの色を赤または青に変える必要がある "赤"と "青"の2つのボタンがあります。私は赤を押してから描画を開始し、MSペイントのようにそのストロークが赤くなるようにしたい。 UIButtonが正しくリンクされていることを確認しました。どんな助けでも感謝しています!iOSクォーツストロークカラーを変更する

DrawView.h:

#import <UIKit/UIKit.h> 
@interface DrawView : UIImageView 
- (void)changeOption:(NSString *)withOption; 
@end 

DrawView.m:

#import "DrawView.h" 
@interface DrawView() 
@property (nonatomic) BOOL fingerMoved; 
@property (nonatomic) CGPoint lastPoint; 
@end 

@implementation DrawView 

CGFloat red = 0.0, green = 0.0, blue = 0.0, width = 5.0; 
@synthesize fingerMoved = _fingerMoved; 
@synthesize lastPoint = _lastPoint; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)changeOption:(NSString *)withOption 
{ 
    if ([withOption isEqualToString:@"Black"]) { 
     red = 0.0; green = 0.0; blue = 0.0; width = 5.0; 
    }else if ([withOption isEqualToString:@"Red"]) { 
     red = 1.0; green = 0.0; blue = 0.0; width = 5.0; 
    }else if ([withOption isEqualToString:@"Blue"]) { 
     red = 0.0; green = 0.0; blue = 1.0; width = 5.0; 
    }else if ([withOption isEqualToString:@"Green"]) { 
     red = 0.0; green = 1.0; blue = 0.0; width = 5.0; 
    }else if ([withOption isEqualToString:@"Brown"]) { 
     red = 0.4; green = 0.0; blue = 0.0; width = 5.0; 
    }else if ([withOption isEqualToString:@"Orange"]) { 
     red = 1.0; green = 0.6; blue = 0.0; width = 5.0; 
    }else if ([withOption isEqualToString:@"Yellow"]) { 
     red = 1.0; green = 1.0; blue = 0.0; width = 5.0; 
    }else if ([withOption isEqualToString:@"Purple"]) { 
     red = 0.5; green = 0.0; blue = 1.0; width = 5.0; 
    }else if ([withOption isEqualToString:@"Eraser"]) { 
     red = 1.0; green = 1.0; blue = 1.0; width = 13.0; 
    }else if ([withOption isEqualToString:@"Clear"]) { 
     self.image = nil; 
    } 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    self.fingerMoved = NO; 
    UITouch *touch = [touches anyObject]; 
    if ([touch tapCount] == 2) { 
     self.image = nil; 
     return; 
    } 

    self.lastPoint = [touch locationInView:self]; 
    self.lastPoint = CGPointMake(self.lastPoint.x, self.lastPoint.y - 20); 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    self.fingerMoved = YES; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self]; 
    currentPoint.y -= 20; 

    UIGraphicsBeginImageContext(self.frame.size); 
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), width); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    self.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    self.lastPoint = currentPoint; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    if ([touch tapCount] == 2) { 
     self.image = nil; 
     return; 
    } 

    if (!self.fingerMoved) { 
     UIGraphicsBeginImageContext(self.frame.size); 
     [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), width); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     self.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 
+0

ストロークの色の部分を設定するのが正しいと思われます。確認するだけで、今すぐ描画するときは緑で描画します。 –

+0

'chooseColor'メソッドは' NSString'という色名で呼び出されます。メソッドが有効なカラー名で呼び出されていますか? – FluffulousChimp

+0

これは非常によく見えます。私たちが確認できるように、ボタンが呼び出すコードを投稿してください。 – lnafziger

答えて

-1

いくつかの他のコードで愚かな間違い。今修正されました。

関連する問題