Render a line graph on Apple Watch using watchOS 2

前端 未结 5 1118
说谎
说谎 2021-02-01 11:13

I am trying to render a line/step graph on Apple Watch using watchOS 2. Unlike iOS 9, watchOS 2 doesn\'t support Quartz. It only supports Core Graphics. I tried writing some cod

5条回答
  •  温柔的废话
    2021-02-01 11:24

    Here's code in watchOS 3

      // Create a graphics context
            let size = CGSize(width:self.contentFrame.size.width, height:100)
    
    
            UIGraphicsBeginImageContext(size)
            let context = UIGraphicsGetCurrentContext()
    
            // Setup for the path appearance
            context!.setStrokeColor(UIColor.white.cgColor)
            context!.setLineWidth(4.0)
    
            // Draw lines
            context!.beginPath ();
    
            context?.move(to: CGPoint())
            context?.addLine(to: CGPoint(x: 100, y: 100))
            context?.addLine(to: CGPoint(x: 0, y: 100))
            context?.addLine(to: CGPoint(x: 100, y: 0))
    
            context!.strokePath();
    
            // Convert to UIImage
            let cgimage = context!.makeImage();
            let uiimage = UIImage(cgImage: cgimage!)
    
            // End the graphics context
            UIGraphicsEndImageContext()
    
            self.graphGroup.setBackgroundImage(uiimage)
    

提交回复
热议问题