反常动作的iOS迅速UITouch

问题描述 投票:0回答:1

simulator gif physical device gif

在我的应用程序快捷我做了一个自定义图表,显示股价的历史。单个手指触摸会显示特定股票价格和日期。两个手指触摸显示在整个价格的平均值。

我的问题是,每当我在这同时使用两个手指(选件+鼠标)正常工作模拟器执行我的应用程序。但在执行上的物理设备(iPhone 5S),并使用2个手指两个杆似乎跨越不规律移动并开始闪烁。

 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    handleTouch(touches: touches)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    if delegate != nil{
        delegate!.chartStopped()
    }

}

private func handleTouch(touches:Set<UITouch>){
    if touches.count == 1{
        if let loc = touches.first?.location(in: self){
            let xRawValue = getClosest(searchValue: Int(loc.x), arrayValue: self.xRaw)
            let yRawValue = self.yRaw[self.xRaw.index(of: xRawValue)!]
            self.date = self.x[self.xRaw.index(of: xRawValue)!]
            self.price = self.y[self.xRaw.index(of: xRawValue)!]
            drawLines(xRawValue: xRawValue, yRawValue: yRawValue,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)
            }
        }
    } else if touches.count == 2{
        var touch = touches
        let touchF = touch.popFirst()
        if var loc1 = touchF?.location(in: self), var loc2 = touch.first?.location(in: self){
            if loc1.x > loc2.x{
                swap(&loc1, &loc2)
            }


            let xRawValue1 = getClosest(searchValue: Int(loc1.x), arrayValue: self.xRaw)
            let xIndex1 = self.xRaw.index(of: xRawValue1)!

            let yRawValue1 = self.yRaw[xIndex1]

            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: xRawValue1, yRawValue: yRawValue1,lineLayer: lineLayer,circleLayer: circleLayer)
            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)
}

https://www.youtube.com/watch?v=7RiWXxYh9pk&feature=youtu.be https://www.youtube.com/watch?v=NfSb09NKxH4&feature=youtu.be

ios swift uitouch
1个回答
0
投票

这里是你解决问题的方法之一。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let touch = touch.location(in: self)

        //height --> height of your chart
        let touchArea = SKSpriteNode(color: .clear, size: CGSize(width: 20, height: 200))
        touchArea.position = touch
    }
}

您可以创建在第一触摸被识别,所以应该没有任何问题不再是一个范围。问题是,当你的设备从你的手指识别触摸设备无法准确定位的触摸,因为你是在同一时刻感人几个像素。

苹果通过允许线的位置是只在,你可以在股票应用中看到图表的边缘解决了这个问题。

让我知道它的工作。

© www.soinside.com 2019 - 2024. All rights reserved.