我有一个使用bezierpath的绘画应用程序,我希望我创建的线条是平滑的。我的绘画应用程序创建像脊线一样的线条。它已经花了我一个星期,请帮助我。
这是我的视图控制器,它调用了笔画子视图。
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
我的笔画在视图上创建了线条。
stroke.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
stroke.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
答案 0 :(得分:0)
通过code查看此Erica Sadun代码使用BezierPath和Catmull-Rom拼接算法来提高性能。您还可以查看她的code to draw on screen