我需要使用NSSlider控制上传图像(JPEG,PNG,TIFF等)的透明度(或不透明度)。我怎么能这样做? 这是用AppDelegate.swift编写的代码
@IBAction func importButton(sender: AnyObject) {
let uploadFile: NSOpenPanel = NSOpenPanel()
uploadFile.allowsMultipleSelection = false
uploadFile.canChooseFiles = true
uploadFile.canChooseDirectories = false
uploadFile.runModal()
let chosenPicture = uploadFile.URL
if(chosenPicture != nil) {
let pictureImport = NSImage(contentsOfURL: chosenPicture!)
imageWell.image = pictureImport
}
}
@IBAction func brightnessSlider(sender: AnyObject) {
//var alpha = sender...
}
答案 0 :(得分:1)
试试这个
@IBAction func brightnessSlider(sender: NSSlider) {
var alpha = sender.minValue + (sender.doubleValue-sender.minValue)/(sender.maxValue-sender.minValue)
imageWell.alphaValue = CGFloat(alpha)
}
如果minValue
始终为0,则alpha的计算变得更简单:
var alpha = sender.doubleValue/sender.maxValue