如何复制iOS主屏幕行为?

时间:2017-03-06 05:23:30

标签: ios sprite-kit swift3 uigesturerecognizer

我目前正在使用SpriteKit进行游戏,我需要移动精灵以响应触摸(即当用户在SKView中的任何位置滑动或平移时。

我想得到平移的方向(对于轻扫我知道怎么做),以便精灵将根据平移移动(如果用户平移,我有一个为精灵定义的路径或者如果用户滑动则根据轻扫),触摸iOS appdrawer的工作方式,即它可以响应最轻微的滑动和平移(即当你向前或向后平移时,它会决定你是否想要移动到下一个屏幕)。

是否有任何文件? (我已经阅读了UIGestureRecognizer文档,但是我找不到实现它的方法。)

1 个答案:

答案 0 :(得分:1)

我在我的MenuScene上使用类似的东西,我有3页设置,用户可以滚动来获取各种游戏数据。但我不想轻轻触摸移动屏幕,这对用户来说是不和谐的。因此,我只是在Touches功能中观察手指移动,并检查移动是否大于我指定的最小移动量,如果它大于我滚动页面。在你的情况下你可以处理它;如果它大于最小移动量,则视为一个平底锅将其视为滑动

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touch: UITouch = touches.first!
    initialTouch = touch.location(in: self.view!)
    moveAmtY = 0
    moveAmtX = 0
    initialPosition = menuScroller.position
}

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

    let touch: UITouch = touches.first!
    let movingPoint: CGPoint = touch.location(in: self.view!)
    moveAmtX = movingPoint.x - initialTouch.x
    moveAmtY = movingPoint.y - initialTouch.y

    //their finger is on the page and is moving around just move the scroller and parallax backgrounds around with them
    //Check if it needs to scroll to the next page when they release their finger
    menuScroller.position = CGPoint(x: initialPosition.x + moveAmtX, y: initialPosition.y)
}

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

    //they havent moved far enough so just reset the page to the original position
    if fabs(moveAmtX) > 0 && fabs(moveAmtX) < minimum_detect_distance {
        resetPages()
    }

    //the user has swiped past the designated distance, so assume that they want the page to scroll
    if moveAmtX < -minimum_detect_distance {
        moveLeft()
    }
    else if moveAmtX > minimum_detect_distance {
        moveRight()
    }
}