Background
I've previously learned how to use a Gesture Recognizer or continueTrackingWithTouch
to get continuous updates of the current touch location and to then use those to do something like this:
Now, however, I would like to learn how to do the same thing using targets. I can already get the touch down and touch up events by using TouchDown
and TouchUpInside
, but I don't know how to get the continuous updates. I assumed that it would be using the ValueChanged
event, but so far that isn't working.
This is what I have tried:
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?
}
Notes
- Changed the code after getting @CaseyWagner's answer. The compiler doesn't throw an error anymore but
UIControlEvents.ValueChanged
never gets fired. - This question is my attempt to have a complete answer for the Method 1: Adding a Target section of this answer.