Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import UIKit
  2.  
  3. class LiveSquishButton: UIView {
  4. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  5. super.touchesBegan(touches, with: event)
  6. updateSquish(with: 0.05)
  7. }
  8.  
  9. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  10. super.touchesMoved(touches, with: event)
  11. updateSquish(with: touches)
  12. }
  13.  
  14. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  15. super.touchesEnded(touches, with: event)
  16. updateSquish(with: 0)
  17. }
  18.  
  19. override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
  20. super.touchesCancelled(touches, with: event)
  21. updateSquish(with: 0)
  22. }
  23.  
  24. private func updateSquish(with touches: Set<UITouch>) {
  25. guard let touch = touches.first else {
  26. return
  27. }
  28.  
  29. let force: CGFloat
  30. if traitCollection.forceTouchCapability == .available, touch.force > 0 {
  31. force = touch.force
  32. } else {
  33. 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)
  34. }
  35.  
  36. updateSquish(with: force + 0.05)
  37. }
  38.  
  39. private func updateSquish(with force: CGFloat) {
  40. let scale = 1 - 0.01 * force
  41. transform = CGAffineTransform(scaleX: scale, y: scale)
  42. }
  43. }
  44.  
  45.  
  46. @UIApplicationMain
  47. class AppDelegate: UIResponder, UIApplicationDelegate {
  48.  
  49. var window: UIWindow?
  50.  
  51.  
  52. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  53.  
  54. let button = LiveSquishButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
  55. button.backgroundColor = .red
  56. button.layer.cornerRadius = 20
  57.  
  58. let container = UIView()
  59. container.backgroundColor = .lightGray
  60. container.addSubview(button)
  61. let vc = UIViewController()
  62. vc.view = container
  63.  
  64. let window = UIWindow()
  65. self.window = window
  66. window.rootViewController = vc
  67. window.makeKeyAndVisible()
  68.  
  69. return true
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement