Skip to content

Instantly share code, notes, and snippets.

@rdougan
Created October 9, 2015 13:47
Show Gist options
  • Save rdougan/db8bc1e76b1d3901b3a9 to your computer and use it in GitHub Desktop.
Save rdougan/db8bc1e76b1d3901b3a9 to your computer and use it in GitHub Desktop.
An example of adding ForceTouch to a UIButton using a new UIControlEvent
//
// ForceTouchButton.swift
//
// Created by Robert Dougan on 09/10/15.
// Copyright © 2015 Robert Dougan. All rights reserved.
//
import UIKit
extension UIControlEvents {
static var ForceTouchDown = UIControlEvents(rawValue: 512)
}
class ForceTouchButton: UIButton {
override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
if traitCollection.forceTouchCapability == .Available {
if (touch.force == touch.maximumPossibleForce) {
self.sendActionsForControlEvents(.ForceTouchDown)
return false
}
}
return super.continueTrackingWithTouch(touch, withEvent: event)
}
}
button.addTarget(self, action: "doAction:", forControlEvents: .ForceTouchDown)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment