我试图实现在iPhone(iOS的12)中的股票应用中的“图表”的简单版本。特别是两个手指手势它用来找到价格的变化。
我用UITouch及其方法TouchesMoved,但线被不断闪烁。
是否有实现该相同未来无闪烁线的任何其它方法。
代码:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.lineLayer.path = UIBezierPath().cgPath
self.lineLayerS.path = UIBezierPath().cgPath
self.circleLayer.path = UIBezierPath().cgPath
self.circleLayerS.path = UIBezierPath().cgPath
var touch = touches
if touches.count == 1{
handleTouch(firstTouch: touch.popFirst()!, secondTouch: nil)
} else if touches.count == 2{
var tempF = touch.popFirst()
var tempS = touch.popFirst()
let tempFX = Int((tempF?.location(in: self).x)!)
let tempSX = Int((tempS?.location(in: self).x)!)
print(String(tempFX)+" "+String(tempSX))
if tempFX > tempSX{
swap(&tempF, &tempS)
}
if !((tempFX-Int((tempF?.majorRadius)!)...tempFX+Int((tempF?.majorRadius)!)~=tempSX)&&(tempSX-Int((tempS?.majorRadius)!)...tempSX+Int((tempS?.majorRadius)!)~=tempFX)){
handleTouch(firstTouch: tempF!, secondTouch: tempS!)
}
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if delegate != nil{
delegate!.chartStopped()
}
self.lineLayer.path = UIBezierPath().cgPath
self.lineLayerS.path = UIBezierPath().cgPath
self.circleLayer.path = UIBezierPath().cgPath
self.circleLayerS.path = UIBezierPath().cgPath
}
private func handleTouch(firstTouch fT:UITouch, secondTouch sT:UITouch?){
let loc1 = fT.location(in: self)
let xRawValue1 = getClosest(searchValue: Int(loc1.x), arrayValue: self.xRaw)
let yRawValue1 = self.yRaw[self.xRaw.index(of: xRawValue1)!]
self.date = self.x[self.xRaw.index(of: xRawValue1)!]
self.price = self.y[self.xRaw.index(of: xRawValue1)!]
drawLines(xRawValue: xRawValue1, yRawValue: yRawValue1,lineLayer: lineLayer,circleLayer: circleLayer)
//self.lineLayerS.path = UIBezierPath().cgPath
//self.circleLayerS.path = UIBezierPath().cgPath
if delegate != nil{
delegate!.chartMoved(currentPrice: self.price, currentDate: self.date)
}
if let locT = sT{
let loc2 = locT.location(in: self)
let xIndex1 = self.xRaw.index(of: xRawValue1)!
let xRawValue2 = getClosest(searchValue: Int(loc2.x), arrayValue: self.xRaw)
let xIndex2 = self.xRaw.index(of: xRawValue2)!
let yRawValue2 = self.yRaw[xIndex2]
var averageSum = 0.0
for i in xIndex1..<xIndex2{
averageSum += Double(self.y[i])
}
averageSum = averageSum/Double(xIndex2-xIndex1)
self.date = self.x[xIndex1]+"-"+self.x[xIndex2]
self.price = Float(averageSum)
if delegate != nil{
delegate!.chartMoved(currentPrice: self.price, currentDate: self.date)
}
drawLines(xRawValue: xRawValue2, yRawValue: yRawValue2,lineLayer: lineLayerS,circleLayer: circleLayerS)
}
}
private func drawLines(xRawValue xV:Int,yRawValue yV:Int,lineLayer:CAShapeLayer,circleLayer:CAShapeLayer){
let line = UIBezierPath(rect: CGRect(x: xV, y:0, width: 1, height: Int(self.frame.height)))
lineLayer.path = line.cgPath
lineLayer.strokeColor = UIColor.black.cgColor
circleLayer.path = UIBezierPath(ovalIn: CGRect(x: xV-4, y: yV-4, width: 8, height: 8)).cgPath;
circleLayer.strokeColor = Colors.blueDark.cgColor
circleLayer.fillColor = Colors.blueDark.cgColor
self.layer.addSublayer(circleLayer)
}