Skip to content

Instantly share code, notes, and snippets.

@JohannMG
Created October 17, 2021 22:23
Show Gist options
  • Save JohannMG/36baad43a636073625cbdc08f1d332e8 to your computer and use it in GitHub Desktop.
Save JohannMG/36baad43a636073625cbdc08f1d332e8 to your computer and use it in GitHub Desktop.
Quick test on adding subview to the window. We are making a label that when tapped creates a second dragging label. The dragging label is moveable around the screen. Then when you release the dragging view is removed and the label is updated.
/*
FirtDraggableLabel, October 2021
Quick test on adding subview to the window.
We are making a label that when tapped creates a second dragging label.
The dragging label is moveable around the screen.
Then when you release the dragging view is removed and the label is updated.
This project is also me remembering how UIKit UIWindows work.
As well as adding subviews directly to UIWindows.
UIKit, UIWindow, Popover, Draggable UI, Custom UIControl, iOS
*/
import UIKit
import PlaygroundSupport
// Use on screen
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let dragLabel = DraggableLabel(frame: CGRect(x: 100, y: 200, width: 200, height: 50))
view.addSubview(dragLabel)
self.view = view
}
}
class DraggableLabel: UIControl {
/// First label that displays the label even when not interacting
let label = UILabel()
/// Second label that is popped into the window above all other elements
let windowLabel = UILabel()
/// Value set by interaction with dragging
var sliderValue = 0.0
required init?(coder: NSCoder) {
fatalError("oops")
}
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor(white: 0.1, alpha: 0.3)
label.text = "I am at value \(sliderValue)"
label.backgroundColor = UIColor(white: 0.0, alpha: 0.3)
self.addSubview(label)
windowLabel.text = "windowLabel"
label.backgroundColor = UIColor(white: 0.0, alpha: 0.3)
}
override func layoutSubviews() {
super.layoutSubviews()
label.frame = self.bounds
}
/// On touches begin we find the UIWindow and add the dragging subview to that window
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("tap on")
guard let window = self.window else { return print("no window")}
window.addSubview(windowLabel)
windowLabel.frame = CGRect(x: 5, y: 5, width: 150, height: 50)
}
/// Everytime a touch changes, we update the position of the label and the value so it follows your finger
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touchOne = touches.first else { return }
guard let window = self.window else { return }
let location = touchOne.location(in: window)
windowLabel.frame.origin = location
windowLabel.text = "\(location.y)"
}
/// When touches are remove we clean up by setting the value to the static label and then removing the dragging label
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("tap off")
label.text = "scrolled to \(windowLabel.frame.minY)"
windowLabel.removeFromSuperview()
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment