가로로 스크롤 할 일종의 타임 라인을 만드는 맞춤 클래스를 만들었습니다. 여기 내 사용자 지정 UIScrollView
에 대한 코드입니다 : 내 장치와 시뮬레이터에내 CoreGraphics 코드가 너무 느리게 작동합니다. 이 사용자 지정 UIScrollView의 성능을 향상 시키려면 어떻게합니까?
import UIKit
struct DataPoint {
var fillColor: UIColor = UIColor.grayColor()
init(color: UIColor) {
fillColor = color
}
}
@IBDesignable
class MyView: UIScrollView {
@IBInspectable var lineColor: UIColor = UIColor.grayColor()
@IBInspectable var lineHeight: CGFloat = 65
@IBInspectable var lineWidth: CGFloat = 15
@IBInspectable var lineGap: CGFloat = 25
@IBInspectable var lineCount: Int = 0
var dataPoints = [DataPoint(color: UIColor.greenColor()), DataPoint(color: UIColor.blueColor())]
override init(frame: CGRect) {
super.init(frame: frame)
setupValues()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupValues()
//fatalError("init(coder:) has not been implemented")
}
func setupValues() {
self.contentSize = CGSize(width: self.frame.width * 2, height: self.frame.height)
self.backgroundColor = UIColor.whiteColor()
self.lineCount = Int(self.frame.width/lineGap)
}
override internal func layoutSubviews() {
super.layoutSubviews()
self.setNeedsDisplay()
self.layoutIfNeeded()
}
override func drawRect(rect: CGRect) {
let ctx = UIGraphicsGetCurrentContext()
CGContextSaveGState(ctx)
for i in 0...lineCount {
let start = CGPoint(x: CGFloat(i) * lineGap, y: self.frame.height)
let end = CGPoint(x: CGFloat(i) * lineGap, y: self.frame.height - lineHeight)
drawLine(from: start, to: end, color: UIColor.grayColor())
if i % (lineCount/(dataPoints.count + 2)) == 0 && i != 0 && i != lineCount {
drawPoint(at: end, radius: 5, color: UIColor.orangeColor())
}
}
CGContextRestoreGState(ctx)
}
func drawLine(from start: CGPoint, to end: CGPoint, color: UIColor) {
let path = UIBezierPath()
path.moveToPoint(start)
path.addLineToPoint(end)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.CGPath
shapeLayer.strokeColor = color.CGColor
shapeLayer.lineWidth = 1
self.layer.addSublayer(shapeLayer)
}
func drawPoint(at center: CGPoint, radius: CGFloat, color: UIColor) {
let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(0), endAngle: CGFloat(M_PI * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.CGPath
shapeLayer.fillColor = color.CGColor
shapeLayer.strokeColor = UIColor.blackColor().CGColor
shapeLayer.lineWidth = 0.5
self.layer.addSublayer(shapeLayer)
}
}
이 매우 느리고 랙이있다. 여기 정확히 내가 뭘 잘못하고 있니? 그리고 스크롤하는 동안 60fps를 달성하려면 어떤 단계를 밟아야합니까?
나중에 애니메이션을 추가하고 스케일을 변경하고 싶습니다. 그래서 내가 이미지 표현을 선택하지 않았습니다. –
이 경우 나는 각 줄과 각 점을 UIView로 만들 것입니다. 그런 다음 스크롤보기에 추가하십시오. 그런 다음 애니메이션 효과를 적용하고 크기를 변경하면 매우 간단 해지며 모든 스크롤을 다시 그리지 않을 것입니다. – Putz1103