如何在iOS设备上绘制波浪线

jul*_*iet 4 iphone cocoa-touch objective-c ios

我有许多点构建一条线.如何通过这些点绘制波浪线?

Ann*_*nne 9

在iOS上使用CGContextRef.

WavyView.h

#import <UIKit/UIKit.h>

@interface WavyView : UIView {

}

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

结果:

海浪


Ann*_*nne 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];
}
Run Code Online (Sandbox Code Playgroud)

结果:

Curve.png

  • NSBezierPath在AppKit框架中,它不是ios sdk的一部分,但是对于mac,我该怎么办? (4认同)