ios - 快速触摸开始并将其显示在触摸的对象上

标签 ios swift swift2 touchesbegan touchesmoved

我想显示触摸和移动的对象的 x 坐标。

尝试一下:

import UIKit

class ViewController: UIViewController {

    var location = CGPoint(x: 0, y: 0)        
    @IBOutlet weak var viewBox: UIView!
    @IBOutlet weak var cntInViewBox: UILabel!

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first! as UITouch
        location = touch.locationInView(self.view)
        viewBox.center = location
        /*cntInViewBox.text=String(location.x)*/
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first! as UITouch
        location = touch.locationInView(self.view)
        viewBox.center = location          
        /*cntInViewBox.text=String(location.x)*/
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        viewBox.center = CGPoint(x:0, y: 0)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

没有线路

cntIntViewBox.text=String(location.x)

效果很好。触摸屏幕,viewBox 跳跃,开始触摸,盒子移动。

但是,如果我激活线来显示坐标(例如,我指的是任何动态文本),则移动和跳跃将不再起作用。

为什么会出现这个问题?

最佳答案

问题是您的行更改了标签的文本。这会导致在整个界面中执行自动布局。您的 viewBox View 由自动布局约束定位,因此这些约束接管并用于定位 View 。约束没有改变,因此 View 不会移动。

关于ios - 快速触摸开始并将其显示在触摸的对象上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33170454/

相关文章:

swift - Kotlin 中的条件接口(interface)

swift - 如何使用 Alamofire 从相机胶卷上传图像

ios - 如何将 GIF 图像添加到 Assets 文件夹并以编程方式将它们加载到 UIImageView 中

iOS Swift 2 Twitter 登录集成

ios - 即使登录错误也能登录

iphone - 如何使用 Phonegap 2.7 保持 iPhone 上的屏幕

ios - iOS 上的阴影更强

swift - 如何从 Healthkit 查询每日总步数并显示到 SwiftUI TextView 中?

android - 在 ionic 框架中,如何默认将第一个 ionic 段按钮设置为 Activity 状态?

ios - 类型 'UITouch' 的值没有成员 'locationInNode'