在所有设备的 spritekit 中设置 UISlider 的位置时遇到问题?

Having trouble setting position of UISlider in spritekit for all devices?

我正在使用此代码为我的所有设备正确设置位置。我遇到的问题是,如果我在 4s 中设置滑块的位置,iphone 6+ 位置会发生变化。我的其他设备也一样。如何让每个设备都完美定位滑块?我在 spritekit 顺便说一句。

//iphone4s
if UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.maxLength < 568.0 {
    middleSlider = UISlider(frame: CGRectMake(180, 50, 150, 20))
    middleSlider.tintColor = UIColor.whiteColor()
    middleSlider.setThumbImage(UIImage(named: "thumb2"), forState: UIControlState.Normal)
    self.view?.addSubview(middleSlider)
    bottomAudioControlBg.position = CGPointMake(self.size.width / 2, self.size.height / 7)

}



//iphone5
if UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.maxLength == 568.0 {

}



//iphone6
if UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.maxLength == 667.0 {

}



//iphone6+
if UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.maxLength == 736.0 {
    middleSlider = UISlider(frame: CGRectMake(255, 50, 150, 20))
    middleSlider.tintColor = UIColor.whiteColor()
    middleSlider.setThumbImage(UIImage(named: "thumb2"), forState: UIControlState.Normal)
    self.view?.addSubview(middleSlider)


}




//ipad
if UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.maxLength == 1024.0 {

}



//ipadpro
if UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.maxLength == 1366.0 {

}

这样的问题代码太多了:)

使用一个简单的比例因子你可以很容易地解决这个问题:

 let scaleFactor = UIScreen.mainScreen().bounds.width / 320

        let middleSlider = UISlider(frame: CGRectMake(180 * scaleFactor, 50, 150, 20))
        middleSlider.tintColor = UIColor.whiteColor()
        middleSlider.setThumbImage(UIImage(named: "thumb2"), forState: UIControlState.Normal)
        self.view?.addSubview(middleSlider)