如何在CAShapeLayer中制作动态strokeColor?在斯威夫特 [英] how to make dynamical strokeColor in CAShapeLayer? In Swift

查看:116
本文介绍了如何在CAShapeLayer中制作动态strokeColor?在斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用六个参数(有效,无效,有线,压缩,空闲和总计)。我制作了一个 CAShapeLayer 的类,并在其中做了一个函数,可以在添加参数的情况下画圆。我制作 CGPath 并在函数中添加六个,绘制一个圆。我将5层放入 UIView 。我使计时器在一秒钟内更新数据,但它将其他5层添加到 UIView 中。这是错误的方式。我想用数据为弧动画并在一秒钟内更新。当图层过多时,设备将缓慢运行。我怎么做呢?

I use six parameters (as active, inactive, wired, compressed, free and total). I make a class of CAShapeLayer and I made one function in there with help I can draw circle just add parameters. I make CGPath and add it six once in function which draw a circle. I get the 5 layers to UIView. I make timer which update data in one second but it add else 5 layer to UIView. It is wrong way. I want to animate arcs with data and update in one second. when layers become too much device is working slowly. How can I make it?

我在这里写了我的例子。

I wrote here my example.

public class Arc {

public init() { }

public func addFigure(path: CGPath, fillColor: UIColor, strokeColor: UIColor, strokeStart: CGFloat, strokeEnd: CGFloat, lineWidth: CGFloat, miterLimit: CGFloat, lineDashPhase: CGFloat, layer: CALayer) -> CAShapeLayer {
    let shape = CAShapeLayer()
    shape.path = path
    shape.fillColor = fillColor.CGColor
    shape.strokeColor = strokeColor.CGColor
    shape.strokeStart = strokeStart
    shape.strokeEnd = strokeEnd
    shape.lineWidth = lineWidth
    shape.miterLimit = miterLimit
    shape.lineDashPhase = lineDashPhase
    return shape
   }
}

UIView绘制圆

    import UIKit
import DrawKit

class MemoryMainView: UIView {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    // MARK: - var and let
    let arc = Arc()
    let notificationCenter = NSNotificationCenter.defaultCenter()
    var boolForMemoryArc = false
    // for ARC
    var memoryArcRadius: CGFloat!
    var lineWidht: CGFloat!
    var path: UIBezierPath!

    var checkDevice = false

    // MARK: - colors
    @IBInspectable var blackColor: UIColor = .blackColor()
    @IBInspectable var redColor: UIColor = .redColor()
    @IBInspectable var cyanColor: UIColor = .cyanColor()
    @IBInspectable var blueColor: UIColor = .blueColor()
    @IBInspectable var grapeColor: UIColor!
    @IBInspectable var greenColor: UIColor = .greenColor()

    override func drawRect(rect: CGRect) {
        if boolForMemoryArc == false {
            drawMemoryArc()
            boolForMemoryArc = true
        }
        notificationCenter.addObserver(self, selector: "turnOnMemoryTimer", name: "turnOnMemoryArc", object: nil)
    }

    func drawMemoryArc() {
        if checkDevice == false {
            let device = CheckDevice().returnResult()
            memoryArcRadius = device.radiusArc
            lineWidht = device.lineWidth
            path = UIBezierPath(arcCenter: CGPoint(x: frame.size.width/2, y: frame.size.height/2), radius: memoryArcRadius, startAngle: CGFloat(-M_PI_2), endAngle: CGFloat(M_PI*2 - M_PI_2), clockwise: true)
            checkDevice = true
        }

        let memory = showMemory()

        // active cyanColor
        arc.addFigure(path.CGPath, fillColor: blackColor, strokeColor: cyanColor, strokeStart: 0.0, strokeEnd: CGFloat(percentageMemory(memory.active)), lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)

        // inactive blue
        arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: blueColor, strokeStart: CGFloat(percentageMemory(memory.active)), strokeEnd: CGFloat(percentageMemory(memory.active + memory.inactive)), lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)

        // wired redColor
        arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: redColor, strokeStart: CGFloat(percentageMemory(memory.active + memory.inactive)), strokeEnd: CGFloat(percentageMemory(memory.active + memory.inactive + memory.wired)), lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)

        // compressed yellow
        arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: grapeColor, strokeStart: CGFloat(percentageMemory(memory.active + memory.inactive + memory.wired)), strokeEnd: CGFloat(percentageMemory(memory.active + memory.inactive + memory.wired + memory.compressed)), lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)

        // free green
        arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: greenColor, strokeStart: CGFloat(percentageMemory(memory.active + memory.inactive + memory.wired + memory.compressed)), strokeEnd: 1.0, lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)
        print("memory layers \(self.layer.sublayers?.count)")
    }

    func turnOnMemoryTimer() {
        GlobalTimer.sharedInstance.viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawMemoryArc", userInfo: nil, repeats: true)
    }

}

推荐答案

我更改了代码,它对我有用。

I changed code and it works for me.

func drawMemoryArc() {
    if checkDevice == false {
        let device = CheckDevice().returnResult()
        memoryArcRadius = device.radiusArc
        lineWidht = device.lineWidth
        path = UIBezierPath(arcCenter: CGPoint(x: frame.size.width/2, y: frame.size.height/2), radius: memoryArcRadius, startAngle: CGFloat(-M_PI_2), endAngle: CGFloat(M_PI*2 - M_PI_2), clockwise: true)
        checkDevice = true
    }

    let memory = showMemory()

    layer.sublayers?.removeAll()

    // active cyanColor
    arc.addFigure(path.CGPath, fillColor: blackColor, strokeColor: cyanColor, strokeStart: 0.0, strokeEnd: CGFloat(percentageMemory(memory.active)), lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)

    // inactive blue
    arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: blueColor, strokeStart: CGFloat(percentageMemory(memory.active)), strokeEnd: CGFloat(percentageMemory(memory.active + memory.inactive)), lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)

    // wired redColor
    arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: redColor, strokeStart: CGFloat(percentageMemory(memory.active + memory.inactive)), strokeEnd: CGFloat(percentageMemory(memory.active + memory.inactive + memory.wired)), lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)

    // compressed yellow
    arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: grapeColor, strokeStart: CGFloat(percentageMemory(memory.active + memory.inactive + memory.wired)), strokeEnd: CGFloat(percentageMemory(memory.active + memory.inactive + memory.wired + memory.compressed)), lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)

    // free green
    arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: greenColor, strokeStart: CGFloat(percentageMemory(memory.active + memory.inactive + memory.wired + memory.compressed)), strokeEnd: 1.0, lineWidth: lineWidht, miterLimit: 0.0, lineDashPhase: 0.0, layer: self.layer)
    print("memory layers \(self.layer.sublayers?.count)")
}

这篇关于如何在CAShapeLayer中制作动态strokeColor?在斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆