Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. // Swift 3.0 throws an exception if you call DispatchQueue.main.sync when already on the same thread
  2. func executeOnMain(closure: () -> Void) {
  3. if Thread.isMainThread {
  4. closure()
  5. } else {
  6. DispatchQueue.main.sync {
  7. closure()
  8. }
  9. }
  10. }
  11.  
  12.  
  13.  
  14. fileprivate func spawn() {
  15.  
  16. let shape = CAShapeLayer()
  17. shape.opacity = 0.0
  18.  
  19. // create an inital path at the starting position
  20. shape.path = UIBezierPath(arcCenter: CGPoint.zero, radius: 1, startAngle: 0, endAngle: 360 * (CGFloat.pi / 180), clockwise: true).cgPath
  21. shape.position = CGPoint.zero
  22.  
  23.  
  24. // create an inital path at the starting position
  25. shape.path = UIBezierPath(arcCenter: startingPosition, radius: startRadius, startAngle: BubbleConstants.StartingAngle, endAngle: BubbleConstants.EndAngle, clockwise: true).cgPath
  26. shape.position = startingPosition
  27.  
  28. // set the fill color
  29. shape.fillColor = UIColor.white.cgColor
  30.  
  31. layer.addSublayer(shape)
  32.  
  33. shape.opacity = Float(opacity)
  34.  
  35. executeOnMain {
  36. self.layer.addSublayer(shape)
  37. CATransaction.begin()
  38. }
  39.  
  40. let radiusAnimation = CABasicAnimation(keyPath: "path")
  41. radiusAnimation.fromValue = shape.path
  42. radiusAnimation.toValue = UIBezierPath(arcCenter: center, radius: endRadius, startAngle: BubbleConstants.StartingAngle, endAngle: BubbleConstants.EndAngle, clockwise: true).cgPath
  43.  
  44.  
  45. executeOnMain { [unowned self] in
  46. CATransaction.setCompletionBlock { [unowned self] in
  47.  
  48. // remove the shape
  49. self.executeOnMain {
  50. shape.removeFromSuperlayer()
  51. shape.removeAllAnimations()
  52. }
  53.  
  54. let qos = DispatchQoS.QoSClass.background
  55. let backgroundQueue = DispatchQueue.global(qos: qos)
  56.  
  57. backgroundQueue.async {
  58. // spawn a new shape
  59. self.spawn()
  60. }
  61. }
  62. }
  63.  
  64.  
  65. let movementAnimation = CABasicAnimation(keyPath: "position")
  66. movementAnimation.fromValue = NSValue(cgPoint: startingPosition)
  67. movementAnimation.toValue = NSValue(cgPoint: destination)
  68.  
  69.  
  70. let animationGroup = CustomAnimationGroup()
  71. animationGroup.animations = [radiusAnimation, movementAnimation]
  72. animationGroup.fillMode = kCAFillModeForwards
  73. animationGroup.isRemovedOnCompletion = false
  74. animationGroup.duration = duration
  75.  
  76. shape.add(animationGroup, forKey: "bubble_spawn")
  77.  
  78. executeOnMain {
  79. CATransaction.commit()
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement