Swift: Constrain object to and point within a circular NSLayoutConstraint? Circle anchor?

53 Views Asked by At

Within a view controller, I have a UIView (backgroundCircle) and a UILabel. The UILabel is moving based on motionManager.startAccelerometerUpdates data. Using this data, I am animating the label.center point. However, I want to bound the movement so that it only moves within a circular area around its starting point (as if it was on a leash from the starting point). Is this possible? Sample code below if it helps.

            //objects called during set up
            let backgroundCircle = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
            backgroundCircle.layer.cornerRadius = circle.frame.width / 2
            backgroundCircle.backgroundColor = .clear

            let label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
            label.text = "Hello"


            //this is called in a separate function, continuously
             label.center = newCenter
1

There are 1 best solutions below

0
Greg de J On

I'd try using UIKit Dynamics, especially UICollisionBehavior which allows to define collision boundaries with bezier paths.