在SpriteKit中使用UIInterpolatingMotionEffect?

时间:2015-08-29 19:15:30

标签: ios swift sprite-kit parallax uimotioneffect

UIInterpolatingMotionEffect旨在与UIView一起使用,但有可能在节点上的SpriteKit中使用它吗?

如果没有,是否有人有想法以另一种方式实现这种效果。

提前致谢

1 个答案:

答案 0 :(得分:3)

通过创建一组SKNode图层,向图层添加精灵,然后在用户倾斜设备时将每个图层移动不同的数量,可以在Sprite Kit中创建视差效果。您可以选择缩放图层以给出深度幻觉。

这是Swift中的一个实现。由于它使用加速度计,因此仅适用于设备(​​而非模拟器)。

import SpriteKit
import CoreMotion

class GameScene: SKScene {

    let squareSize = CGFloat(50.0)
    let manager = CMMotionManager()
    let maxDistance = CGFloat(40.0)

    override func didMoveToView(view: SKView) {

        let colors = [SKColor.redColor(), SKColor.greenColor(), SKColor.blueColor()]

        for i in 0..<colors.count {
            let node = createLayer(colors[i], depth: CGFloat(colors.count-i), screenSize: view.frame.size)
            addChild(node)
        }

        var tiltX:CGFloat = 0.0
        var alpha:CGFloat = 0.25

        // Define block to handle accelerometer updates
        if manager.accelerometerAvailable {
            manager.accelerometerUpdateInterval = 0.1
            manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
                [weak self] (data: CMAccelerometerData!, error: NSError!) in
                // Low-pass filter to smooth the measurements
                tiltX = tiltX * (1-alpha) + CGFloat(data.acceleration.x) * alpha
                for (index, node) in enumerate(self!.children as! [SKNode]) {
                    // Move each layer by a different amount to produce parallax effect
                    let deltaX = tiltX*CGFloat(index+1)*self!.maxDistance
                    node.position = CGPoint(x:self!.size.width/2.0+deltaX, y:self!.size.height/2.0)
                }
            }
        }
    }

    func createLayer(color:SKColor, depth:CGFloat, screenSize:CGSize) -> SKNode {
        let scale = 1.0/depth
        let node = SKNode()
        // Scale layer to create depth effect
        node.setScale(scale)
        node.zPosition = -depth
        node.position = CGPoint(x:screenSize.width/2.0, y:screenSize.height/2.0)
        let size = CGSize(width: squareSize, height: squareSize)
        // Create squares at random positions
        for i in 1...10 {
            let square = SKSpriteNode(color: color, size: size)
            let x = CGFloat(arc4random_uniform(UInt32(screenSize.width))) - screenSize.width / 2.0
            let y = CGFloat(arc4random_uniform(UInt32(screenSize.height))) - screenSize.height / 2.0
            square.position = CGPoint(x:x, y:y)
            node.addChild(square)
        }
        return node
     }
 }