2016-01-13 70 views
9

背景如何獲得觸摸從ValueChanged事件斯威夫特座標

我已經previously learned如何使用手勢識別或continueTrackingWithTouch來獲取當前觸摸位置的不斷更新和再使用那些做是這樣的:

enter image description here

但是現在,我想學習如何使用目標,以做同樣的事情。我已經可以通過使用TouchDownTouchUpInside來觸摸並修改事件,但我不知道如何獲得持續更新。我認爲它將使用ValueChanged事件,但目前爲止無法使用。

這是我曾嘗試:

ViewController.swift

import UIKit 
class ViewController: UIViewController { 

    @IBOutlet weak var myCustomControl: MyCustomControl! 
    @IBOutlet weak var trackingBeganLabel: UILabel! 
    @IBOutlet weak var trackingEndedLabel: UILabel! 
    @IBOutlet weak var xLabel: UILabel! 
    @IBOutlet weak var yLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // these work 
     myCustomControl.addTarget(self, action: "myTouchDown", forControlEvents: UIControlEvents.TouchDown) 
     myCustomControl.addTarget(self, action: "myTouchUpInside", forControlEvents: UIControlEvents.TouchUpInside) 

     // this doesn't work 
     myCustomControl.addTarget(self, action: "myValueChangedEvent:", 
      forControlEvents: UIControlEvents.ValueChanged) 
    } 

    // this works 
    func myTouchDown() { 
     trackingBeganLabel.text = "Touched down" 
    } 

    // this works 
    func myTouchUpInside() { 
     trackingEndedLabel.text = "Touch up inside" 
    } 

    // this doesn't work, function never gets called 
    func myValueChangedEvent(sender: UIControl) { 
     let location = sender.convertPoint(CGPointZero, toView: myCustomControl) 
     xLabel.text = "x: \(location.x)" 
     yLabel.text = "y: \(location.y)" 
    } 
} 

MyCustomControl.swift

import UIKit 
class MyCustomControl: UIControl { 
    // currently empty. Do I need to add something here? 
} 

注意

  • 獲得@ CaseyWagner的答案後更改了代碼。編譯器不會再拋出錯誤,但不會被解僱。
  • 這個問題是我試圖對方法1:添加一個目標部分this answer的完整答案。

回答

9

使用UIControlEvents.TouchDragInside而不是UIControlEvents.ValueChanged(也注意到,行動方法接收兩個參數):

myCustomControl.addTarget(self, action: "didDragInsideControl:withEvent:", 
     forControlEvents: UIControlEvents.TouchDragInside) 

處理函數如下所示:

func didDragInsideControl(control: MyCustomControl, withEvent event: UIEvent) { 
    let touch = event.touchesForView(control)!.first! 
    let location = touch.locationInView(control) 
    xLabel.text = "x: \(location.x)" 
    yLabel.text = "y: \(location.y)" 
} 
+0

太棒了!我錯過了'TouchDragInside'事件。我用'if let touch = ...',因爲我不確定強制解包是否會導致崩潰。 – Suragch

1

沒有看到你的代碼的其餘部分,它看起來像你可能需要:

let location = sender.convertPoint(CGPointZero, toView: myCustomControl) 
+0

我更新了我的問題。 – Suragch

1
  1. 評論@UIApplicationMainAppDelegate
  2. 創建名爲主要的新文件。迅速,在該方法 進口的UIKit

    class AppClass: UIApplication { 
        override func sendEvent(event: UIEvent) 
        { 
         super.sendEvent(event) 
         print("send event") // this is an example 
         // ... dispatch the message... 
        } 
    } 
    
  3. 寫下面的代碼文件

    import UIKit 
    import Foundation 
    UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(AppClass), NSStringFromClass(AppDelegate)) 
    
  4. 名稱爲 「AppClass」 創建新類的UIApplication的子類中添加以下代碼在這裏,在sendEvent方法中,您將獲得應用程序(所有屏幕)中發生的所有事件,所有觸摸事件,因此您可以在此處使用此UIEvent並執行任何任務(如果需要)