Swift 将CGFloat值打印到标签时出现问题

Swift 将CGFloat值打印到标签时出现问题,swift,Swift,我现在正在使用UIPanGestureRecognizer,我正在学习教程,我正在尝试将UIView的速度打印到视图中的标签中 但每次我使用UIPangestureRecognitor移动视图时,它都会打印0.0,如下图所示: 这是我没有移动UIView的第一张图片 这是我移动它的第二个图像: 正如您所见,标签在两种情况下都显示相同的消息 以下是我的简单代码: import UIKit class PanViewController: UIViewController { @IBOutlet

我现在正在使用UIPanGestureRecognizer,我正在学习教程,我正在尝试将UIView的速度打印到视图中的标签中

但每次我使用UIPangestureRecognitor移动视图时,它都会打印0.0,如下图所示:

这是我没有移动UIView的第一张图片

这是我移动它的第二个图像:

正如您所见,标签在两种情况下都显示相同的消息

以下是我的简单代码:

import UIKit

class PanViewController: UIViewController {

@IBOutlet weak var testView: UIView!
@IBOutlet weak var horizontalVelocityLabel: UILabel!
@IBOutlet weak var verticalVelocityLabel: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()

    let aSelector : Selector = "moveViewWithGestureRecognizer:"
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: aSelector)

    self.testView.addGestureRecognizer(panGestureRecognizer)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func moveViewWithGestureRecognizer(panGestureRecognizer: UIPanGestureRecognizer){

    var touchLocation : CGPoint = panGestureRecognizer.locationInView(self.view)
    self.testView.center = touchLocation

    var velocity : CGPoint = panGestureRecognizer.velocityInView(self.view)

    // Here is the Problem
    self.horizontalVelocityLabel.text = String(format: "Horizontal Velocity: %.2f points/sec", velocity.x)
    self.verticalVelocityLabel.text = String(format: "Vertical Velocity: %.2f points/sec", velocity.y)
     }
}
我试过了,但没有任何帮助

请告诉我我做错了什么?

您首先需要将CGFloat转换为Float


谢谢你的回复,我以前也尝试过类似的方法,但是这个标签打印得很好,但是UIView没有移动,因为只有文本标签在更改,当我左键单击鼠标时,视图处于该位置。@DharmeshKheni这可能是代码问题,你应该在另一个问题中发布它。但是将CGFloat转换为Float将格式化标签。你应该看看你的代码。如果你遇到任何问题,你可以问me@DharmeshKheni这看起来像是主线程问题。由于panGesture将其移动了很多次,您正在格式化字符串,每次都在更新标签。您可以使用计数器每10次调用更新一次标签。还有其他简单易行的方法吗?
 self.horizontalVelocityLabel.text = String(format: "Horizontal Velocity: %.2f points/sec",Float(velocity.x))
 self.verticalVelocityLabel.text = String(format: "Vertical Velocity: %.2f points/sec",Float(velocity.y))