大佬教程收集整理的这篇文章主要介绍了ios – SpriteKit中的Animate路径绘图,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
func start() { var i:Double = 1.0 let spinAction:SKAction = SKAction.repeatAction(SKAction.sequence([ SKAction.runBlock({ self.drawCirclePercent(i * 0.01) if (++i > 100.0) { i = 1.0 } }),SKAction.waitForDuration(0.01) ]),count: 100) runAction(spinAction) } func drawCirclePercent(percent:DoublE) { UIGraphicsBeginImageContext(self.sizE) let ctx:CGContextRef = UIGraphicsGetCurrentContext() CGContextSaveGState(ctX) CGContextSetLineWidth(ctx,lineWidth) CGContextSetRGBstrokeColor(ctx,1.0,1.0) CGContextAddArc(ctx,CGFloat(self.size.width/2.0),CGFloat(self.size.height/2.0),radius/2.0,CGFloat(M_PI * 1.5),CGFloat(M_PI * (1.5 + 2.0 * percent)),0) CGContextstrokePath(ctX) let textureImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() texture = SKTexture(image: textureImagE) UIGraphicsEndImageContext() }
虽然上面的代码有效,但它肯定不是很漂亮或有效,而且很可能不是SKActions的用途. CABasicAnimation解决方案更优雅,更高效.
let path:CGMutablePathRef = CGPathCreateMutable() CGPathAddArc(path,nil,40.0,CGFloat(M_PI_2 * 3.0),CGFloat(M_PI_2 * 7.0),falsE) let pathLayer:CAShapeLayer = CAShapeLayer() pathLayer.frame = CGRectMake(100,100,80.0,80.0) pathLayer.path = path pathLayer.strokeColor = SKColor.whiteColor().CGColor pathLayer.fillColor = nil pathLayer.lineWidth = 3.0 self.view.layer.addSublayer(pathLayer) let pathAnimation:CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd") pathAnimation.duration = 2.0 pathAnimation.fromValue = 0.0 pathAnimation.toValue = 1.0 pathLayer.addAnimation(pathAnimation,forKey: "strokeEndAnimation")
我的问题是我真的更喜欢将所有代码都包含在SKSpriteNode的子类中(以至于如果以上两个解决方案是我唯一的选择,我会选择第一个).有没有什么方法可以让我的SKAction实现更接近CoreAnimation实现,而不需要包含CoreAnimation?基本上,我想知道SpriteKit是否具有我不知道哪些功能可用于改进第一个实现的功能.
着色器作为文件添加到Xcode项目“animatestroke.fsh”:
void main() { if ( u_path_length == 0.0 ) { gl_FragColor = vec4( 0.0,0.0,1.0 ); } else if ( v_path_distance / u_path_length <= u_current_percentage ) { gl_FragColor = vec4( 1.0,1.0 ); } else { gl_FragColor = vec4( 0.0,0.0 ); } }
和样本SKScene子类使用它:
import SpriteKit import GameplayKit func shaderWithFil@R_801_8371@( _ fil@R_801_8371@: String?,fileExtension: String?,uniforms: [SKUniform] ) -> SKShader { let path = Bundle.main.path( forresource: fil@R_801_8371@,ofType: fileExtension ) let source = try! NSString( contentsOfFile: path!,encoding: String.Encoding.utf8.rawValue ) let shader = SKShader( source: source as String,uniforms: uniforms ) return shader } class GameScene: SKScene { let strokeSizeFactor = CGFloat( 2.0 ) var strokeShader: SKShader! var strokeLengthUniform: SKUniform! var _strokeLengthFloat: Float = 0.0 var strokeLengthKey: String! var strokeLengthFloat: Float { get { return _strokeLengthFloat } set( newstrokeLengthFloat ) { _strokeLengthFloat = newstrokeLengthFloat strokeLengthUniform.floatValue = newstrokeLengthFloat } } override func didMove(to view: SKView) { strokeLengthKey = "u_current_percentage" strokeLengthUniform = SKUniform( name: strokeLengthKey,float: 0.0 ) let uniforms: [SKUniform] = [strokeLengthUniform] strokeShader = shaderWithFil@R_801_8371@( "animatestroke",fileExtension: "fsh",uniforms: uniforms ) strokeLengthFloat = 0.0 let cameraNode = SKCameraNode() self.camera = cameraNode let strokeHeight = CGFloat( 200 ) * strokeSizeFactor let path1 = CGMutablePath() path1.move( to: CGPoint.zero ) path1.addLine( to: CGPoint( x: 0,y: strokeHeight ) ) // prior to a fix in iOS 10.2,bug #27989113 "SKShader/SKShapeNode: u_path_length is not set unless shouldUseLocalstrokeBuffers() is true" // add a few points to work around this bug in iOS 10-10.1 -> // for i in 0...15 { // path1.addLine( to: CGPoint( x: 0,y: strokeHeight + CGFloat( 0.001 ) * CGFloat( i ) ) ) // } path1.closeSubpath() let strokeWidth = 17.0 * strokeSizeFactor let path2 = CGMutablePath() path2.move( to: CGPoint.zero ) path2.addLine( to: CGPoint( x: 0,y: strokeHeight ) ) path2.closeSubpath() let BACkgroundShapeNode = SKShapeNode( path: path2 ) BACkgroundShapeNode.lineWidth = strokeWidth BACkgroundShapeNode.zPosition = 5.0 BACkgroundShapeNode.lineCap = .round BACkgroundShapeNode.strokeColor = SKColor.darkGray addChild( BACkgroundShapeNode ) let shapeNode = SKShapeNode( path: path1 ) shapeNode.lineWidth = strokeWidth shapeNode.lineCap = .round BACkgroundShapeNode.addChild( shapeNode ) shapeNode.addChild( cameraNode ) shapeNode.strokeShader = strokeShader BACkgroundShapeNode.calculateAccumulatedFrame() cameraNode.position = CGPoint( x: BACkgroundShapeNode.frame.size.width/2.0,y: BACkgroundShapeNode.frame.size.height/2.0 ) } override func update(_ currentTime: TimeInterval) { // the increment chosen determines how fast the path is stroked. Note this maps to "u_current_percentage" within animatestroke.fsh strokeLengthFloat += 0.01 if strokeLengthFloat > 1.0 { strokeLengthFloat = 0.0 } } }
以上是大佬教程为你收集整理的ios – SpriteKit中的Animate路径绘图全部内容,希望文章能够帮你解决ios – SpriteKit中的Animate路径绘图所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。