This question already has answers here:
UIColor not working with RGBA values
(6个答案)
3年前关闭。
Xcode 8.1的快速3转换后,我的类(UIView)无法正常工作,我不知道这里出了什么问题,该类是一个进度视图,转换后看起来还不错,但是我的进度不可见,这是转换后的类:
这就是我设定进度的方式:
仍然没有任何进展,任何人都知道这里出了什么问题,然后让我知道
(6个答案)
3年前关闭。
Xcode 8.1的快速3转换后,我的类(UIView)无法正常工作,我不知道这里出了什么问题,该类是一个进度视图,转换后看起来还不错,但是我的进度不可见,这是转换后的类:
class CircularLoaderView: UIView, CAAnimationDelegate {
let circlePathLayer = CAShapeLayer()
let circleRadius: CGFloat = 60.0
let innerCirclePathLayer = CAShapeLayer()
let innerCircleRadius: CGFloat = 60.0
override init(frame: CGRect) {
super.init(frame: frame)
configure()
innerConfigure()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
configure()
innerConfigure()
}
func configure() {
circlePathLayer.frame = bounds
circlePathLayer.lineWidth = 10
circlePathLayer.fillColor = UIColor.clear.cgColor
circlePathLayer.strokeColor = UIColor.darkGray.cgColor
layer.addSublayer(circlePathLayer)
backgroundColor = UIColor.clear
progress = 0
}
func innerConfigure() {
innerCirclePathLayer.frame = bounds
innerCirclePathLayer.lineWidth = 10
innerCirclePathLayer.fillColor = UIColor.clear.cgColor
innerCirclePathLayer.strokeColor = UIColor(red: 100, green: 60, blue: 70, alpha: 0.2).cgColor
layer.addSublayer(innerCirclePathLayer)
backgroundColor = UIColor.clear
}
func innerCircleFrame() -> CGRect {
var circleFrame = CGRect(x: 0, y: 0, width: 2*innerCircleRadius, height: 2*innerCircleRadius)
circleFrame.origin.x = innerCirclePathLayer.bounds.midX - circleFrame.midX
circleFrame.origin.y = innerCirclePathLayer.bounds.midY - circleFrame.midY
return circleFrame
}
func innerCirclePath() -> UIBezierPath {
return UIBezierPath(ovalIn: innerCircleFrame())
}
func circleFrame() -> CGRect {
var circleFrame = CGRect(x: 0, y: 0, width: 2*circleRadius, height: 2*circleRadius)
circleFrame.origin.x = circlePathLayer.bounds.midX - circleFrame.midX
circleFrame.origin.y = circlePathLayer.bounds.midY - circleFrame.midY
return circleFrame
}
func circlePath() -> UIBezierPath {
return UIBezierPath(ovalIn: circleFrame())
}
override func layoutSubviews() {
super.layoutSubviews()
circlePathLayer.frame = bounds
circlePathLayer.path = circlePath().cgPath
innerCirclePathLayer.frame = bounds
innerCirclePathLayer.path = innerCirclePath().cgPath
}
var progress: CGFloat {
get {
return circlePathLayer.strokeEnd
}
set {
if (newValue > 1) {
circlePathLayer.strokeEnd = 1
} else if (newValue < 0) {
circlePathLayer.strokeEnd = 0
} else {
circlePathLayer.strokeEnd = newValue
}
}
}
func reveal() {
// 1
backgroundColor = UIColor.clear
progress = 1
// 2
circlePathLayer.removeAnimation(forKey: "strokeEnd")
// 3
circlePathLayer.removeFromSuperlayer()
superview?.layer.mask = circlePathLayer
// 1
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let finalRadius = sqrt((center.x*center.x) + (center.y*center.y))
let radiusInset = finalRadius - circleRadius
let outerRect = circleFrame().insetBy(dx: -radiusInset, dy: -radiusInset)
let toPath = UIBezierPath(ovalIn: outerRect).cgPath
// 2
let fromPath = circlePathLayer.path
let fromLineWidth = circlePathLayer.lineWidth
// 3
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
circlePathLayer.lineWidth = 2*finalRadius
circlePathLayer.path = toPath
CATransaction.commit()
// 4
let lineWidthAnimation = CABasicAnimation(keyPath: "lineWidth")
lineWidthAnimation.fromValue = fromLineWidth
lineWidthAnimation.toValue = 2*finalRadius
let pathAnimation = CABasicAnimation(keyPath: "path")
pathAnimation.fromValue = fromPath
pathAnimation.toValue = toPath
// 5
let groupAnimation = CAAnimationGroup()
groupAnimation.duration = 1
groupAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
groupAnimation.animations = [pathAnimation, lineWidthAnimation]
groupAnimation.delegate = self
circlePathLayer.add(groupAnimation, forKey: "strokeWidth")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
superview?.layer.mask = nil
}
}
这就是我设定进度的方式:
cell.loaderView?.progress = CGFloat(receivedSize)/CGFloat(expectedSize)
仍然没有任何进展,任何人都知道这里出了什么问题,然后让我知道
最佳答案
您尚未指定完全不起作用的内容,但是当我测试您的代码以显示进度时,唯一的事情是不起作用,因为它没有向我显示innerCirclePathLayer
,因为您没有将RGB
颜色除以255
,因为init(red:green:blue:alpha:)
接受0.0
到1.0
之间的值,并且1.0
以上的值被解释为1.0
。因此,请尝试一次将RGB划分为255
。
innerCirclePathLayer.strokeColor = UIColor(red: 100/255.0, green: 60/255.0, blue: 70/255.0, alpha: 0.2).cgColor
关于ios - 快速3转换后,类无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40689465/
10-14 20:06