0

我有一个使用 bezierpath 的绘图应用程序,我希望我创建的线条流畅。我的绘图应用程序创建了类似于脊线的线条。我已经花了一个星期,请帮助我。

这是我的视图控制器,它称为中风子视图。

视图控制器.h

#import <UIKit/UIKit.h>
@class Strokes;

@interface ViewController : UIViewController {
        Strokes *strokes;
        UISegmentedControl *colorControl;
        UISegmentedControl *bkControl;
        UISlider *slider;
}
@property (nonatomic, retain) IBOutlet UISegmentedControl *bkControl;
@property (nonatomic, retain) IBOutlet UISegmentedControl *colorControl;
@property (nonatomic, retain) IBOutlet Strokes *strokes;
@property (nonatomic, retain) IBOutlet UISlider *slider;
- (IBAction)sizSlider:(id)sender;
- (IBAction)pickColor:(id)sender;
- (IBAction)clearAll:(id)sender;
- (IBAction)undoStroke: (id)sender;
- (IBAction)showGrid:(id)sender;

@end

视图控制器.m

#import "ViewController.h"
#import "Strokes.h"

@implementation SecondViewController

@synthesize strokes;
@synthesize colorControl;
@synthesize bkControl;
@synthesize slider;

- (IBAction)clearAll:(id)sender {
    [strokes.strokeArray removeAllObjects];
    [strokes setNeedsDisplay];
}

- (IBAction)undoStroke: (id)sender {
    if ([strokes.strokeArray count]>0) {
        [strokes.strokeArray removeLastObject];
        [strokes setNeedsDisplay];
    }
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)sizSlider:(id)sender{
 strokes.strokeSize=(int)slider.value;
 }

- (IBAction)showGrid:(id)sender {
    if (bkControl.selectedSegmentIndex==0) {
        UIColor *bkg = [[UIColor alloc] initWithPatternImage: [UIImage imageNamed: @"grid.jpg"]];
        strokes.backgroundColor=bkg;
    }
    else if (bkControl.selectedSegmentIndex==1) {
        UIColor *bkg = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];;
        strokes.backgroundColor=bkg;
    }
}

- (IBAction)pickColor:(id)sender {
      if (colorControl.selectedSegmentIndex==0) {
        strokes.strokeColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:1];
      }
      else if (colorControl.selectedSegmentIndex==1) {
        strokes.strokeColor=[UIColor colorWithRed:250 green:0 blue:0 alpha:1];
      }
      else if (colorControl.selectedSegmentIndex==2) {
        strokes.strokeColor=[UIColor colorWithRed:100 green:100 blue:0 alpha:1];
      }
      else if (colorControl.selectedSegmentIndex==3) {
        strokes.strokeColor=[UIColor colorWithRed:0 green:0 blue:250 alpha:1];
      } 
      else if (colorControl.selectedSegmentIndex==4) {
        strokes.strokeColor=[UIColor colorWithRed:0 green:250 blue:0 alpha:1];
      }
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [slider release];
    [strokes release];
    [colorControl release];
    [bkControl release];
    [super dealloc];
}

@end

我在视图上创建线条的笔划。

行程.h

进口

@interface Strokes : UIView {
    NSMutableArray *strokeArray;
    UIColor *strokeColor;
    int strokeSize;
}

@property (nonatomic, retain) UIColor *strokeColor;
@property (nonatomic) int strokeSize;
@property (nonatomic, retain) NSMutableArray *strokeArray;

@end

行程.m

#import "Strokes.h"


@implementation Strokes

@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 = 1;
}

- (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)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch;
    NSEnumerator *counter = [touches objectEnumerator];
    while ((touch = (UITouch *)[counter nextObject])) {
        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];
        }
    }
}

- (void)dealloc {
    [strokeColor release];
    [super dealloc];
}

@end
4

1 回答 1

0

看看Erica Sadun的这段代码,该代码使用 BezierPath 和 Catmull-Rom 样条算法来提高性能。您还可以查看她在屏幕上绘制的代码

于 2012-10-27T07:00:37.937 回答