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
Related
I have button animations that are not working on my ScrollView. I have other VCs that are just UIViews and animations work just fine.
So I tried unchecking from the Attributes Inspector and adding...
self.scrollView.delaysContentTouches = false
...in my viewDidLoad, but it had effect.
I also found that tapping does not trigger the animation but holding on the button for a second or more does and haptic presses also trigger the animation.
This is my animation code:
extension UIButton {
func startAnimatingPressActions() {
addTarget(self, action: #selector(animateDown), for: [.touchDown, .touchDragEnter])
addTarget(self, action: #selector(animateUp), for: [.touchDragExit, .touchCancel, .touchUpInside, .touchUpOutside])
}
#objc private func animateDown(sender: UIButton) {
animate(sender, transform: CGAffineTransform.identity.scaledBy(x: 0.95, y: 0.95))
}
#objc private func animateUp(sender: UIButton) {
animate(sender, transform: .identity)
}
private func animate(_ button: UIButton, transform: CGAffineTransform) {
UIView.animate(withDuration: 0.4,
delay: 0,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 3,
options: [.curveEaseInOut],
animations: {
button.transform = transform
}, completion: nil)
}
}
I am able to get this animation working, but it doesn't feel quite right. So I'd rather not use the code below. But it does work. So I'm not sure what in the code above is causing the problem.
extension UIButton {
func pulsate() {
let pulse = CASpringAnimation(keyPath: "transform.scale")
pulse.duration = 0.2
pulse.fromValue = 0.96
pulse.toValue = 1.0
pulse.repeatCount = 0
pulse.initialVelocity = 0.5
pulse.damping = 1.0
layer.add(pulse, forKey: nil)
}
}
I had the same issue and I solved unchecking the delay touch down button in the scrollview's attributes inspector :
ScrollView's attrobutes inspector
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()
}
}
I have a UIView on which I want to change the alpha from 0 to 0.5 when the user opens a slide out menu. When the user taps the darkened area the alpha should go back to 0. Currently, when the menu button is tapped the alpha changes to 0.5 adding a dimming effect to the view. However, a breakpoint and print statement show that when tapping the UIView the line to change the alpha back to 0 runs, but the UI still shows a 0.5 alpha. Everywhere I have looked the code is exactly the same, so I am unsure what I am doing wrong.
let dimView = UIView()
func setupMenuButton() {
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self, action: #selector(showMenu))
navigationItem.rightBarButtonItem = menuButton
}
#objc func showMenu() {
//TODO: present menu and dim background
if let window = UIApplication.shared.keyWindow {
let dimView = UIView()
dimView.backgroundColor = UIColor.black
dimView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissDimView)))
window.addSubview(dimView)
dimView.frame = window.frame
dimView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
dimView.alpha = 0.5
})
}
}
#objc func dismissDimView() {
UIView.animate(withDuration: 0.5, animations: {
self.dimView.alpha = 0
print("dim view is not transparent")
})
}
The dimView created in showMenu is not the same dimView created in the first line. You are creating a brand new dimView in showMenu.
One way to fix this is to not create a new dimView in showMenu, and use the one declared outside instead:
#objc func showMenu() {
//TODO: present menu and dim background
if let window = UIApplication.shared.keyWindow {
// notice I deleted a line here
dimView.backgroundColor = UIColor.black
dimView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissDimView)))
window.addSubview(dimView)
dimView.frame = window.frame
dimView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
dimView.alpha = 0.5
})
}
}
#objc func dismissDimView() {
UIView.animate(withDuration: 0.5, animations: {
self.dimView.alpha = 0
// here I remove the dimView from the window so that it can be added back in the next time showMenu is called
}, completion: { [weak self] _ in self?.dimView.removeFromSuperView() })
}
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?
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 tried also to add touchUpOutside function but no result. I don't know how to fix it.
For me this works (I prefer the exit on leaving the button)
#IBAction func touchDown(_ sender: UIButton) {
UIView.animate(withDuration: 0.25, animations: {
self.background.alpha = 1.0
}) { (_) in
print("do")
}
}
#IBAction func touchDragExit(_ sender: UIButton) {
UIView.animate(withDuration: 0.25, animations: {
self.background.alpha = 0.0
}) { (_) in
print("away")
}
}