在Swift中放大和转出SKSpriteNode

时间:2015-04-10 01:48:22

标签: ios swift sprite-kit

首先,我已经看到并尝试实施类似问题hereherehere的其他答案。问题是我去年开始使用Swift进行iOS编程,(遗憾的是)我没有先学习ObjC(是的,它现在在我的待办事项列表中)。 ; - )

所以请看一看,看看你是否可以通过这个方式帮我看看。

我可以轻松捏缩放整个SKScene。我还可以使用其他UI手势(即滑动)和SKActions来向上/向下扩展SKSpiteNode。

基于 this post我已经将SKAction应用于UIPinchGestureRecognizer,它可以完美地放大IN,但我不能让它缩小OUT。

我错过了什么?

以下是我的示例项目代码:

class GameScene: SKScene {

var board = SKSpriteNode(color: SKColor.yellowColor(), size: CGSizeMake(200, 200))

func pinched(sender:UIPinchGestureRecognizer){
    println("pinched \(sender)")
    // the line below scales the entire scene
    //sender.view!.transform = CGAffineTransformScale(sender.view!.transform, sender.scale, sender.scale)
    sender.scale = 1.01

    // line below scales just the SKSpriteNode
    // But it has no effect unless I increase the scaling to >1
    var zoomBoard = SKAction.scaleBy(sender.scale, duration: 0)
    board.runAction(zoomBoard)
}

// line below scales just the SKSpriteNode
func swipedUp(sender:UISwipeGestureRecognizer){
    println("swiped up")
    var zoomBoard = SKAction.scaleBy(1.1, duration: 0)
    board.runAction(zoomBoard)
}

// I thought perhaps the line below would scale down the SKSpriteNode
// But it has no effect at all
func swipedDown(sender:UISwipeGestureRecognizer){
    println("swiped down")
    var zoomBoard = SKAction.scaleBy(0.9, duration: 0)
    board.runAction(zoomBoard)
}

override func didMoveToView(view: SKView) {

    self.addChild(board)

    let pinch:UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: Selector("pinched:"))
    view.addGestureRecognizer(pinch)


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

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


}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    // should I be using this function instead?


}

感谢@sangony的帮助,我终于得到了这个工作。我想我会发布工作代码,万一其他人想在Swift中看到它。

var board = SKSpriteNode(color: SKColor.yellowColor(), size: CGSizeMake(200, 200))

var previousScale = CGFloat(1.0)

func pinched(sender:UIPinchGestureRecognizer){
    if sender.scale > previousScale {
        previousScale = sender.scale
        if(board.size.height < 800) {
            var zoomIn = SKAction.scaleBy(1.05, duration:0)
            board.runAction(zoomIn)
        }
    }
    if sender.scale < previousScale {
        previousScale = sender.scale
        if(board.size.height > 200) {
            var zoomOut = SKAction.scaleBy(0.95, duration:0)
            board.runAction(zoomOut)
        }
    }

1 个答案:

答案 0 :(得分:0)

我尝试了你的代码(在Objective C中)并使用pinch放大和缩小。我不认为您的代码有任何问题,但您可能没有考虑比例因素,因为它们被放在不断变化的精灵大小上。

您可以轻松地进行缩放,或者需要多次捏合手势才能使节点恢复到可管理的大小。我建议您使用步骤过程,而不是直接使用缩放属性作为缩放因子。您还应该有缩放尺寸的最大/最小限制。

要使用步骤过程,您需要创建一个CGFloat ivar previousScale来存储最后一个缩放值,以确定当前的缩放是放大还是缩小。然后,您将新传递的sender.scale与ivar进行比较,并根据比较放大或缩小。

应用最小和最大比例限制,一旦达到它们就停止缩放。

下面的代码在Obj-C中,但我相信你可以得到它的要点:

首先声明你的ivar float float previousScale;

- (void)handlePinch:(UIPinchGestureRecognizer *)sender {

    NSLog(@"pinchScale:%f",sender.scale);

    if(sender.scale > previousScale) {
        previousScale = sender.scale;

        // only scale up if the node height is less than 200
        if(node0.size.height < 200) {
            // step up the scale factor by 0.05
            [node0 runAction:[SKAction scaleBy:1.05 duration:0]];
        }
    }

    if(sender.scale < previousScale) {
        previousScale = sender.scale;

        // only scale down if the node height is greater than 20
        if(node0.size.height > 20) {
            // step down the scale factor by 0.05
            [node0 runAction:[SKAction scaleBy:0.95 duration:0]];
        }
    }
}