Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Swift 3.0 throws an exception if you call DispatchQueue.main.sync when already on the same thread
- func executeOnMain(closure: () -> Void) {
- if Thread.isMainThread {
- closure()
- } else {
- DispatchQueue.main.sync {
- closure()
- }
- }
- }
- fileprivate func spawn() {
- let shape = CAShapeLayer()
- shape.opacity = 0.0
- // create an inital path at the starting position
- shape.path = UIBezierPath(arcCenter: CGPoint.zero, radius: 1, startAngle: 0, endAngle: 360 * (CGFloat.pi / 180), clockwise: true).cgPath
- shape.position = CGPoint.zero
- // create an inital path at the starting position
- shape.path = UIBezierPath(arcCenter: startingPosition, radius: startRadius, startAngle: BubbleConstants.StartingAngle, endAngle: BubbleConstants.EndAngle, clockwise: true).cgPath
- shape.position = startingPosition
- // set the fill color
- shape.fillColor = UIColor.white.cgColor
- layer.addSublayer(shape)
- shape.opacity = Float(opacity)
- executeOnMain {
- self.layer.addSublayer(shape)
- CATransaction.begin()
- }
- let radiusAnimation = CABasicAnimation(keyPath: "path")
- radiusAnimation.fromValue = shape.path
- radiusAnimation.toValue = UIBezierPath(arcCenter: center, radius: endRadius, startAngle: BubbleConstants.StartingAngle, endAngle: BubbleConstants.EndAngle, clockwise: true).cgPath
- executeOnMain { [unowned self] in
- CATransaction.setCompletionBlock { [unowned self] in
- // remove the shape
- self.executeOnMain {
- shape.removeFromSuperlayer()
- shape.removeAllAnimations()
- }
- let qos = DispatchQoS.QoSClass.background
- let backgroundQueue = DispatchQueue.global(qos: qos)
- backgroundQueue.async {
- // spawn a new shape
- self.spawn()
- }
- }
- }
- let movementAnimation = CABasicAnimation(keyPath: "position")
- movementAnimation.fromValue = NSValue(cgPoint: startingPosition)
- movementAnimation.toValue = NSValue(cgPoint: destination)
- let animationGroup = CustomAnimationGroup()
- animationGroup.animations = [radiusAnimation, movementAnimation]
- animationGroup.fillMode = kCAFillModeForwards
- animationGroup.isRemovedOnCompletion = false
- animationGroup.duration = duration
- shape.add(animationGroup, forKey: "bubble_spawn")
- executeOnMain {
- CATransaction.commit()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement