菜鸟教程小白 发表于 2022-12-11 19:15:58

ios - 用手势快速移动标签位置


                                            <p><p>我的 ViewController 中有一个标签。每次点击标签时,我都试图将标签的位置向左移动 50 点。这是我到目前为止所拥有的,但我的标签不会在模拟中移动。我确实对标签有限制。它大约有 100 宽和 50 高,并且也在 ViewController 中居中。</p>

<pre><code>    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let tapGesture = UITapGestureRecognizer(target: gestureLabel, action: #selector(moveLabel))
    gestureLabel.addGestureRecognizer(tapGesture)

}

func moveLabel(){
    if(left){
      moveLeft()
    }
    else{
      moveRight()
    }

}

func moveLeft(){
    let originalX = gestureLabel.frame.origin.x
    if(originalX &lt; 0){
      bringIntoFrame()
    }
    else{
      gestureLabel.frame.offsetBy(dx: -50, dy: 0)
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>UILabel 的 <code>userInteractionEnabled</code> 默认为 <code>false</code>。尝试将其设置为 <code>true</code></p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 用手势快速移动标签位置,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42823051/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42823051/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 用手势快速移动标签位置