UIButton transition - swift

I have a group of six buttons - when one is pressed (programmatically) I would like it to light up and then fade out. So I created a function called lightUp(button: UIButton). I also have two images for each button - lit and unlit. I can go from the default unlit to lit, but so far, trying to light the button first, before fading it back to unlit has been a problem. In the code below, the buttons don't light up at all.
func lightUp(button: UIButton){
button.setImage(UIImage(named: padArrayOn[button.tag]), for: .normal)
UIView.transition(with: button, duration: 0.4, options: .transitionCrossDissolve, animations: {
button.setImage(UIImage(named: self.padArrayOff[button.tag]), for: .normal)
}) { (bool) in
self.playSequence()
}
}

Set two transitions, the second in the completion handler of the first perhaps?
func lightUp(button: UIButton){
UIView.transition(with: button, duration: 0.2, options: .transitionCrossDissolve, animations: {
button.setImage(UIImage(named: padArrayOn[button.tag]), for: .normal)
}, completion: {_ in
UIView.transition(with: button, duration: 0.2, options: .transitionCrossDissolve, animations: {
button.setImage(UIImage(named: self.padArrayOff[button.tag]), for: .normal)
}, completion: { _ in
self.playSequence()
}
)})
}

if I understood you correctly you want to light up the button (maybe change the background) and then it should fade out.
You can try something like this:
func lightUpAndFadeOut(button: UIButton){
UIView.animate(withDuration: 2) {
button.backgroundColor = UIColor.red
}
UIView.animate(withDuration: 5) {
button.alpha = 0
}
}
Every button calls this function and it should look like this:
lightUpAndFadeOut(button: sender as! UIButton)
Correct?

Related

how to set background color animation?

When I click on the button, I animate the button to change the image via:
#IBAction func btn_lockdragg(sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected == true {
UIView.transition(with: sender as UIView, duration: 1.0, options: .transitionFlipFromRight, animations: {
sender.setImage(UIImage(systemName: "lock"), for: .normal)
}, completion: nil)
} else {
UIView.transition(with: sender, duration: 1.0, options: .transitionFlipFromLeft, animations: {
sender.setImage(UIImage(systemName: "lock.open"), for: .normal)
}, completion: nil)
}
}
Please how can I change background color of animation? I still need the background color of the button.
Finally I found the solution. For the button, the following must be set: Type: Custom, Style: Default and Background: Default

UIButton resize animation is not animating

I'm trying to make that my button double its size using the UIView animation, but for some reason it is not working it the size goes right, but no animated.
The function that should animate the button resizing
Irrelevant code above
#objc func createButtonPressed(){
//Removes the bottom stack with buttons
if let stackButton = self.view.viewWithTag(50){
stackButton.removeFromSuperview()
}
//Add the button back with half ares size
bottomHolder.addSubview(rightButton)
rightButton.setImage(nil, for: .normal)
rightButton.addTarget(self, action: #selector(anima), for: .touchUpInside)
rightButton.topAnchor.constraint(equalTo: bottomHolder.topAnchor).isActive = true
rightButton.trailingAnchor.constraint(equalTo: bottomHolder.trailingAnchor).isActive = true
rightButton.bottomAnchor.constraint(equalTo: bottomHolder.bottomAnchor).isActive = true
}
#objc func anima(){
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.rightButton.leadingAnchor.constraint(equalTo: self.bottomHolder.leadingAnchor).isActive = true
self.rightButton.layoutIfNeeded()
}, completion: nil)
}
Irrelevant code below
try this one:
let buttonFinalWidth = UIScreen.main.bounds.width
DispatchQueue.main.async {
UIView.animate(withDuration: 5.0) {
self.rightButton.widthAnchor.constraint(equalToConstant: buttonFinalWidth).isActive = true
self.view.layoutIfNeeded()
}
}

Trying change Button Title and label.center.x in UIView.animate when press button but stuck. Why? Swift 4 Xcode 9.4

My Problem
I'm trying do some animation with label using UIView.animate(..) when touchInside button . Everything still be OK until I added a line: "self?.confirm.setTitle("Đăng nhập", for: .normal). The animation doesn't work.
My Will
I want the yellow underline below Đăng Ký switch to below Đăng nhập.
It good when code is
#IBAction func signUpAction(_ sender:Any?){
if (signup == false){
signup = true
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
self?.underlineSignup.center.x -= (self?.underlineSignup.bounds.width)!
self?.view.layoutIfNeeded()
}, completion: nil)
}
}
#IBAction func signInAction(_ sender:Any?){
if (signup == true){
signup = false
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
self?.underlineSignup.center.x += (self?.underlineSignup.bounds.width)!
self?.view.layoutIfNeeded()
}, completion: nil)
}
}
It work
But when I add .setTitle
#IBAction func signUpAction(_ sender:Any?){
if (signup == false){
signup = true
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
self?.underlineSignup.center.x -= (self?.underlineSignup.bounds.width)!
self?.confirm.setTitle("Đăng ký", for: .normal)
self?.view.layoutIfNeeded()
}, completion: nil)
}
}
#IBAction func signInAction(_ sender:Any?){
if (signup == true){
signup = false
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
self?.underlineSignup.center.x += (self?.underlineSignup.bounds.width)!
self?.confirm.setTitle("Đăng nhập", for: .normal)
self?.view.layoutIfNeeded()
}, completion: nil)
}
}
It stuck, only the title of button confirm change, the underline doesn't move
Please anyone can explain this situation.
EDIT:
The animation working but it's destination is always the first place which under Đăng ký ( animation come from left or right of it, the result always first place )
I think I knew how it is. Because in setTitle has updateFrame Constraintlayout of view. I have a contraintlayout when build button.
When I setTitles it reset the view using constraint.
That's what I think about this.
Try putting 'setTitle' into completion.
UIView.animate(withDuration: 0.15, delay: 0, options: .curveLinear, animations: {
self?.underlineSignup.center.x -= (self?.underlineSignup.bounds.width)!
self?.view.layoutIfNeeded()
}){
//completion
self?.confirm.setTitle("Đăng ký", for: .normal)
}

Swift animate UIButton background images

I have a UIButton that shows an image. When the button is tapped I want to show a checkmark and then animate back to the original image. I've never worked with animation so hopefully, you can help. With the code below I do get 1 and 2 in my debug window, but the "1st" image doesn't show up or displays way too fast. I have played with all the value and can't seem to get the proper values. So basically, you tap the button, circle_150px_green filled copy shows (checkmark), what a short duration, and original button image out of country shows...
Thanks in advance.
UIView.animateKeyframes(withDuration: 25.0, delay: 0.0, animations: {
//add keyframes
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 10.0, animations: {
print("1")
sender.setImage(#imageLiteral(resourceName: "circle_150px_green filled copy"), for: .normal)
})
UIView.addKeyframe(withRelativeStartTime: 5.0, relativeDuration: 0.5) {
print("2")
sender.setImage(#imageLiteral(resourceName: "out of country"), for: .normal)
}
}, completion: nil)

Swift animation - Touchdown doesn't work well

I'm trying to create de same effect than the app "Music" :
When I click on a button, there is a view behind and when the button is not focused anymore, the view is hidden. I do this with TouchUpInside and TouchDown funcs.
#IBAction func pressed(_ sender: UIButton) {
UIView.animate(withDuration: 0.25, animations: {
self.backgroundMoreView.alpha = 0.0
self.backgroundMoreView.transform = CGAffineTransform(scaleX:
1.2, y: 1.2)
sender.transform = CGAffineTransform.identity
}) { (_) in
self.backgroundMoreView.transform = CGAffineTransform.identity
}
}
#IBAction func unpressed(_ sender: UIButton) {
UIView.animate(withDuration: 0.25) {
self.backgroundMoreView.alpha = 0.3
sender.transform = CGAffineTransform(scaleX: 0.8, y:
0.8)
}
}
The problem is that, when I click and hold focus, and then I swipe out of the button, the function unpressed() is not called and the button stay "focused".
I don't know how to fix it.
This should solve the problem:
button.addTarget(self, action: #selector(unpressed(sender:)), for: .touchUpOutside)
Or connect it using storyboard to the same action (unpressed) with touchUpOutside