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
UIControlEvents.ValueChanged
never gets fired.Use UIControlEvents.TouchDragInside
instead of UIControlEvents.ValueChanged
(also notice that action method receives two arguments):
myCustomControl.addTarget(self, action: "didDragInsideControl:withEvent:",
forControlEvents: UIControlEvents.TouchDragInside)
The handler function looks like this:
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)"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With