How to draw wavy line on iOS device

后端 未结 3 1580
悲&欢浪女
悲&欢浪女 2020-12-29 16:14

I have many points constructing a line. How can I draw a wavy line through those points?

相关标签:
3条回答
  • 2020-12-29 16:57

    Example:

    - (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];
    }
    

    Result:

    Curve.png

    0 讨论(0)
  • 2020-12-29 17:00

    create a NSBezierPath , and use your points as the control points.

    0 讨论(0)
  • 2020-12-29 17:08

    Use CGContextRef on iOS.

    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
    

    Result:

    The Wave

    0 讨论(0)
提交回复
热议问题