好做,這裏是我做過什麼,當我需要更改按鈕的外觀:繼承它。
//
// LSButtonColor.swift
//
// Created by Lloyd Sargent on 8/26/16.
// Copyright © 2016 Canna Software. All rights reserved.
// License: Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the 「Software」), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED 「AS IS」, WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
import Cocoa
class LSButtonColor: NSButton {
fileprivate struct AssociatedKeys {
static var variableDictionary = "ls_variableDictionary"
}
fileprivate var variableDictionary: [String:AnyObject]! {
get {
var localVariableDictionary = objc_getAssociatedObject(self, &AssociatedKeys.variableDictionary) as! [String:AnyObject]!
if localVariableDictionary == nil {
localVariableDictionary = [String:AnyObject]()
objc_setAssociatedObject(self, &AssociatedKeys.variableDictionary, localVariableDictionary, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
return localVariableDictionary
}
set(updatedDictionary) {
objc_setAssociatedObject(self, &AssociatedKeys.variableDictionary, updatedDictionary, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
var backgroundColor: NSColor! {
get {
return variableDictionary["backgroundColor"] as? NSColor
}
set(newBackgroundColor) {
var localVariableDictionary = variableDictionary
localVariableDictionary?["backgroundColor"] = newBackgroundColor
variableDictionary = localVariableDictionary
}
}
var altBackgroundColor: NSColor! {
get {
return variableDictionary["altBackgroundColor"] as? NSColor
}
set(newAltBackgroundColor) {
var localVariableDictionary = variableDictionary
localVariableDictionary?["altBackgroundColor"] = newAltBackgroundColor
variableDictionary = localVariableDictionary
}
}
var borderColor: NSColor! {
get {
return variableDictionary["borderColor"] as? NSColor
}
set(newBorderColor) {
var localVariableDictionary = variableDictionary
localVariableDictionary?["borderColor"] = newBorderColor
variableDictionary = localVariableDictionary
self.layer?.borderColor = newBorderColor.cgColor
}
}
var cornerRadius: CGFloat {
get {
return variableDictionary["cornerRadius"] as! CGFloat
}
set(newCornerRadius) {
var localVariableDictionary = variableDictionary
localVariableDictionary?["cornerRadius"] = newCornerRadius as AnyObject?
variableDictionary = localVariableDictionary
self.layer?.cornerRadius = newCornerRadius
}
}
var borderWidth: CGFloat {
get {
return variableDictionary["borderWidth"] as! CGFloat
}
set(newBorderWidth) {
var localVariableDictionary = variableDictionary
localVariableDictionary?["borderWidth"] = newBorderWidth as AnyObject?
variableDictionary = localVariableDictionary
self.layer?.borderWidth = newBorderWidth
}
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
if self.isHighlighted {
if self.layer != nil {
self.layer?.backgroundColor = self.altBackgroundColor.cgColor
}
}
else {
if self.layer != nil {
self.layer?.backgroundColor = self.backgroundColor.cgColor
}
}
}
}
好吧,你可能會令人費解的第一件事就是objc_getAssociatedObject
- 我原本這是一個類別,它是增加了變量的categorys(一種易於peasy方式人人都說你不能做到這一點,但每個人都沒有意識到,是的,你可以 - 這只是不明顯)。
不幸的是,我遇到了一些問題,所以我只是做了它的類 - 並沒有理會採取了相關的對象(爲什麼成功爛攤子?)
無論如何,你應該能夠拉出相關的代碼來改變任何按鈕的背景。
非常感謝,我知道如何隨機挑選顏色,但第一部分太棒了! – 2015-04-02 19:05:10
很高興幫助你:) – 2015-04-02 19:06:38