Button opacity / alpha setting - swift

I want to change the alpha when the user pressed the button. I have used the below code and it works.
However, I am having to write this line of code for every button I connect to the assistance.
Is there any way that I can create a method/class/function to enable all buttons to have this function?
#IBAction func sundayButton(_ sender: UIButton) {
sender.alpha = 0.5
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {sender.alpha = 1.0
}
}

You can write extention on Button and call that on every button
extension UIButton {
func setAlpha() {
self.alpha = 0.5
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.alpha = 1.0
}
}
}
call it this way:
myButton.setAlpha()

Related

Change NSTextField text color with fade animation - Cocoa

I am trying to change NSTextField text color with fade animation by using NSAnimationContext. But it's not working. Please help me to solve this issue. Thanking you all!
Code:
override func viewDidLoad() {
super.viewDidLoad()
label!.animator().textColor = NSColor.black
}
#IBAction func changeColor(_ sender: NSButton){
NSAnimationContext.runAnimationGroup { (context) in
context.duration = 1.0
label!.animator().textColor = NSColor.red
}
}
Here is the outline of one possible solution:
Subclass NSAnimation, say with TextColorAnimation
Have the init take the NSTextField and final NSColor
Override currentProgress as per NSAnimation docs to (a) call the super implementation and (b) set the intermediate color and display the NSTextField
Use NSColor.blend(...) to determine the intermediate color
start this NSAnimation
You should get a nice smooth color transition. HTH
Update the color and alpha value in the completion handler:
#IBAction func changeColor(_ sender: NSButton){
NSAnimationContext.runAnimationGroup({ [self] context in
context.duration = 1.0
context.timingFunction = CAMediaTimingFunction(name: .easeOut)
label.animator().alphaValue = 0.0
label.animator().textColor = NSColor.black
}, completionHandler: { [self] in
NSAnimationContext.runAnimationGroup({ [self] context in
context.duration = 1.0
context.timingFunction = CAMediaTimingFunction(name: .easeIn)
label.animator().alphaValue = 1.0
label.animator().textColor = NSColor.red
}, completionHandler: {
})
})
}

MacOS Animation delay in swift

Is it possible to add a delay to an animation with Cocoa?
In my current code it shows a window and hides it with fade animation. What I am trying to do is add a delay before the fade animation.
#IBAction func doIt(_ sender: NSButton) {
openPanel()
NSAnimationContext.runAnimationGroup { (cont) in
cont.duration = 1.0
self.panel.animator().alphaValue = 0
}
//hide on completion
}
Wrap what you want to delay in a DispathQueue async call:
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { //delays 1 second
//code to delay
}

How to change button back to its original state

I'm currently working through one of Angela Yu's udemy courses in which one has to develop a working xylophone app.
The final aim was that the buttons (red, blue, green etc.) should change their opacity while pressed, as in the following code:
#IBAction func noteButton(_ sender: UIButton) {
playSound(soundName: sender.currentTitle!)
sender.alpha = 0.5
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
sender.alpha = 1
}
Is there a possibility to not only change the opacity but also the background color as a whole - e.g. grey (pressed) - back to its original color - after the delay?
I guess you'd have to store the button's original background color somehow but I don't know how.
Well one way would be to set the backgroundColor like so:
sender.backgroundColor = .white
You could also override the isHighlighted function if you are working with a UIButton class that you overrode. like so
override open var isHighlighted: Bool {
didSet {
backgroundColor = isHighlighted ? .black :.white
}
}
Obviously this would have to have to be overridden in a child class of UIButton.
you can store the original in a variable like this
#IBAction func noteButton(_ sender: UIButton) {
playSound(soundName: sender.currentTitle!)
let oldColor = sender.backgroundColor
sender.alpha = 0.5
sender.backgroundColor = .white
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
sender.alpha = 1
sender.backgroundColor = oldColor
}

How hide UILabel after 2 seconds Swift?

When I taped button UILabel appears and immediately disappears again. I need it to disappear after a few seconds. It's my first app and I can't solve this problem.
Thanks!
func done() {
if sauserImageView.isHidden == false && cupImageView.isHidden == false && spoonImageView.isHidden == false {
winningLabel.isHidden = false
}
}
You can perform a delayed action by using the DispatchQueue API, e.g.
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.label.isHidden = true
}
Or if you want to animate the hiding, use UIView.animate(withDuration:animations:) or UIView.animate(withDuration:delay:options:animations:completion:) e.g.:
UIView.animate(withDuration: 2) {
self.label.alpha = 0
}
Good luck!

smooth animate between 3 locations swift 3

still i'm bignner in swift and ios
i'm building my unversity project, but i faced some issues with animating, firstly here is my code:
#IBAction func tab1(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations:{
self.slider.center.x -= self.tab1.bounds.width
})
}
#IBAction func tab2(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations:{
self.slider.center.x += self.tab2.bounds.width
})
}
#IBAction func tab3(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations:{
self.slider.center.x += self.tab3.bounds.width
})
}
you can see how much it's basic , and there is many problems like if slider in tab1 and click on tab 3 slider will move one step (to tab2) than user needs press one more time, + if you are in tab2 and press on tab2 again slider move again , many bugs in my code, i search for solutions but no luck .
i want adjust it to be animate to correct place and validate it, that means if slider in correct place the slider will not move.
i hope find some help from swift developers
You can just use the button frame to calculate x:
let tab = sender as! UIView
UIView.animate(withDuration: 0.5, animations:{
self.slider.center.x = tab.frame.midX
})