2011-04-17 45 views

回答

7

例子:

- (void)drawRect:(NSRect)rect 
{ 
    NSBezierPath *curve = [NSBezierPath bezierPath]; 
    [curve moveToPoint:NSMakePoint(0,50)]; 
    [curve relativeCurveToPoint:NSMakePoint(150,50) controlPoint1:NSMakePoint(50,100) controlPoint2:NSMakePoint(100,0)]; 
    [[NSColor redColor] set]; 
    [curve stroke]; 
} 

結果:

Curve.png

+4

NSBezierPath在了AppKit框架,它不是IOS的一部分SDK但對於mac,那我該怎麼辦? – juliet 2011-04-22 05:27:29

+0

改爲使用CGContextRef,看到我的新答案,包括示例。 – Anne 2011-04-22 13:49:31

-1

創建NSBezierPath,並使用您的點作爲控制點。

9

在iOS上使用CGContextRef。

WavyView.h

#import <UIKit/UIKit.h> 

@interface WavyView : UIView { 

} 

@end 

WavyView.m

#import "WavyView.h" 

@implementation WavyView 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 2.0); 
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, 100, 100); 
    CGContextAddCurveToPoint(context,125,150,175,150,200,100); 
    CGContextAddCurveToPoint(context,225,50,275,75,300,200); 
    CGContextStrokePath(context); 
} 

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

@end 

結果:

The Wave

+0

我想把這個WavyView放到ViewController中,但是當我將它添加到UIViewController時,我所得到的只是一個黑色方塊。這裏是我使用WavyView * drawScreen = [[WIALines alloc] initWithFrame:CGRectMake(170,351,100,100)]的代碼; [self.view addSubview:drawScreen];我究竟做錯了什麼? – 2012-12-03 19:45:56