Swift - NSArray作为参数并在touchesBegan方法中调用LocationManager函数

时间:2015-06-18 05:10:01

标签: ios xcode swift nsarray locationmanager

我想要一种方法,可以在屏幕被按下时找到设备位置的距离,当设备移动时找到新位置。如果再次在不同的位置按下屏幕,我希望方法重新开始并找到设备从该位置移动的距离。由于iOS模拟器无法检测到位置,因此我很难对其进行测试,但我的逻辑看起来是否准确?这是代码:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
        var currentLocation = locations.first as! CLLocation
        var currentAlt = currentLocation.altitude
        var newLocation = locations.last as! CLLocation
        var newAlt = newLocation.altitude
        var lengthInMeters = newLocation.distanceFromLocation(currentLocation) as Double
        var heightInMeters = abs(newAlt - currentAlt)
        var hypotenuse = sqrt((lengthInMeters*lengthInMeters)+(heightInMeters+heightInMeters))
        self.horizontalText.text = "Horizontal: " + "\(lengthInMeters)" + "m"
        self.verticalText.text = "Vertical: " + "\(heightInMeters)" + "m"
        self.totalText.text = "Total: " + "\(hypotenuse)" + "m"
     }

}

我的主要问题是函数中的此函数是否是对locationManager的有效使用。我对iOS / Swift编程比较陌生,而且我还不完全确定每次触摸屏幕时NSArray会重新初始化自己的位置。

1 个答案:

答案 0 :(得分:0)

没有;你的逻辑不准确。你的函数范围是错误的,你对运行循环如何发生的理解是关闭的。

Cocoa框架中的第一个关闭方法名称通常很好地描述了它们的作用,因此在触摸序列开始时调用touchesBegan并在发生位置更新时调用didUpdateLocations

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) 

CLLocationManager的委托方法,因此假设此类已设置为位置管理器的委托,每次发生位置更新时都会调用它。

  1. 你所拥有的是在touchesBegan覆盖中声明的这个方法,虽然它在语法上是合法的,但它永远不会被调用,除非你自己在函数中调用它。
  2. 您了解运行循环的工作原理以及关闭时会发生什么。在didUpdateLocations
  3. 期间,您不会接到touchesBegan的来电

    您需要做的是将它放在类的范围内,并将最新位置记录到类var中,您可以在touchesBegan方法中引用它。

    class FooView:UIView,CLLocationManagerDelegate {
    
        var firstLocation:CLLocation?
        var currentLocation:CLLocation?
        var locationManager = CLLocationManager()
    
        func commonSetup() {
            locationManager.delegate = self
            locationManager.startUpdatingLocation()
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            commonSetup()
        }
    
        required init(coder aDecoder: NSCoder) {
            super.init(coder:aDecoder)
            commonSetup()
        }
    
        func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
            if firstLocation == nil {
                firstLocation = locations.first as? CLLocation
            }
            else {
                currentLocation = locations.first as? CLLocation
            }
    
        }
    
        override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    
            if let currentLocation = self.currentLocation,firstLocation = self.firstLocation {
                var currentAlt = currentLocation.altitude
                var firstAlt = firstLocation.altitude
                var lengthInMeters = currentLocation.distanceFromLocation(firstLocation) as Double
                var heightInMeters = abs(firstAlt - currentAlt)
                var hypotenuse = sqrt((lengthInMeters*lengthInMeters)+(heightInMeters+heightInMeters))
                self.horizontalText.text = "Horizontal: " + "\(lengthInMeters)" + "m"
                self.verticalText.text = "Vertical: " + "\(heightInMeters)" + "m"
                self.totalText.text = "Total: " + "\(hypotenuse)" + "m"
            }
    
    
        }
    
    
    }
    

    看起来您要做的是显示“第一个”位置与触摸视图时的位置之间的差异。

    是的,您可以在模拟器中模拟静态和移动的位置。检查这些菜单。

    在Xcode中

    enter image description here

    在模拟器中

    enter image description here