67 lines
1.7 KiB
Swift
67 lines
1.7 KiB
Swift
//
|
|
// SliderNode.swift
|
|
// GoldWars
|
|
//
|
|
// Created by Niko Jochim on 05.05.20.
|
|
// Copyright © 2020 SP2. All rights reserved.
|
|
//
|
|
|
|
import SpriteKit
|
|
|
|
|
|
class SliderNode :SKNode {
|
|
|
|
var sliderLine :SKShapeNode
|
|
var sliderKnob :SliderKnob
|
|
var width: CGFloat
|
|
|
|
var getValue: CGFloat{
|
|
get{
|
|
return ((sliderKnob.position.x.rounded() - sliderKnob.min) / width)
|
|
}
|
|
}
|
|
|
|
init(width: CGFloat, position: CGPoint) {
|
|
self.width = width
|
|
sliderLine = SKShapeNode(rectOf: CGSize(width: width, height: 8))
|
|
sliderLine.position = position
|
|
sliderLine.fillColor = SKColor.white
|
|
sliderKnob = SliderKnob(circleOfRadius: 20)
|
|
sliderKnob.min = position.x - width / 2
|
|
sliderKnob.max = position.x + width / 2
|
|
sliderKnob.fillColor = SKColor.red
|
|
sliderKnob.zPosition = sliderLine.zPosition + 1
|
|
sliderKnob.position = CGPoint(x: sliderLine.position.x, y: sliderLine.position.y + 1)
|
|
super.init()
|
|
|
|
}
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
}
|
|
|
|
class SliderKnob: SKShapeNode {
|
|
var min = CGFloat()
|
|
var max = CGFloat()
|
|
|
|
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
|
|
|
|
for touch in touches {
|
|
let touchLocation = touch.location(in: self.scene!)
|
|
|
|
if self.position.x >= min - 1 && self.position.x <= max + 1{
|
|
self.position.x = touchLocation.x
|
|
}
|
|
|
|
if(self.position.x <= min){
|
|
self.position.x = min
|
|
}
|
|
if(self.position.x >= max){
|
|
self.position.x = max
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|