2015-05-06 42 views
0

这个问题几乎不言自明:我需要使用SpriteKit绘制一条看起来像正弦波的线,但我还需要稍后改变此波的振幅。如何使用SpriteKit绘制正弦曲线?

+0

谷歌 “的iOS画正弦波”。 – sangony

+0

@sangony,导致我到这个问题:http://stackoverflow.com/questions/23985840/drawing-an-infinite-sine-wave 这不是我想要的。相反,我想绘制一个幅度可变的正弦波**。这个问题已经得到了回答。 – DDPWNAGE

回答

3

的基本步骤... 1)创建一个SKShapeNode,2)产生的正弦曲线CGPath,和3)指定CGPath到形状节点的属性path

-(void)didMoveToView:(SKView *)view {   
    self.scaleMode = SKSceneScaleModeResizeFill; 

    // Create an SKShapeNode 
    SKShapeNode *node = [SKShapeNode node]; 
    node.position = CGPointMake(300.0, 300.0); 
    // Assign to the path attribute 
    node.path = [self sineWithAmplitude:20.0 frequency:1.0 width:200.0 
           centered:YES andNumPoints:32]; 

    [self addChild:node]; 
} 

// Generate a sinusoid CGPath 
- (CGMutablePathRef)sineWithAmplitude:(CGFloat)amp frequency:(CGFloat)freq 
           width:(CGFloat)width centered:(BOOL)centered 
         andNumPoints:(NSInteger)numPoints { 

    CGFloat offsetX = 0; 
    CGFloat offsetY = amp; 

    // Center the sinusoid within the shape node 
    if (centered) { 
     offsetX = -width/2.0; 
     offsetY = 0; 
    } 

    CGMutablePathRef path = CGPathCreateMutable(); 

    // Move to the starting point 
    CGPathMoveToPoint(path, nil, offsetX, offsetY); 
    CGFloat xIncr = width/(numPoints-1); 

    // Construct the sinusoid 
    for (int i=1;i<numPoints;i++) { 
     CGFloat y = amp * sin(2*M_PI*freq*i/(numPoints-1)); 
     CGPathAddLineToPoint(path, nil, i*xIncr+offsetX, y+offsetY); 
    } 

    return path; 
} 
+0

老兄......你摇滚。谢谢! – DDPWNAGE