微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – touchesEnded不被认可

当我用touchesBegan()调用

override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
        print("TOUCH_BEGAN")
}

我得到了打印声明.
我注意到,如果我不移动接触点,

override func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?) {

        print("TOUCH_ENDED")
}

没有被召唤,但是当我移动接触点时.调用touchesEnded()需要一些最小的移动吗?如果是这样,有没有办法覆盖这个,以确保始终调用touchesEnded()?

完整的代码太长了,无法添加,但下面多一点:

import SpriteKit
import UIKit
import Foundation

class GameScene: SKScene,SKPhysicsContactDelegate {
    // MARK: Properties
    var touchStart: CGPoint?
    let minSwipedistance:CGFloat = 22
    var distance: CGFloat = 0

    override func didMovetoView(view: SKView) {
        super.didMovetoView(view)
        //...

        // MARK: INCLUDE SWIPE GESTURES

        let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self,action: Selector("swipedRight:"))
        swipeRight.direction = .Right
        swipeRight.cancelstouchesInView = false
        swipeRight.delaystouchesEnded = false
        view.addGestureRecognizer(swipeRight)

        let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self,action: Selector("swipedLeft:"))
        swipeLeft.direction = .Left
        swipeLeft.cancelstouchesInView = false
        swipeLeft.delaystouchesEnded = false
        view.addGestureRecognizer(swipeLeft)

        let swipeUp: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self,action: Selector("swipedUp:"))
        swipeUp.direction = .Up
        swipeUp.cancelstouchesInView = false
        swipeUp.delaystouchesEnded = false
        view.addGestureRecognizer(swipeUp)

        let swipeDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self,action: Selector("swipedDown:"))
        swipeDown.direction = .Down
        swipeDown.cancelstouchesInView = false
        swipeDown.delaystouchesEnded = false
        view.addGestureRecognizer(swipeDown)
    }

    // MARK: ******* User Interaction ******

    override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
        if let touch = touches.first {
            touchStart = touch.locationInNode(self)
            let location = touch.locationInNode(self)
            let node = nodeAtPoint(location)
        }
    }

    override func touchesMoved(touches: Set<UITouch>,withEvent event: UIEvent?) {
    }

    override func touchesCancelled(touches: Set<UITouch>?,withEvent event: UIEvent?) {    
    }

    override func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?) {
        if let touch = touches.first,start = touchStart {
            let location = touch.locationInNode(self)
            // Compute the distance between the two touches
            let dx = location.x - start.x
            let dy = location.y - start.y
            distance = sqrt(dx*dx + dy*dy)
            if distance < minSwipedistance {
                let node = nodeAtPoint(location)
                print("NODE NAME: \(node.name)")
            }
        }
        // Reset the initial location
        touchStart = nil
    }

    // MARK: handle swipes
    func swipedRight(sender: UISwipeGestureRecognizer) {
        print("swiped right")
    }

    func swipedLeft(sender: UISwipeGestureRecognizer) {
        print("swiped left")
    }

    func swipedUp(sender: UISwipeGestureRecognizer) {
        print("swiped up")
    }

    func swipedDown(sender: UISwipeGestureRecognizer) {
        print("swiped down")
    }
}

解决方法

您无法确保调用touchesEnded,因为可能会调用touchesCancelled.您应该始终在两者中实现结束功能.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐