如何平滑绘图笔划?

时间:2012-11-27 05:01:34

标签: xcode paint

我正在创作绘画..我可以撤消,并在上面添加颜色。但是当我用我的手指画画时,笔画不是那么平滑并且有边缘线,

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

stroke.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

stroke.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

1 个答案:

答案 0 :(得分:0)

不应使用CGContextAddLines(),而应使用CGContextAddCurveToPoint()创建贝塞尔路径。你需要用4个点构建它,所以你可能需要使用前一个和后两个点,或者沿着这些点的东西。