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.
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)
I am trying to create a simple BreakOut game.
There are blocks with different colors. I use these differences to set new attributes to the block. For example: A blue block will turn to a white block after the ball hits it. The white block will disappear...
My code so far:
let crashedBlock = againstBody.node! as! SKSpriteNode // againstBody is the body which touches the ball
switch crashedBlock.color {
case UIColor(red: 6/255, green: 123/255, blue: 178/255, alpha: 1.0):
crashedBlock.color = UIColor(red: 7/255, green: 151/255, blue: 218/255, alpha: 1.0)
break
case UIColor(red: 7/255, green: 151/255, blue: 218/255, alpha: 1.0):
crashedBlock.color = UIColor(red: 18/255, green: 175/255, blue: 248/255, alpha: 1.0)
break
case UIColor(red: 18/255, green: 175/255, blue: 248/255, alpha: 1.0):
crashedBlock.color = UIColor(red: 136/255, green: 215/255, blue: 251/255, alpha: 1.0)
break
case UIColor(red: 136/255, green: 215/255, blue: 251/255, alpha: 1.0):
crashedBlock.color = UIColor(red: 176/255, green: 228/255, blue: 253/255, alpha: 1.0)
break
case UIColor(red: 176/255, green: 228/255, blue: 253/255, alpha: 1.0):
crashedBlock.color = UIColor(red: 215/255, green: 242/255, blue: 254/255, alpha: 1.0)
break
case UIColor(red: 215/255, green: 242/255, blue: 254/255, alpha: 1.0):
crashedBlock.color = UIColor.white
break
case UIColor.white:
blocks.remove(againstBody.node!)
againstBody.node?.removeFromParent()
break
default: break
}
When I print the color of my block, I get a (1, 1, 1, 1), so that must be white. But it does not even get into my switch-case for white colors.
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)
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'