ios - iOS swift UITouch 移动不稳定

标签 ios swift uitouch

simulator gif
physical device gif

在我的 swift 应用程序中,我制作了一个自定义图表来显示股票价格的历史记录。只需手指触摸即可显示特定的股票价格和日期。两根手指触摸将显示价格的平均值。

我的问题是,每当我在模拟器中执行我的应用程序时,它在使用两根手指(选项+鼠标)时工作正常。但是在物理设备(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 <- 物理设备屏幕录制

最佳答案

这是解决问题的一种方法。

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

您可以创建一个范围来识别第一次触摸,这样就不会再出现问题了。问题是,当您的设备识别出手指的触摸时,设备无法准确定位触摸,因为您同时触摸了多个像素。

苹果通过允许线条的位置仅位于图表的边缘来解决这个问题,正如您在股票应用程序上看到的那样。

让我知道它是否有效。

关于ios - iOS swift UITouch 移动不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54500068/

相关文章:

ios - 使用 Swift 在键盘上启动一个带有搜索按钮的新 View Controller

ios - 取消按钮 Action 上的手势识别器

iphone - UIControl 未接收到触摸

ios - 你如何知道在 touchesBegan 中被触摸的是什么对象?

ios - 替代 iOS 9 的 UIAlertView?

iphone - 如何设置用于 NSDictionary 的键值对?

ios - 在 iOS 中切换 UIViewControllers 不起作用

ios - 如何在 iOS-Charts 中显示重新加载文本?

ios - 从通用链接打开 iOS 应用程序

ios - 或者在 whereField 中从 Firestore 中获取文档