-1

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
nicksarno
  • 3,850
  • 1
  • 13
  • 33
  • This article, demonstrating a custom "circular slider," probably has exactly the information you need -- specifically toward the end where it tracks a touch position that keeps the "indicator" on the edge of the circle: https://www.raywenderlich.com/5294-how-to-make-a-custom-control-tutorial-a-reusable-knob – DonMag Mar 25 '20 at 14:03

1 Answers1

-1

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

Greg de J
  • 320
  • 1
  • 2
  • 8