Skip to content

Instantly share code, notes, and snippets.

@giomurru
Last active June 1, 2022 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giomurru/16af06f3464f0901ed1d8d3136486c76 to your computer and use it in GitHub Desktop.
Save giomurru/16af06f3464f0901ed1d8d3136486c76 to your computer and use it in GitHub Desktop.
A UIButton for tvOS that continuously sends events when pressed
//
// PressButton.swift
//
// Created by Giovanni Murru on 31/05/22.
// Copyright © 2022 Giovanni Murru.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
protocol PressButtonDelegate : AnyObject {
func didStartPressing(_ sender: PressButton)
func isPressing(_ sender: PressButton)
func didStopPressing(_ sender: PressButton)
func didCancelPressing(_ sender: PressButton)
}
//Makes all the protocol functions optional
extension PressButtonDelegate {
func didStartPressing(_ sender: PressButton) {}
func isPressing(_ sender: PressButton) {}
func didStopPressing(_ sender: PressButton) {}
func didCancelPressing(_ sender: PressButton) {}
}
class PressButton: UIButton {
weak var delegate : PressButtonDelegate!
var timer: Timer!
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
super.pressesBegan(presses, with: event)
guard let press = presses.first else {
super.pressesBegan(presses, with: event)
return
}
switch press.type {
case .select:
timer = Timer.scheduledTimer(timeInterval: 1.0/60.0, target: self, selector: #selector(userIsPressing), userInfo: nil, repeats: true)
delegate?.didStartPressing(self)
default:
super.pressesBegan(presses, with: event)
break
}
}
override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
super.pressesEnded(presses, with: event)
guard let press = presses.first else {
super.pressesEnded(presses, with: event)
return
}
switch press.type {
case .select:
timer.invalidate()
delegate?.didCancelPressing(self)
default:
super.pressesEnded(presses, with: event)
break
}
}
override func pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
super.pressesCancelled(presses, with: event)
guard let press = presses.first else {
super.pressesCancelled(presses, with: event)
return
}
switch press.type {
case .select:
timer.invalidate()
delegate?.didStopPressing(self)
default:
super.pressesCancelled(presses, with: event)
break
}
}
@objc func userIsPressing() {
delegate?.isPressing(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment