A WIP iOS app for Mastodon and Pleroma.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UIBezierPath+Helpers.swift 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // UIBezierPath+Helpers.swift
  3. // Tusker
  4. //
  5. // Created by Shadowfacts on 6/25/20.
  6. // Copyright © 2020 Shadowfacts. All rights reserved.
  7. //
  8. import UIKit
  9. // TODO: write unit tests for this
  10. extension UIBezierPath {
  11. /// Create a new UIBezierPath that wraps around the given array of rectangles.
  12. /// This is not a convex hull aglorithm. What this does is it takes a set of rectangles
  13. /// and draws a line around the outer borders of the combined shape.
  14. convenience init(wrappingAround rects: [CGRect]) {
  15. precondition(rects.count > 0)
  16. let rects = rects.sorted { $0.minY < $1.minY }
  17. self.init()
  18. // start at the top left corner
  19. self.move(to: CGPoint(x: rects.first!.minX, y: rects.first!.minY))
  20. // walk down the left side
  21. var prevLeft = rects.first!.minX
  22. for rect in rects where !rect.minX.isEqual(to: prevLeft) {
  23. self.addLine(to: CGPoint(x: prevLeft, y: rect.minY))
  24. self.addLine(to: CGPoint(x: rect.minX, y: rect.minY))
  25. prevLeft = rect.minX
  26. }
  27. // ensure at the bottom left if not already
  28. let bottomLeft = CGPoint(x: rects.last!.minX, y: rects.last!.maxY)
  29. if !self.currentPoint.equalTo(bottomLeft) {
  30. self.addLine(to: bottomLeft)
  31. }
  32. // across the bottom of the last rect
  33. self.addLine(to: CGPoint(x: rects.last!.maxX, y: rects.last!.maxY))
  34. // walk up the right side
  35. var prevRight = rects.last!.maxX
  36. for rect in rects.reversed() where !rect.maxX.isEqual(to: prevRight) {
  37. self.addLine(to: CGPoint(x: prevRight, y: rect.maxY))
  38. self.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
  39. prevRight = rect.maxX
  40. }
  41. // ensure at the top right if not already
  42. let topRight = CGPoint(x: rects.first!.maxX, y: rects.first!.minY)
  43. if !self.currentPoint.equalTo(topRight) {
  44. self.addLine(to: topRight)
  45. }
  46. // across the top of the first rect
  47. self.addLine(to: CGPoint(x: rects.first!.minX, y: rects.first!.minY))
  48. }
  49. }