簡體   English   中英

在WatchOS3中向SKShapeNode添加手勢

[英]Add gesture to SKShapeNode in WatchOS3

我能夠在SpriteKit Scene中添加手勢。 但是無法在場景中的某個節點上添加手勢。

此外, WatchKit沒有touchesBegan方法。 所以在我的界面中添加了gestureRecognizer並將其傳遞給SpriteKit場景。

但這導致將panGesture添加到整個場景而不是我的節點。

有沒有辦法將手勢添加到我的一個節點?

您不會向節點添加手勢。

您只能向可以識別手勢的任何內容添加手勢。

在UIKit中,這些是UIViews

在Watchkit中,這些是WKInterfaces

現在你想在Watchkit中做什么,就是當一個手勢開始時,檢測你是否正在觸摸一個節點

您可以通過獲取locationinObject的locationinObject並通過執行以下操作將其轉換為場景坐標系來在Watchkit中執行此操作:

extension WKGestureRecognizer {
    var position : CGPoint
    {
        get
        {
            let location = locationInObject()
            let bounds = objectBounds()
            let dWidth = self.scene.size.width / bounds.size.width
            let dHeight = self.scene.size.height / bounds.size.height
            let x = (location.x * dWidth) - scene.width * scene.anchorPoint.x)
            let y = (bounds.maxY - location.y) * dHeight - (scene.height * scene.anchorPoint.y)
            return CGPoint(x:x, y:y)
        }
    }
}

這樣做是在界面中獲取您的位置,然后通過翻轉y軸將其轉換為場景,然后調整場景寬度與界面寬度的差異。

現在使用它,你只需在識別器方法中調用它,並檢查你所觸摸的節點是否是你的節點

func didPan(_ recognizer: WKPanGestureRecognizer) {
        switch(recognizer.state)
        {
            case .began:
                let scenePosition = recognizer.position
                let node = scene.atPoint(scenePosition)
                guard let node = shapeNode else {return}
                //allow pan, so make a note or something
            case //do other cases here
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM