【问题标题】:Swift: Constrain object to and point within a circular NSLayoutConstraint? Circle anchor?Swift:将对象约束到并指向圆形 NSLayoutConstraint?圆形锚?
【发布时间】:2023-03-19 04:07:01
【问题描述】:

在视图控制器中,我有一个 UIView (backgroundCircle) 和一个 UILabel。 UILabel 基于 motionManager.startAccelerometerUpdates 数据移动。使用这些数据,我正在为 label.center 点设置动画。但是,我想限制运动,使其仅在其起点周围的圆形区域内移动(就好像它从起点开始系在皮带上一样)。这可能吗?如果有帮助,下面的示例代码。

            //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

【问题讨论】:

  • 这篇文章展示了一个自定义的“圆形滑块”,可能正好包含您需要的信息——特别是在它跟踪将“指示器”保持在圆圈边缘的触摸位置的末尾:raywenderlich.com/…

标签: swift xcode autolayout nslayoutconstraint cmmotionmanager


【解决方案1】:

我会尝试使用UIKit Dynamics,尤其是UICollisionBehavior,它允许使用贝塞尔路径定义碰撞边界。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多