gpt4 book ai didi

ios - 如何在 CAShapeLayer 中制作动态 strokeColor?在 swift

转载 作者:行者123 更新时间:2023-11-28 08:50:13 24 4
gpt4 key购买 nike

我使用六个参数(事件、非事件、有线、压缩、免费和总计)。我创建了一个 CAShapeLayer 类,并在其中创建了一个函数,我可以在其中绘制圆圈,只需添加参数即可。我制作了 CGPath 并在绘制圆圈的函数中添加了六个。我将 5 层添加到 UIView。我制作了在一秒钟内更新数据的计时器,但它向 UIView 添加了 5 层。这是错误的方式。我想用数据制作弧线动画并在一秒钟内更新。当层变得太多时,设备工作缓慢。我怎样才能做到?

我在这里写了我的例子。

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)
}

enter image description here

最佳答案

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

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)")
}

关于ios - 如何在 CAShapeLayer 中制作动态 strokeColor?在 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34439865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com