简体   繁体   中英

Setting CALayer color in raywenderlich.com article

I have a question about this article https://www.raywenderlich.com/7595-how-to-make-a-custom-control-tutorial-a-reusable-slider

I substitute

var trackTintColor = UIColor(white: 0.9, alpha: 1) {
    didSet {
      trackLayer.setNeedsDisplay()
    }
  }

with

var trackTintColor = UIColor(red: 56, green: 81, blue: 117, alpha: 1) {
    didSet {
      trackLayer.setNeedsDisplay()
    }
  }

in RangeSlider.swift .

But the color that I want won't appear and RangeSlider have no background color.

Could you let me know why? And How could I make RangeSlider have the color that I want?

Thanks for reading.

The value range of the UIColor components must be between 0.0 and 1.0 which is pretty obvious in the first example.

You could divide the UInt8 values by 255 (as floating point numbers to get correct floating point results)

var trackTintColor = UIColor(red: 56.0/255.0, green: 81.0/255.0, blue: 117.0/255.0, alpha: 1.0) { ...

or pre-calculate the values, or define the color in the asset catalog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM