Objective c 绘制移动正弦波消除混叠
我想画一个移动的正弦波,以清晰和抗锯齿的方式,具有可变频率和可变振幅。这怎么可能呢?好吧,我在UIView drawrect方法中实现了正弦波,如下所示:Objective c 绘制移动正弦波消除混叠,objective-c,cocoa-touch,ios,graphics,Objective C,Cocoa Touch,Ios,Graphics,我想画一个移动的正弦波,以清晰和抗锯齿的方式,具有可变频率和可变振幅。这怎么可能呢?好吧,我在UIView drawrect方法中实现了正弦波,如下所示: float x=75; float yc=50; float w=0; while (w<=rect.frame.size.width) { CGPathMoveToPoint(path, nil, w,y/2); CGPathAddQuadCurveToPoint(path, nil, w+x/4, -yc
float x=75;
float yc=50;
float w=0;
while (w<=rect.frame.size.width) {
CGPathMoveToPoint(path, nil, w,y/2);
CGPathAddQuadCurveToPoint(path, nil, w+x/4, -yc,w+ x/2, y/2);
CGPathMoveToPoint(path, nil, w+x/2,y/2);
CGPathAddQuadCurveToPoint(path, nil, w+3*x/4, y+yc, w+x, y/2);
CGContextAddPath(context, path);
CGContextDrawPath(context, kCGPathStroke);
w+=x;
}
float x=75;
浮动yc=50;
浮点数w=0;
而(w则是GeneratorOfOne版本的一个更完整、更快速的版本。这一版本还用所选的颜色填充了波浪底部:
class WaveView: UIView {
private var maskPath: UIBezierPath!
@IBInspectable var fillColor: UIColor = UIColor.blueColor()
@IBInspectable var cycles: CGFloat = 7
override func drawRect(rect: CGRect) {
var w: CGFloat = 0 // Starting position
let width = rect.width
let y: CGFloat = rect.height
let yc: CGFloat = rect.height / 2
let x = width/cycles
let context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, UIColor.greenColor().CGColor);
let path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 0, 0)
while (w<=rect.width) {
CGPathMoveToPoint(path, nil, w,y/2);
CGPathAddQuadCurveToPoint(path, nil, w+x/4, -yc, (w+x/2), y/2);
CGPathMoveToPoint(path, nil, w+x/2,y/2);
CGPathAddQuadCurveToPoint(path, nil, w+3*x/4, y+yc, w+x, y/2);
w+=x;
}
CGPathAddLineToPoint(path, nil, rect.width, rect.height)
CGPathAddLineToPoint(path, nil, 0, rect.height)
CGPathAddLineToPoint(path, nil, 0, y/2);
CGPathCloseSubpath(path)
maskPath = UIBezierPath(CGPath: path)
maskPath.lineCapStyle = CGLineCap.Square
maskPath.lineJoinStyle = CGLineJoin.Miter
CGContextAddPath(context, path);
CGContextSetFillColorWithColor(context, fillColor.CGColor)
CGContextFillPath(context)
}
}
类WaveView:UIView{
私有var maskPath:UIBezierPath!
@IBInspectable变量fillColor:UIColor=UIColor.blueColor()
@IBInspectable var cycles:CGFloat=7
重写func drawRect(rect:CGRect){
变量w:CGFloat=0//起始位置
let width=rect.width
设y:CGFloat=rect.height
设yc:CGFloat=rect.height/2
设x=宽度/周期
让context=UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(上下文,UIColor.greenColor().CGColor);
让path=CGPathCreateMutable();
CGPathMoveToPoint(路径,零,0,0)
而(wIt)不是正弦波