我是iOS的新手,我正在做这个学习经历。我从教程及其工作中获得了大部分代码,但我想添加更多笔触颜色。我的代码以UIColor greenColor中的默认笔划开始,但我有两个标记为“红色”和“蓝色”的按钮,按下时应将笔触颜色更改为红色或蓝色。我想按下红色,然后开始绘画并让那个笔画出红色等,就像在MS Paint中一样。我已经确认我的UIButtons已正确链接。感谢任何帮助,谢谢!
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 :(得分:-1)
其他一些代码中的愚蠢错误。现在修好了。