Swift, using UISwipeGestureRecongizer



我目前正在开发我的第一个使用 Swift 1.2 和 Xcode 6.3 的应用程序,并且遇到了一堵墙。

到目前为止,我有两个精灵在视图上方生成并下降到视图下方

func spawnSquare() {
    let square = SKSpriteNode(imageNamed: "Square")
    square.physicsBody = SKPhysicsBody(rectangleOfSize: square.size)
    square.physicsBody?.dynamic = true
    let positionStart = CGPointMake(size.width * 0.5, size.width * 2.0)
    let positionEnd = CGPointMake(size.width * 0.5, size.height * -2.0)
     square.position = positionStart
            addChild(square)

    let shapesMovment = SKAction.moveTo(positionEnd, duration: shapeSpeed)
        square.runAction(shapesMovment)
}
func spawnCircle() {
    let circle = SKSpriteNode(imageNamed: "Circle")
    circle.physicsBody = SKPhysicsBody (circleOfRadius:    
    circle.size.width/2 )
    circle.physicsBody?.dynamic = true
    let positionStart = CGPointMake(size.width * 0.5, size.width * 2.0)
    let positionEnd = CGPointMake(size.width * 0.5, size.height * -2.0)
    circle.position = positionStart
            addChild(circle)

    let shapesMovment = SKAction.moveTo(positionEnd, duration: shapeSpeed)
        circle.runAction(shapesMovment)
        }

现在我希望能够向左或向右滑动其中一个精灵,但我不知道如何以使用 UISwipeGestureCogniser(英语:UISwipeGestureRecogniser) 实现移动操作。

我已经在didMoveToView中添加了两个手势识别器

  let swipeLeft: UISwipeGestureRecognizer  = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
    swipeLeft.direction = .Left
    view.addGestureRecognizer(swipeLeft)
    let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
    swipeRight.direction = .Right
    view.addGestureRecognizer(swipeRight)

我在GameScene的底部添加了两个函数,因此println将说明我滑动了

func swipedRight(sender: UISwipeGestureRecognizerDirection) {
    println("Swiped Right")
   }
   func swipedLeft(sender: UISwipeGestureRecognizerDirection) {
        println("Swiped Left")
    }

现在我假设我可以在其中一个滑动函数中编写代码来告诉 SwipeGesture 我想移动精灵,但我不知道从哪里开始,也没有太多运气在网上寻找答案,所以任何帮助将不胜感激。

滑动

手势识别器是"一次性"交易。你被告知"用户刷了!"就是这样。你不能让用户根据轻扫手势拖动你的子画面。

如果您想在滑动时进行固定操作(例如将侧面的精灵动画化),那么滑动就可以正常工作。

您的向右滑动和向左滑动功能是错误的。手势识别器的目标方法始终传递一个且仅传递一个参数:手势识别器。

手势识别器具有其附加到的视图的参数。

您需要重写滑动右和左滑动函数,并将滑动手势识别器作为唯一参数。

然后在函数的主体中,您需要从手势识别器(您的精灵)获取视图属性。然后,您可以在该视图上执行所需的任何动画。(

相关内容

  • 没有找到相关文章

最新更新