0

ベジエパスを使用したペイント アプリを持っています。作成している線を滑らかにしたいのです。私のペイントアプリは、稜線のような線を作成します。すでに1週間かかっています。助けてください。

これは、ストローク サブビューと呼ばれるビュー コントローラーです。

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

viewController.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 に答える