Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4. let baseLayer = CAShapeLayer()
  5.  
  6. var pinchX: CGFloat = 0
  7. var pinchY: CGFloat = 0
  8.  
  9. var panStart = CGPoint()
  10.  
  11. var viewPosition: CGPoint = CGPoint(x:0, y:0) {
  12. didSet {
  13. moveToCurrentViewPoint()
  14. }
  15. }
  16.  
  17. private var chartScaleState: CGFloat = 1
  18. private var chartScale: CGFloat {
  19. get {
  20. return chartScaleState
  21. }
  22. set(val) {
  23. let previousScale = chartScaleState
  24. chartScaleState = (val) + chartScaleState
  25. handleScaleAction(scaleAdjust: previousScale / chartScaleState)
  26. }
  27. }
  28.  
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31.  
  32. drawBaseMap()
  33. viewPosition = CGPoint(x: 0, y: 0)
  34. }
  35.  
  36. @IBAction func handlePinch(_ sender: UIPinchGestureRecognizer) {
  37. if (sender.state == .changed) {
  38. let pinchCenter = CGPoint(x: sender.location(in: view).x - view.bounds.midX,
  39. y: sender.location(in: view).y - view.bounds.midY)
  40.  
  41. pinchX = pinchCenter.x + (baseLayer.anchorPoint.x * view.bounds.width)
  42. pinchY = pinchCenter.y + (baseLayer.anchorPoint.y * view.bounds.height)
  43. chartScale = -((sender.scale - 1) * chartScale)
  44.  
  45. sender.scale = 1.0
  46. }
  47. }
  48.  
  49. @IBAction func panHandle(_ sender: UIPanGestureRecognizer) {
  50. let location = sender.location(in: self.view)
  51.  
  52. if (sender.state == .began) {
  53. panStart = CGPoint(x: location.x, y: location.y)
  54. }
  55.  
  56. if (sender.state == .changed) {
  57. let deltaX = panStart.x - location.x
  58. let deltaY = panStart.y - location.y
  59. moveItems(deltaX: deltaX, deltaY: deltaY)
  60. panStart = CGPoint(x: location.x, y: location.y)
  61. }
  62. }
  63.  
  64. func moveToCurrentViewPoint() {
  65. baseLayer.anchorPoint = CGPoint(
  66. x: CGFloat(Double(viewPosition.x) / Double(chartScale)) / baseLayer.bounds.width,
  67. y: (CGFloat(Double(viewPosition.y) / Double(chartScale)) / baseLayer.bounds.height) * -1
  68. )
  69. }
  70.  
  71. func drawBaseMap() {
  72. baseLayer.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
  73. baseLayer.position = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
  74.  
  75. baseLayer.anchorPoint = CGPoint(
  76. x: 0,
  77. y: 0
  78. )
  79.  
  80. baseLayer.actions = [
  81. "transform": NSNull(),
  82. "anchorPoint": NSNull() ]
  83.  
  84. baseLayer.backgroundColor = UIColor.green.cgColor
  85. baseLayer.strokeColor = UIColor.black.cgColor
  86. baseLayer.fillColor = UIColor(red: 184/255.0, green: 174/255.0, blue: 87/255.0, alpha: 1.0).cgColor
  87. baseLayer.lineWidth = 1
  88. baseLayer.zPosition = -10
  89.  
  90. let coolRect = CGRect(x: 0, y: 0, width: 100, height: 100)
  91. let path = CGMutablePath()
  92. path.addRect(coolRect)
  93. self.view.layer.addSublayer(baseLayer)
  94. baseLayer.path = path
  95. }
  96.  
  97. func handleScaleAction(scaleAdjust: CGFloat) {
  98. var pathTransform = CGAffineTransform.identity
  99. pathTransform = pathTransform.translatedBy(x: pinchX, y: pinchY)
  100. pathTransform = pathTransform.scaledBy(x: scaleAdjust, y: scaleAdjust)
  101. pathTransform = pathTransform.translatedBy(x: -(pinchX), y: -(pinchY))
  102.  
  103. baseLayer.path = baseLayer.path?.copy(using: &pathTransform)
  104. }
  105.  
  106. public func moveItems(deltaX: CGFloat, deltaY: CGFloat) {
  107. viewPosition = CGPoint(
  108. x: Double(viewPosition.y) - (Double(deltaY) * Double(chartScale)),
  109. y: Double(viewPosition.x) + (Double(deltaX) * Double(chartScale))
  110. )
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement