0
votes

I'm willing to create an horizontal spinning wheel with SpriteKit to use it as my protagonist cellphone homepage. Something like this, to explain me better: a wheel that scrolls element when spinned horizontally . What I'm looking for

I've already created a class called Box to create the icons and delegate their slide, I attach the two codes down here to be as clearer as possible and help you out with what I've done. This is my Box class code:

    import SpriteKit
    import GameplayKit
    
    
    protocol BoxDelegate: NSObjectProtocol {
        func boxSwiped(to: String)
    }

    class Box: SKSpriteNode {

    weak var boxDelegate: BoxDelegate!
    private var moveAmtX: CGFloat = 0
    private var moveAmtY: CGFloat = 0
    private let minimum_detect_distance: CGFloat = 50
    private var initialPosition: CGPoint = CGPoint.zero
    private var initialTouch: CGPoint = CGPoint.zero
    private var resettingSlider = false
    var rest: CGFloat = 0
    var clickable: Bool = false

    override init(texture: SKTexture?, color: UIColor, size: CGSize) {

        super.init(texture: texture, color: color, size: size)
        self.clickable = false

        self.isUserInteractionEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func moveWithCamera() {
    }

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

        if let touch = touches.first as UITouch? {

            initialTouch = touch.location(in: self.scene!.view)
            moveAmtY = 0
            moveAmtX = 0
            initialPosition = self.position
            print(initialPosition)
        }
    }

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

        if let touch = touches.first as UITouch? {

            let movingPoint: CGPoint = touch.location(in: self.scene!.view)

            moveAmtX = movingPoint.x - initialTouch.x
            moveAmtY = movingPoint.y - initialTouch.y
            print(moveAmtX, moveAmtY)
            self.position.x = initialPosition.x + moveAmtX
            let minus = UIScreen.main.bounds.width/2 + ((UIScreen.main.bounds.width/2) - self.position.x)
            if (UIScreen.main.bounds.width/2)/self.position.x < 1 {
                rest = (UIScreen.main.bounds.width/2)/self.position.x
            } else {
                let theFloat = (UIScreen.main.bounds.width/2)/self.position.x
                rest = (UIScreen.main.bounds.width/2)/minus
            }
            self.xScale = rest
            self.yScale = rest
            
        }
    }

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

        var direction = ""
        if fabs(moveAmtX) > minimum_detect_distance {

            //must be moving side to side
            if moveAmtX < 0 {
                direction = "left"
            }
            else {
                direction = "right"
            }
        }
        else if fabs(moveAmtY) > minimum_detect_distance {

            //must be moving up and down
            if moveAmtY < 0 {
                direction = "up"
            }
            else {
                direction = "down"
            }
        }

        print("object \(self.name!) swiped " + direction)

        self.boxDelegate.boxSwiped(to: direction)
    }
}

And this is my GameScene class:

import SpriteKit
import GameplayKit

class CellScene: SKScene, BoxDelegate {

    var box = Box()
    var box2 = Box()
    var box3 = Box()
    var Boxes: [Box] = []
    var Slots: [CGPoint] = []
    var swiped = ""

    override func sceneDidLoad(){
        createBoxes()
        createSlot()
    }

    func boxSwiped(to: String) {
        swiped = to
    }

    override func update(_ currentTime: TimeInterval) {
        checkBox()
    }
    
    func createSlot(){
        Slots[0] = CGPoint(x: UIScreen.main.bounds.width/2 , y: UIScreen.main.bounds.height/2)
        Slots[1] = CGPoint(x: UIScreen.main.bounds.width/5, y: UIScreen.main.bounds.height/2)
        Slots[2] = CGPoint(x: UIScreen.main.bounds.width  - UIScreen.main.bounds.width/5, y: UIScreen.main.bounds.height/2)
    }
    
    func createBoxes(){
        box = Box(color: .white, size: CGSize(width: 200, height: 200))
        box.zPosition = 1
        box.position = CGPoint(x: UIScreen.main.bounds.width/2 , y: UIScreen.main.bounds.height/2)
        box.name = "1"
        box.boxDelegate = self
        addChild(box)

        box2 = Box(color: .blue, size: CGSize(width: 200, height: 200))
        box2.zPosition = 1
        box2.name = "2"
        box2.position = CGPoint(x: UIScreen.main.bounds.width/5, y: UIScreen.main.bounds.height/2)
        box2.boxDelegate = self
        addChild(box2)

        box3 = Box(color: .red, size: CGSize(width: 200, height: 200))
        box3.zPosition = 1
        box3.name = "3"
        box3.position = CGPoint(x: UIScreen.main.bounds.width  - UIScreen.main.bounds.width/5, y: UIScreen.main.bounds.height/2)
        box3.boxDelegate = self
        addChild(box3)
        
        Boxes = [box, box2, box3]
    }
    
    func checkBox(){
        for i in 1...3 {
            let node = childNode(withName: String(i))
            
            var rest: CGFloat = 0
            let minus = UIScreen.main.bounds.width/2 + ((UIScreen.main.bounds.width/2) - node!.position.x)
            
            if (UIScreen.main.bounds.width/2)/node!.position.x < 1 {
                rest = (UIScreen.main.bounds.width/2)/node!.position.x
            } else {
                rest = (UIScreen.main.bounds.width/2)/minus
            }
            node!.xScale = rest
            node!.yScale = rest
            
            if Boxes[i].position == Slots[0] {
                Boxes[i].clickable = true
            }
        }
    }
}

Thanks to anyone who can help me out, I'm still at the beginning with SpriteKit and I want to know the best way to optimize my code :)

1

1 Answers

0
votes

Why not use SpriteKit animations with three separate panels, where you add the new contact as an image (sprite) to the collection to the same place of the list. Then scroll them with one offset for each panel. Three images per contact, one left angle, one centre, one right angle.