0

我正在创建绘图.. 我可以撤消并在其上添加颜色。但是当我用手指画画时,笔触不是那么光滑,而且有边缘线,,

这是我的代码。我可以在其上绘制视图、撤消、更改颜色和不透明度。

行程.h

#import <UIKit/UIKit.h>

@interface stroke : UIView{
    NSMutableArray *strokeArray;
    UIColor *strokeColor;
    int strokeSize;
    float strokeAlpha;
    int strokeAlpha2;
    IBOutlet UISlider *slides;
    float red;
    float green;
    float blue;
    CGPoint mid1;
    CGPoint mid2; 
    CGPoint endingPoint,previousPoint1,previousPoint2;
    CGPoint currentTouch;
}

@property (nonatomic, retain) UIColor *strokeColor;
@property (nonatomic) int strokeSize;
@property (nonatomic, retain) NSMutableArray *strokeArray;
- (IBAction)changeAlphaValue;
-(void)loadSLider;
-(void)blueColor;
-(void)darkvioletColor;
-(void)violetColor;
-(void)pinkColor;
-(void)darkbrownColor;
-(void)redColor;
-(void)magentaRedColor;
-(void)lightBrownColor;
-(void)lightOrangeColor;
-(void)OrangeColor;
-(void)YellowColor;
-(void)greenColor;
-(void)lightYellowColor;
-(void)darkGreenColor;
-(void)TurquioseColor;
-(void)PaleTurquioseColor;
-(void)skyBlueColor;
-(void)whiteColor;
-(void)DirtyWhiteColor;
-(void)SilverColor;
-(void)LightGrayColor;
-(void)GrayColor;
-(void)LightBlackColor;
-(void)BlackColor;
@end

行程.m

#import "stroke.h"

@implementation stroke
@synthesize strokeColor;
@synthesize strokeSize;
@synthesize strokeArray;


- (void) awakeFromNib{
    self.strokeArray = [[NSMutableArray alloc] init];
    self.strokeColor = [UIColor colorWithRed:0 green:0 blue:232 alpha:1];
    self.strokeSize = 3;
}
- (void)drawRect:(CGRect)rect{
    NSMutableArray *stroke;
    for (stroke in strokeArray) {
        CGContextRef contextRef = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(contextRef, [[stroke objectAtIndex:1] intValue]);
        CGFloat *color = CGColorGetComponents([[stroke objectAtIndex:2] CGColor]);
        CGContextSetRGBStrokeColor(contextRef, color[0], color[1], color[2], color[3]);    
        CGContextBeginPath(contextRef);
        CGPoint points[[stroke count]];
        for (NSUInteger i = 3; i < [stroke count]; i++) {
            points[i-3] = [[stroke objectAtIndex:i] CGPointValue];
        }
        CGContextAddLines(contextRef, points, [stroke count]-3);
        CGContextStrokePath(contextRef);
    }

}
-(void)loadSLider{

}
- (IBAction)changeAlphaValue{
    strokeAlpha2 =((int)slides.value);
}
-(void)blueColor{
    red = 0/255.0;
    green = 0/255.0;
    blue = 255/255.0;
}
-(void)darkvioletColor{
    red = 75/255.0;
    green = 0/255.0;
    blue = 130/255.0;
}
-(void)violetColor{
    red = 128/255.0;
    green = 0/255.0;
    blue = 128/255.0;
}
-(void)pinkColor{
    red = 255/255.0;
    green = 0/255.0;
    blue = 255/255.0;
}
-(void)darkbrownColor{
    red = 0.200;
    green = 0.0;
    blue = 0.0;
}
-(void)redColor{
    red = 255/255.0;
    green = 0/255.0;
    blue = 0/255.0;
}
-(void)magentaRedColor{
    red = 0.350;
    green = 0.0;
    blue = 0.0;
}
-(void)lightBrownColor{
    red = 0.480;
    green = 0.0;
    blue = 0.0;
}
-(void)lightOrangeColor{
    red = 0.600;
    green = 0.200;
    blue = 0.0;
}
-(void)OrangeColor{
    red = 1.0;
    green = 0.300;
    blue = 0.0;
}
-(void)YellowColor{
    red = 0.950;
    green = 0.450;
    blue = 0.0;
}
-(void)greenColor{
    red = 0.0;
    green = 1.0;
    blue = 0.0;
}
-(void)lightYellowColor{
    red = 1.0;
    green = 1.0;
    blue = 0.0;

}
-(void)darkGreenColor{
    red = 0.0;
    green = 0.500;
    blue = 0.0;
}
-(void)TurquioseColor{
    red = 0.0;
    green = 0.700;
    blue = 0.200;
}
-(void)PaleTurquioseColor{
    red = 0.0;
    green = 0.700;
    blue = 0.600;
}
-(void)skyBlueColor{
    red = 0.0;
    green = 0.400;
    blue = 0.800;
}
-(void)whiteColor{
    red = 1.0;
    green = 1.0;
    blue = 1.0;
}
-(void)DirtyWhiteColor{
    red = 0.800;
    green = 0.800;
    blue = 0.800;
}
-(void)SilverColor{
    red = 0.600;
    green = 0.600;
    blue = 0.600;
}
-(void)LightGrayColor{
    red = 0.500;
    green = 0.500;
    blue = 0.500;
}
-(void)GrayColor{
    red = 0.300;
    green = 0.300;
    blue = 0.300;
}
-(void)LightBlackColor{
    red = 0.150;
    green = 0.150;
    blue = 0.150;
}
-(void)BlackColor{
    red = 0.0;
    green = 0.0;
    blue = 0.0;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch;

    NSEnumerator *counter = [touches objectEnumerator];
    while ((touch = (UITouch *)[counter nextObject])) {
        switch (strokeAlpha2) {
            case 1:
                strokeAlpha = .1;
                break;
            case 2:
                strokeAlpha = .2;
                break;
            case 3:
                strokeAlpha = .3;
                break;
            case 4:
                strokeAlpha = .4;
                break;
            case 5:
                strokeAlpha = .5;
                break;
            case 6:
                strokeAlpha = .6;
                break;
            case 7:
                strokeAlpha = .7;
                break;
            case 8:
                strokeAlpha = .8;
                break;
            case 9:
                strokeAlpha = .9;
                break;
            case 10:
                strokeAlpha = 1;
                break;
            default:
                strokeAlpha = 1;
                break;
        }

        self.strokeColor = [UIColor colorWithRed:red green:green blue:blue alpha:strokeAlpha];
        NSValue *touchPos = [NSValue valueWithCGPoint:[touch locationInView:self]];
        UIColor *color = [UIColor colorWithCGColor:strokeColor.CGColor];
        NSNumber *size = [NSNumber numberWithInt:strokeSize]; 
        NSMutableArray *stroke = [NSMutableArray arrayWithObjects: touch, size, color, touchPos, nil];
        [strokeArray addObject:stroke];
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch;

    NSEnumerator *counter = [touches objectEnumerator];

    while ((touch = (UITouch *)[counter nextObject])) {
        NSMutableArray *stroke;
        for (stroke in strokeArray) {
            if ([stroke objectAtIndex:0] == touch) {
                [stroke addObject:[NSValue valueWithCGPoint:[touch locationInView:self]]];
            }
            [self setNeedsDisplay];
        }
    }
}



@end
4

1 回答 1

0

而不是使用CGContextAddLines()你应该使用CGContextAddCurveToPoint(). 你需要用 4 个点来构造它,所以你可能需要使用前两个点和下一个点,或者类似的东西。

于 2012-11-27T05:08:45.327 回答