Swift – Circular button bubbles with gravity

Today I want to show how to use the GravitySDK 🚀 (as discussed in the previous post) to make an animated bubbles buttons that falling around with gravity, like this:

Let’s start!

First of all, add as Swift Package the “Gravity” SDK 😎, importing the package:


And imports in your project the newest SDK:

import GravitySPM


Create a new BubbleView class that extends from UIView.

In order to match exactly the circle view borders you should use a CAShapeLayer instead of a rounded view.

The difference is that using UIView with corner radius you have the view borders that collide with other views:


class BubbleView: UIView {

    var shapeLayer: CAShapeLayer = {
        let _shapeLayer = CAShapeLayer()
        _shapeLayer.fillColor = UIColor.clear.cgColor
        _shapeLayer.allowsEdgeAntialiasing = true
        _shapeLayer.backgroundColor = UIColor.clear.cgColor
        return _shapeLayer

    override func layoutSubviews() {

        self.layer.cornerRadius = self.bounds.width / 2
        self.backgroundColor = .white

        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        shapeLayer.path = circularPath(lineWidth: 0, center: center).cgPath

    private func circularPath(lineWidth: CGFloat = 0, center: CGPoint = .zero) -> UIBezierPath {
        let radius = (min(bounds.width, bounds.height) - lineWidth) / 2
        return UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: .pi * 2, clockwise: true)


This class create a UIView with a circular CAShapeLayer.

This is not enough because the collision type of this class must be setted to “path“, overriding the collisionBoundsType 🚧 and the collisionBoundingPath:

override var collisionBoundsType: UIDynamicItemCollisionBoundsType {
    return .path

override var collisionBoundingPath: UIBezierPath {
    return circularPath()

In this way the collision respect exactly the circle borders!

Create the Bubbles 🎾🏀!

Now go into your ViewController and prepare the UI:

import GravitySPM

class ViewController: UIViewController {

    var circles: [BubbleView] = []
    var gravity: Gravity?
    var gravityItems: [UIDynamicItem] = []

    override func viewDidLoad() {

        // create random BubbleView in random position
        for idx in 0..<6 {
            let randX = Int.random(in: 100..<Int(self.view.frame.size.width - 100))
            let randY = Int.random(in: 100..<Int(self.view.frame.size.height - 100))

            let bubble = BubbleView(
                frame: CGRect(x: randX, y: randY, width: 145, height: 145),
                parentBound: self.view.bounds,
                size: 145,
                expansionDelta: 30,
                padding: 30

            bubble.delegate = self
            bubble.tag = idx.offset

        // prepare the bubbles to pass to SDK
        gravityItems = self.view.subviews.filter{ $0 is BubbleView }

        gravity = Gravity(
            gravityItems: gravityItems, // <<-- your bubbles
            collisionItems: nil,
            referenceView: self.view,
            boundary: UIBezierPath(rect: self.view.frame),
            queue: nil)

        // start gravity


You’re done!

Build and run and you should see the bubbles falling around your main view!


Remember to lock the orientation of your app…

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait


If you want to make the bubbles touchable and scaled when touched, add into the BubbleView class the touched protocol:

protocol BubbleViewDelegate {
    func didViewTouched( view: BubbleView )

and implement the touchesEnded delegate:

var delegate: BubbleViewDelegate?

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    shapeLayer.fillColor = selected ? UIColor.red.cgColor : UIColor.white.cgColor
    self.backgroundColor = selected ? UIColor.clear : UIColor.white

    if selected {
        self.bounds.size.width = defaultSize! + expansionSize!
        self.bounds.size.height = self.bounds.size.width
    } else {
        self.bounds.size.width = defaultSize!
        self.bounds.size.height = self.bounds.size.width

    self.delegate?.didViewTouched(view: self)

Next, in your ViewController implement the protocol:

extension ViewController: BubbleViewDelegate {

    func didViewTouched(view: BubbleView) {
        let viewIndex = gravityItems.firstIndex { item in
            if let vitem = item as? BubbleView {
                return vitem.tag == view.tag
            return false

        if viewIndex != NSNotFound {
            self.gravityItems.remove(at: viewIndex!)

        // remember to restart the gravity animator
        // in order to re-layout all views with borders.
        // Of course, only if the size change else is useless...

Final result 🎥:

Enjoy and share the bubbles! 🎉🎉🎉


Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.