Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class LiveSquishButton: UIView {
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- super.touchesBegan(touches, with: event)
- updateSquish(with: 0.05)
- }
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
- super.touchesMoved(touches, with: event)
- updateSquish(with: touches)
- }
- override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
- super.touchesEnded(touches, with: event)
- updateSquish(with: 0)
- }
- override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
- super.touchesCancelled(touches, with: event)
- updateSquish(with: 0)
- }
- private func updateSquish(with touches: Set<UITouch>) {
- guard let touch = touches.first else {
- return
- }
- let force: CGFloat
- if traitCollection.forceTouchCapability == .available, touch.force > 0 {
- force = touch.force
- } else {
- force = 1 // this occurs both when the touch moves (in which case we should not update the scale) and when using the mouse (in which case we should use a force of 1)
- }
- updateSquish(with: force + 0.05)
- }
- private func updateSquish(with force: CGFloat) {
- let scale = 1 - 0.01 * force
- transform = CGAffineTransform(scaleX: scale, y: scale)
- }
- }
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate {
- var window: UIWindow?
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
- let button = LiveSquishButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
- button.backgroundColor = .red
- button.layer.cornerRadius = 20
- let container = UIView()
- container.backgroundColor = .lightGray
- container.addSubview(button)
- let vc = UIViewController()
- vc.view = container
- let window = UIWindow()
- self.window = window
- window.rootViewController = vc
- window.makeKeyAndVisible()
- return true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement