Guest User

Untitled

a guest
Jul 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. class ResizableView: UIView {
  2.  
  3. enum Edge {
  4. case topLeft, topRight, bottomLeft, bottomRight, none
  5. }
  6.  
  7. static var edgeSize: CGFloat = 44.0
  8. private typealias `Self` = ResizableView
  9.  
  10. var currentEdge: Edge = .none
  11. var touchStart = CGPoint.zero
  12.  
  13. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  14. if let touch = touches.first {
  15.  
  16. touchStart = touch.location(in: self)
  17.  
  18. currentEdge = {
  19. if self.bounds.size.width - touchStart.x < Self.edgeSize && self.bounds.size.height - touchStart.y < Self.edgeSize {
  20. return .bottomRight
  21. } else if touchStart.x < Self.edgeSize && touchStart.y < Self.edgeSize {
  22. return .topLeft
  23. } else if self.bounds.size.width - touchStart.x < Self.edgeSize && touchStart.y < Self.edgeSize {
  24. return .topRight
  25. } else if touchStart.x < Self.edgeSize && self.bounds.size.height - touchStart.y < Self.edgeSize {
  26. return .bottomLeft
  27. }
  28. return .none
  29. }()
  30. }
  31. }
  32.  
  33. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  34. if let touch = touches.first {
  35. let currentPoint = touch.location(in: self)
  36. let previous = touch.previousLocation(in: self)
  37.  
  38. let originX = self.frame.origin.x
  39. let originY = self.frame.origin.y
  40. let width = self.frame.size.width
  41. let height = self.frame.size.height
  42.  
  43. let deltaWidth = currentPoint.x - previous.x
  44. let deltaHeight = currentPoint.y - previous.y
  45.  
  46. switch currentEdge {
  47. case .topLeft:
  48. self.frame = CGRect(x: originX + deltaWidth, y: originY + deltaHeight, width: width - deltaWidth, height: height - deltaHeight)
  49. case .topRight:
  50. self.frame = CGRect(x: originX, y: originY + deltaHeight, width: width + deltaWidth, height: height - deltaHeight)
  51. case .bottomRight:
  52. self.frame = CGRect(x: originX, y: originY, width: currentPoint.x + deltaWidth, height: currentPoint.y + deltaWidth)
  53. case .bottomLeft:
  54. self.frame = CGRect(x: originX + deltaWidth, y: originY, width: width - deltaWidth, height: height + deltaHeight)
  55. default:
  56. // Moving
  57. self.center = CGPoint(x: self.center.x + currentPoint.x - touchStart.x,
  58. y: self.center.y + currentPoint.y - touchStart.y)
  59. }
  60. }
  61. }
  62.  
  63. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  64. currentEdge = .none
  65. }
  66.  
  67. // MARK: - Border
  68.  
  69. @IBInspectable public var borderColor: UIColor = UIColor.clear {
  70. didSet {
  71. layer.borderColor = borderColor.cgColor
  72. }
  73. }
  74.  
  75. @IBInspectable public var borderWidth: CGFloat = 0 {
  76. didSet {
  77. layer.borderWidth = borderWidth
  78. }
  79. }
Add Comment
Please, Sign In to add comment