【问题标题】:Tracing touches with touchesMoved in Swift在 Swift 中使用 touchesMoved 跟踪触摸
【发布时间】:2015-02-16 12:02:14
【问题描述】:

我在 Swift 中使用 touchesMoved 方法跟踪我的触摸,但是当我只滑动屏幕时我的应用程序崩溃了。需要明确的是:当我按住触摸一会儿,然后移动时,它不会崩溃;当我立即滑动时,它因运行时错误而崩溃:

locationInView:]: 无法识别的选择器发送到实例

我的代码如下所示:

   override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        println("MOVED")

        var aTouch: AnyObject = touches as AnyObject

        var location = aTouch.locationInView(self.imageViewer)

        self.textEdit.frame = CGRectMake(0, location.y, self.view.frame.width, 44)
    }

任何关于为什么会发生这种情况的建议将不胜感激。

编辑:

不知道为什么touchesBegan 方法会崩溃,但我使用这样的手势识别器得到了想要的效果:

func buttonLongPressed(gestureRecognizer:UIGestureRecognizer){

    if gestureRecognizer.state == UIGestureRecognizerState.Changed{
    self.textEdit.frame = CGRectMake(0, gestureRecognizer.locationInView(self.view).y, self.view.frame.width, 44)
    }
}

希望这会有所帮助。

【问题讨论】:

    标签: ios swift uiview uigesturerecognizer uitouch


    【解决方案1】:

    发生这种情况是因为您将触摸(这是一个 Set)分配给 aTouch 变量:

    var aTouch: AnyObject = touches as AnyObject
    

    尝试将其更改为:

    var aTouch: AnyObject = touches.anyObject() as UITouch
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多