Swift Fortune Wheel changing color issue - ios
Im making an app where i have to add fortune wheel, for this i search the pod and find this library very useful https://github.com/sh-khashimov/SwiftFortuneWheel. Almost every thing is customisable but now i'm facing one issue with the library. i have a requirement that each slice color will be change once wheel stop, means winning color will be different and losing color will be different after wheel stop. i try to update it color is changing but now wheel is not stoping at correct index. which was stoping before i update color. can any one let me know what i'm doing wrong here. im pasting my code implentation here too. also i raised detail issue in github too https://github.com/sh-khashimov/SwiftFortuneWheel/issues/20.
import UIKit
import SwiftFortuneWheel
class VariousWheelPodiumViewController: UIViewController {
var selectedIndex = -1
#IBOutlet weak var wheelControl: SwiftFortuneWheel!
var prizes = [(id:0,name: "MONEYMONEY", color: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1),losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1), textColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)),
(id:1,name: "GRAPHIC", color: #colorLiteral(red: 0.9843137255, green: 0.9098039216, blue: 0.8274509804, alpha: 1),winningColor: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)),
(id:2,name: "HOME", color: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1),losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1), textColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)),
(id:3,name: "IDEA", color: #colorLiteral(red: 0.9843137255, green: 0.9098039216, blue: 0.8274509804, alpha: 1), winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)),
(id:4,name: "MANAGMENT", color: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)),
(id:5,name: "SEARCH", color: #colorLiteral(red: 0.9843137255, green: 0.9098039216, blue: 0.8274509804, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)),
(id:6,name: "TARGET", color: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)),
(id:7,name: "TIME", color: #colorLiteral(red: 0.9843137255, green: 0.9098039216, blue: 0.8274509804, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1))]
var slices = [Slice] ()
var finishIndex: Int {
return Int.random(in: 0..<wheelControl.slices.count)
}
override func viewDidLoad() {
super.viewDidLoad()
wheelControl.configuration = .variousWheelPodiumConfiguration
wheelControl.spinImage = "center"
wheelControl.isSpinEnabled = false
updateSlice()
wheelControl.slices = slices
}
#IBAction func rotateTap(_ sender: Any) {
let finishingIndex = self.finishIndex
print(finishingIndex)
wheelControl.startRotationAnimation(finishIndex: finishingIndex, continuousRotationTime: 1) { (isFinished) in
guard isFinished else { return }
print(self.prizes[finishingIndex])
self.selectedIndex = finishingIndex
self.updateSlice()
self.wheelControl.slices = self.slices
self.wheelControl.rotate(toIndex: finishingIndex)
}
}
func updateSlice(){
slices = []
for (index,prize) in prizes.enumerated() {
print(index,prize.name)
var titleTextPreferences = TextPreferences(textColorType: .customPatternColors(colors: nil, defaultColor: .black), font: .systemFont(ofSize: 10, weight: .bold), verticalOffset: 20)
titleTextPreferences.horizontalOffset = 10
titleTextPreferences.orientation = .vertical
titleTextPreferences.spacing = 0
titleTextPreferences.alignment = .left
var descriptionTextPreferences = TextPreferences(textColorType: .customPatternColors(colors: nil, defaultColor: .red), font: .systemFont(ofSize: 10), verticalOffset:-75)
descriptionTextPreferences.horizontalOffset = 0
descriptionTextPreferences.orientation = .vertical
descriptionTextPreferences.spacing = 0
descriptionTextPreferences.alignment = .left
let content: [Slice.ContentType] = [.text(text: prize.name, preferences: titleTextPreferences),.text(text: prize.name, preferences: descriptionTextPreferences)]
var slice = Slice(contents: content)
if selectedIndex == -1{
slice.backgroundColor = prize.color
}else{
slice.backgroundColor = index == selectedIndex ? prize.winningColor : prize.losingColor
}
slices.append(slice)
}
}
}
It would be nice if you could loop through the current slices and set their background colors directly. Quick testing of that code, though, and it doesn't work. Apparently, anytime you touch a "slice" object, the wheel resets its rotation.
You can try replacing your rotateTap(...) function with this:
#IBAction func rotateTap(_ sender: Any) {
let finishingIndex = self.finishIndex
print(finishingIndex)
wheelControl.startRotationAnimation(finishIndex: finishingIndex, continuousRotationTime: 1) { (isFinished) in
guard isFinished else { return }
print(self.prizes[finishingIndex])
self.selectedIndex = finishingIndex
self.updateSlice()
self.wheelControl.slices = self.slices
// set rotation async on main queue
DispatchQueue.main.async {
self.wheelControl.rotate(toIndex: finishingIndex)
}
}
}
See if that gives you acceptable results. If not, there is a more complex work-around, but ideally modify the SwiftFortuneWheel code to provide for a "modify slice property" solution.
Related
how to override button selection without fatal error
So my goal is to be able to go back to a table vc and the topic that was selected remains selected topic with the right questions displayed when the view is loaded again. As you can see below, I have 5 topics... This is how the view always loads, I always have the event topic selected with the tableview displaying the event questions and this is the code that handles the button selection: #IBAction func topicPressed(_ sender: UIButton) { eventsTopicButton.isSelected = false paymentsTopicButton.isSelected = false guestsTopicButton.isSelected = false refundsTopicButton.isSelected = false profileTopicButton.isSelected = false sender.isSelected = true switch sender.currentTitle { case "Events": rowsToDisplay = eventQuestions questionTableView.reloadData() eventsTopicButton.backgroundColor = #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1) profileTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) paymentsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) refundsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) guestsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) case "Payments": rowsToDisplay = paymentQuestions questionTableView.reloadData() eventsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) profileTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) paymentsTopicButton.backgroundColor = #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1) refundsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) guestsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) case "Guests": rowsToDisplay = guestQuestions questionTableView.reloadData() eventsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) profileTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) paymentsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) refundsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) guestsTopicButton.backgroundColor = #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1) case "Refunds": rowsToDisplay = refundQuestions questionTableView.reloadData() eventsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) profileTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) paymentsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) refundsTopicButton.backgroundColor = #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1) guestsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) case "Profile": rowsToDisplay = profileQuestions questionTableView.reloadData() profileTopicButton.backgroundColor = #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1) eventsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) paymentsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) refundsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) guestsTopicButton.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) default: break } } Now the issue i'm having is that when click a cell and segue to a vc from a selected topic different from Events, when the vc is exited and segued back to the main FAQ page the events button is selected by default and I want to override that. So I tried to run this block of code in the segued vc when the cell is selected, I get a thread error saying the value is nil. override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let vc = segue.destination as! FrequentlyAskedQuestionsViewController switch segue.identifier { case Constants.GuestQuestions.fromGuestQ1AnswerBackToFAQ: vc.rowsToDisplay = vc.guestQuestions vc.guestsTopicButton.backgroundColor = #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1) vc.eventsTopicButton.backgroundColor = #colorLiteral(red: 0.6666666865, green: 0.6666666865, blue: 0.6666666865, alpha: 1) // case Constants.GuestQuestions.fromGuestQ2AnswerBackToFAQ: // vc.guestsTopicButton.isSelected = true // vc.eventsTopicButton.isSelected = false default: break } } The vc.rowsToDisplay.. line works completely fine, but the UI would be misleading if the right rows were displayed but the button that is supposed to be selected isn't selected at all. I commented out the bottom 2 lines as well after I tried to use those instead but also got thread errors saying the value was nil. I can't understand how the value could be nil, if anyone can help explain this and provide a solution, that would be great. Thanks
access Xcode color picker 'Pencils' option programmatically
When I choose my button's background color in the storyboard, I can use a colorpicker widget. One of the tabs is called 'pencils'. This tab shows a picture of coloring pencils lined up. One pencil has the color 'mercury'. Is there a way to access this predefined color through the UIColor object? e.g. let myColor = UIColor(pencils: "mercury")
You could access Mercury color in code this way: let myColor = Color Click on the white square, then click on others, finally you could select Mercury color in the pencil tab.
Using Fkzm's answer, here are all the colors. public struct XCodePencilColorPalette { private init() { } public static let licorice = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) public static let lead = #colorLiteral(red: 0.1298420429, green: 0.1298461258, blue: 0.1298439503, alpha: 1) public static let tungsten = #colorLiteral(red: 0.2605174184, green: 0.2605243921, blue: 0.260520637, alpha: 1) public static let iron = #colorLiteral(red: 0.370555222, green: 0.3705646992, blue: 0.3705595732, alpha: 1) public static let steel = #colorLiteral(red: 0.4756349325, green: 0.4756467342, blue: 0.4756404161, alpha: 1) public static let tin = #colorLiteral(red: 0.5704585314, green: 0.5704723597, blue: 0.5704649091, alpha: 1) public static let nickel = #colorLiteral(red: 0.5741485357, green: 0.5741624236, blue: 0.574154973, alpha: 1) public static let aluminum = #colorLiteral(red: 0.6642242074, green: 0.6642400622, blue: 0.6642315388, alpha: 1) public static let magnesium = #colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1) public static let silver = #colorLiteral(red: 0.8374180198, green: 0.8374378085, blue: 0.8374271393, alpha: 1) public static let mercury = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1) public static let snow = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) public static let cayenne = #colorLiteral(red: 0.5807225108, green: 0.066734083, blue: 0, alpha: 1) public static let mocha = #colorLiteral(red: 0.5787474513, green: 0.3215198815, blue: 0, alpha: 1) public static let asparagus = #colorLiteral(red: 0.5738074183, green: 0.5655357838, blue: 0, alpha: 1) public static let fern = #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1) public static let clover = #colorLiteral(red: 0, green: 0.5603182912, blue: 0, alpha: 1) public static let moss = #colorLiteral(red: 0, green: 0.5628422499, blue: 0.3188166618, alpha: 1) public static let teal = #colorLiteral(red: 0, green: 0.5690457821, blue: 0.5746168494, alpha: 1) public static let ocean = #colorLiteral(red: 0, green: 0.3285208941, blue: 0.5748849511, alpha: 1) public static let midnight = #colorLiteral(red: 0.004859850742, green: 0.09608627111, blue: 0.5749928951, alpha: 1) public static let eggplant = #colorLiteral(red: 0.3236978054, green: 0.1063579395, blue: 0.574860394, alpha: 1) public static let plum = #colorLiteral(red: 0.5810584426, green: 0.1285524964, blue: 0.5745313764, alpha: 1) public static let maroon = #colorLiteral(red: 0.5808190107, green: 0.0884276256, blue: 0.3186392188, alpha: 1) public static let maraschino = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1) public static let tangerine = #colorLiteral(red: 1, green: 0.5781051517, blue: 0, alpha: 1) public static let lemon = #colorLiteral(red: 0.9994240403, green: 0.9855536819, blue: 0, alpha: 1) public static let lime = #colorLiteral(red: 0.5563425422, green: 0.9793455005, blue: 0, alpha: 1) public static let spring = #colorLiteral(red: 0, green: 0.9768045545, blue: 0, alpha: 1) public static let seaFoam = #colorLiteral(red: 0, green: 0.9810667634, blue: 0.5736914277, alpha: 1) public static let turquoise = #colorLiteral(red: 0, green: 0.9914394021, blue: 1, alpha: 1) public static let aqua = #colorLiteral(red: 0, green: 0.5898008943, blue: 1, alpha: 1) public static let blueberry = #colorLiteral(red: 0.01680417731, green: 0.1983509958, blue: 1, alpha: 1) public static let grape = #colorLiteral(red: 0.5818830132, green: 0.2156915367, blue: 1, alpha: 1) public static let magenta = #colorLiteral(red: 1, green: 0.2527923882, blue: 1, alpha: 1) public static let strawberry = #colorLiteral(red: 1, green: 0.1857388616, blue: 0.5733950138, alpha: 1) public static let salmon = #colorLiteral(red: 1, green: 0.4932718873, blue: 0.4739984274, alpha: 1) public static let cantaloupe = #colorLiteral(red: 1, green: 0.8323456645, blue: 0.4732058644, alpha: 1) public static let banana = #colorLiteral(red: 0.9995340705, green: 0.988355577, blue: 0.4726552367, alpha: 1) public static let honeydew = #colorLiteral(red: 0.8321695924, green: 0.985483706, blue: 0.4733308554, alpha: 1) public static let flora = #colorLiteral(red: 0.4500938654, green: 0.9813225865, blue: 0.4743030667, alpha: 1) public static let spindrift = #colorLiteral(red: 0.4508578777, green: 0.9882974029, blue: 0.8376303315, alpha: 1) public static let ice = #colorLiteral(red: 0.4513868093, green: 0.9930960536, blue: 1, alpha: 1) public static let sky = #colorLiteral(red: 0.4620226622, green: 0.8382837176, blue: 1, alpha: 1) public static let orchid = #colorLiteral(red: 0.476841867, green: 0.5048075914, blue: 1, alpha: 1) public static let lavender = #colorLiteral(red: 0.8446564078, green: 0.5145705342, blue: 1, alpha: 1) public static let bubblegum = #colorLiteral(red: 1, green: 0.5212053061, blue: 1, alpha: 1) public static let carnation = #colorLiteral(red: 1, green: 0.5409764051, blue: 0.8473142982, alpha: 1) }
I wanted to do the same thing, so I created a custom class. Hope this saves someone else some time :) To access, call PencilColors.instance... import Foundation import UIKit class PencilColors { static let instance = PencilColors() let pencilColors: [(color: UIColor, name: String, type: Int)] = [ //instead of creating an enum in each project: //type 0 -> black and white //type 1 -> dark colors //type 2 -> medium colors //type 3 -> light colors (color: .black, name: "Black", type: 0), (color: #colorLiteral(red: 0.1298420429, green: 0.1298461258, blue: 0.1298439503, alpha: 1), name: "Lead", type: 0), (color: #colorLiteral(red: 0.2605174184, green: 0.2605243921, blue: 0.260520637, alpha: 1), name: "Tungsten", type: 0), (color: #colorLiteral(red: 0.370555222, green: 0.3705646992, blue: 0.3705595732, alpha: 1), name: "Iron", type: 0), (color: #colorLiteral(red: 0.4756349325, green: 0.4756467342, blue: 0.4756404161, alpha: 1), name: "Steel", type: 0), (color: #colorLiteral(red: 0.5704585314, green: 0.5704723597, blue: 0.5704649091, alpha: 1), name: "Tin", type: 0), (color: #colorLiteral(red: 0.5741485357, green: 0.5741624236, blue: 0.574154973, alpha: 1), name: "Nickel", type: 0), (color: #colorLiteral(red: 0.6642242074, green: 0.6642400622, blue: 0.6642315388, alpha: 1), name: "Aluminum", type: 0), (color: #colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1), name: "Magnesium", type: 0), (color: #colorLiteral(red: 0.8374180198, green: 0.8374378085, blue: 0.8374271393, alpha: 1), name: "Silver", type: 0), (color: #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1), name: "Mercury", type: 0), (color: .white, name: "White", type: 0), (color: #colorLiteral(red: 0.5807225108, green: 0.066734083, blue: 0, alpha: 1), name: "Cayenne", type: 1), (color: #colorLiteral(red: 0.5787474513, green: 0.3215198815, blue: 0, alpha: 1), name: "Mocha", type: 1), (color: #colorLiteral(red: 0.5738074183, green: 0.5655357838, blue: 0, alpha: 1), name: "Asparagus", type: 1), (color: #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1), name: "Fern", type: 1), (color: #colorLiteral(red: 0, green: 0.5603182912, blue: 0, alpha: 1), name: "Clover", type: 1), (color: #colorLiteral(red: 0, green: 0.5628422499, blue: 0.3188166618, alpha: 1), name: "Moss", type: 1), (color: #colorLiteral(red: 0, green: 0.5690457821, blue: 0.5746168494, alpha: 1), name: "Teal", type: 1), (color: #colorLiteral(red: 0, green: 0.3285208941, blue: 0.5748849511, alpha: 1), name: "Ocean", type: 1), (color: #colorLiteral(red: 0.004859850742, green: 0.09608627111, blue: 0.5749928951, alpha: 1), name: "Midnight", type: 1), (color: #colorLiteral(red: 0.3236978054, green: 0.1063579395, blue: 0.574860394, alpha: 1), name: "Eggplant", type: 1), (color: #colorLiteral(red: 0.5810584426, green: 0.1285524964, blue: 0.5745313764, alpha: 1), name: "Plum", type: 1), (color: #colorLiteral(red: 0.5808190107, green: 0.0884276256, blue: 0.3186392188, alpha: 1), name: "Maroon", type: 1), (color: #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1), name: "Maraschino", type: 2), (color: #colorLiteral(red: 1, green: 0.5781051517, blue: 0, alpha: 1), name: "Tangerine", type: 2), (color: #colorLiteral(red: 0.9994240403, green: 0.9855536819, blue: 0, alpha: 1), name: "Lemon", type: 2), (color: #colorLiteral(red: 0.5563425422, green: 0.9793455005, blue: 0, alpha: 1), name: "Lime", type: 2), (color: #colorLiteral(red: 0, green: 0.9768045545, blue: 0, alpha: 1), name: "Spring", type: 2), (color: #colorLiteral(red: 0, green: 0.9810667634, blue: 0.5736914277, alpha: 1), name: "Sea Foam", type: 2), (color: #colorLiteral(red: 0, green: 0.9914394021, blue: 1, alpha: 1), name: "Turquoise", type: 2), (color: #colorLiteral(red: 0, green: 0.5898008943, blue: 1, alpha: 1), name: "Aqua", type: 2), (color: #colorLiteral(red: 0.01680417731, green: 0.1983509958, blue: 1, alpha: 1), name: "Blueberry", type: 2), (color: #colorLiteral(red: 0.5818830132, green: 0.2156915367, blue: 1, alpha: 1), name: "Grape", type: 2), (color: #colorLiteral(red: 1, green: 0.2527923882, blue: 1, alpha: 1), name: "Magenta", type: 2), (color: #colorLiteral(red: 1, green: 0.1857388616, blue: 0.5733950138, alpha: 1), name: "Strawberry", type: 2), (color: #colorLiteral(red: 1, green: 0.4932718873, blue: 0.4739984274, alpha: 1), name: "Salmon", type: 3), (color: #colorLiteral(red: 1, green: 0.8323456645, blue: 0.4732058644, alpha: 1), name: "Cantaloupe", type: 3), (color: #colorLiteral(red: 0.9995340705, green: 0.988355577, blue: 0.4726552367, alpha: 1), name: "Banana", type: 3), (color: #colorLiteral(red: 0.8321695924, green: 0.985483706, blue: 0.4733308554, alpha: 1), name: "Honeydew", type: 3), (color: #colorLiteral(red: 0.4500938654, green: 0.9813225865, blue: 0.4743030667, alpha: 1), name: "Flora", type: 3), (color: #colorLiteral(red: 0.4508578777, green: 0.9882974029, blue: 0.8376303315, alpha: 1), name: "Sprindrift", type: 3), (color: #colorLiteral(red: 0.4513868093, green: 0.9930960536, blue: 1, alpha: 1), name: "Ice", type: 3), (color: #colorLiteral(red: 0.4620226622, green: 0.8382837176, blue: 1, alpha: 1), name: "Sky", type: 3), (color: #colorLiteral(red: 0.476841867, green: 0.5048075914, blue: 1, alpha: 1), name: "Orchid", type: 3), (color: #colorLiteral(red: 0.8446564078, green: 0.5145705342, blue: 1, alpha: 1), name: "Lavender", type: 3), (color: #colorLiteral(red: 1, green: 0.5212053061, blue: 1, alpha: 1), name: "Bubblegum", type: 3), (color: #colorLiteral(red: 1, green: 0.5409764051, blue: 0.8473142982, alpha: 1), name: "Carnation", type: 3), ] func getAllColors() -> [UIColor] { let colorArray = self.pencilColors.map { (existingType) -> UIColor in return existingType.color } return colorArray } func getBlackAndWhiteColors() -> [UIColor] { let colorArray = self.pencilColors.filter { (existingColor) -> Bool in return existingColor.type == 0 }.map { (currentType) -> UIColor in return currentType.color } return colorArray } func getDarkColors() -> [UIColor] { let colorArray = self.pencilColors.filter { (existingColor) -> Bool in return existingColor.type == 1 }.map { (currentType) -> UIColor in return currentType.color } return colorArray } func getMediumColors() -> [UIColor] { let colorArray = self.pencilColors.filter { (existingColor) -> Bool in return existingColor.type == 2 }.map { (currentType) -> UIColor in return currentType.color } return colorArray } func getLightColors() -> [UIColor] { let colorArray = self.pencilColors.filter { (existingColor) -> Bool in return existingColor.type == 3 }.map { (currentType) -> UIColor in return currentType.color } return colorArray } func getColorName(fromColor color: UIColor) -> String { let name = self.pencilColors.first { (existingColor) -> Bool in return existingColor.color == color }.map { (existingType) -> String in return existingType.name } return name ?? "" } }
trying to make navigation bar background color r:1 g:68 r:148
tried this UINavigationBar.appearance().barTintColor = UIColor(red: 1/255, green: 68/255, blue: 148/255, alpha: 1) UINavigationBar.appearance().tintColor = UIColor.white UINavigationBar.appearance().titleTextAttributes =[NSForegroundColorAttributeName:UIColor.white] and also tried this UINavigationBar.appearance().barTintColor = UIColor(red: 1, green: 68, blue: 148, alpha: 1) UINavigationBar.appearance().tintColor = UIColor(red: 1, green: 68, blue: 148, alpha: 1) UINavigationBar.appearance().backgroundColor = UIColor(red: 1, green: 68, blue: 148, alpha: 1) Can anyone help me achieve correct color. I must be using the various params wrong thanks
UINavigationBar.appearance().barTintColor = UIColor(red: 1.0/255.0, green: 68.0/255.0, blue: 148.0/255.0, alpha: 1.0) Give Float values
Your code will always return 0 . Change UINavigationBar.appearance().barTintColor = UIColor(red: 1/255, green: 68/255, blue: 148/255, alpha: 1) to UINavigationBar.appearance().barTintColor = UIColor(red: 1.0/255.0f, green: 68.0/255.0f, blue: 148.0/255.0f, alpha: 1.0)
How can I animate the background color of a label using Swift
I have this code the animate the background color perfectly: UIView.animate(withDuration: 2, delay: 0.0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: { self.view.backgroundColor = UIColor(red: 0/255, green: 185/255, blue: 215/255, alpha: 1.0) self.view.backgroundColor = UIColor(red: 0/255, green: 78/255, blue: 215/255, alpha: 1.0) self.view.backgroundColor = UIColor(red: 0/255, green: 215/255, blue: 138/255, alpha: 1.0) }, completion: nil) But then I tried to use it to animate the background color of a label, but in a darker color, and it didn't work: UIView.animate(withDuration: 2, delay: 0.0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: { self.topBar.backgroundColor = UIColor(red: 0/255, green: 159/255, blue: 184/255, alpha: 1.0) self.topBar.backgroundColor = UIColor(red: 0/255, green: 67/255, blue: 184/255, alpha: 1.0) self.topBar.backgroundColor = UIColor(red: 0/255, green: 184/255, blue: 117/255, alpha: 1.0) }, completion: nil) Is there something wrong, or what can I do to animate the background color of a label?
try animate layer.backgroundColor instead backgroundColor this code works for me UIView.animate(withDuration: 2, delay: 0.0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: { self.label.layer.backgroundColor = UIColor(red: 0/255, green: 185/255, blue: 215/255, alpha: 1.0).cgColor self.label.layer.backgroundColor = UIColor(red: 0/255, green: 78/255, blue: 215/255, alpha: 1.0).cgColor self.label.layer.backgroundColor = UIColor(red: 0/255, green: 215/255, blue: 138/255, alpha: 1.0).cgColor }, completion: nil)
(UIButton) -> 0' does not have a member named 'setTitleColor'
I'm trying to modify the colour of a UIButton in Swift using the code below: struct ColorWheel { let colorsArray = [ UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), //teal color UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), //yellow color UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), //red color UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), //orange color UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), //dark color UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), //purple color UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), //green color ] func randomColor() -> UIColor { var unsignedArrayCount = UInt32(colorsArray.count) var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount) var randomNumber = Int(unsignedRandomNumber) return colorsArray[randomNumber] } } var colorWheel = ColorWheel() var randomColor = colorWheel.randomColor() view.backgroundColor = randomColor myButton.setTitleColor(randomColor, forState: .Normal) This code will work perfectly when changing the background colour, but when I try to use the setTitleColor method, I get the following error: (UIButton) -> 0' does not have a member named 'setTitleColor'