I have a view (grey background) and another over it with a UITextField.
When I tap on the grey background, I want the view with the UITextField to disappear with an animation (move to top)
When I tap on the return key (virtual keyboard) the animation is smooth, but when I tap on the grey background, it disappears without animation. When I comment out the vueRecherche.isHidden = true in the completion, it's ok, but I want to hide it!
here is my function
private func fermeRecherche() {
if self.constraintVueRecherche_Top.constant == -5 {
return
}
self.txtRercherche.endEditing(true)
UIView.animate(withDuration: 0.3, animations: {
self.constraintVueRecherche_Top.constant = -5
self.vueFondGris.alpha = 0.0
self.view.layoutIfNeeded()
}, completion: { termine in
self.vueRecherche.isHidden = true
self.vueFondGris.isHidden = true
})
}
with this call, the animation is smooth :
func textFieldDidEndEditing(_ textField: UITextField) {
fermeRecherche()
}
but not with this one :
let tap=UITapGestureRecognizer(target: self, action: #selector(self.tapFondGris(_:)))
vueFondGris.addGestureRecognizer(tap)
func tapFondGris(_ sender: UITapGestureRecognizer) {
fermeRecherche()
}
any ideas?
Thanks
Related
I want to move UIImageView horizontally from left to right and right to left.
I designed this as below.
UIView (green color)
Arrow Image (Leading of UIView, Center vertically of UIView, Fix Height, Width)
UILabel (Center Horizontally & center Vertically of UIView)
Button (Leading, Top, Trailing, Bottom of UIView)
In above image, Green color is UIView and the left arrow icon is image. so when user press the button i want to move Arrow Image from Left to Right and Right to Left vice versa.
EDIT 1
Thanks for your answer
if sender.isSelected {
UIView.animate(withDuration: 1.0, animations: {
self.imgVWInOut.frame.origin.x = (self.vwPunchInOut.bounds.maxX - self.imgVWInOut.bounds.width) - 10
}) { (done) in
}
} else {
UIView.animate(withDuration: 1.0, animations: {
self.imgVWInOut.frame.origin.x = 10
}) { (done) in
}
}
But when i try to change UIView background color and UIImageView image then animation not working proper.
#IBAction func btnPunchInOutTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
UIView.animate(withDuration: 1.0, animations: {
self.imgVWInOut.frame.origin.x = (self.vwPunchInOut.bounds.maxX - self.imgVWInOut.bounds.width) - 10
}) { (done) in
self.imgVWInOut.image = #imageLiteral(resourceName: "ic_punchout")
self.lblPunchInOut.text = "Punch out".localized()
self.vwPunchInOut.backgroundColor = Colors.punchOutColor
}
} else {
UIView.animate(withDuration: 1.0, animations: {
self.imgVWInOut.frame.origin.x = 10
}) { (done) in
self.imgVWInOut.image = #imageLiteral(resourceName: "ic_punchout")
self.lblPunchInOut.text = "Punch out".localized()
self.vwPunchInOut.backgroundColor = Colors.punchOutColor
}
}
}
Requirement
Default look likes this.
enter image description here
When user press button it will look like this after animation done.
enter image description here
GIF Link : https://drive.google.com/file/d/1R2hfcwfhyO5JA9CQt1_6Auto3YBRj3yn/view?usp=sharing
Demo project Link :https://drive.google.com/file/d/1H0b3D61fPIxWqSZL8tdvp0RP8juVdr_Z/view?usp=sharing
How can i achieve this. will you please help me for that.
Thanks
You need to animate the frame of arrowImageView w.r.t to the customView(green color view), i.e.
#IBAction func onTapButton(_ sender: UIButton) {
self.arrowImageView.frame.origin.x = self.customView.bounds.minX
UIView.animate(withDuration: 1.0) {
self.arrowImageView.frame.origin.x = self.customView.bounds.maxX - self.arrowImageView.bounds.width
}
}
Give the animation duration as per your requirement.
EDIT:
You need to change the isSelected state of sender on button tap,
#IBAction func onTapButton(_ sender: UIButton) {
sender.isSelected = !sender.isSelected //here...
//rest of the code...
}
Using UITextFieldDelegate I have two function that controls what happens the textfield begins editing and ends.
Using UIView.animate I am having a button disappear and hide using the .isHidden and .alpha property.
.alpha is being used because I also animate the button disappearing with a slight delay.
While the disappearing works well, upon using textFieldDidEndEditing the animation does not work, the button does come back but it does so abruptly with no ease.
To dismiss the keyboard I allow the user to tap anywhere by using this in ViewDidLoad()
self.view.addGestureRecognizer(UITapGestureRecognizer(target:
self.view, action: #selector(UIView.endEditing(_:))))
The delegate functions I use are below:
extension ExampleViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == exampleTextField {
UIView.animate(withDuration: 0.1, delay: 0.1, options: .curveEaseOut,
animations: {self.exampleButton.alpha = 0.0},
completion: { _ in self.exampleButton.isHidden = true
})
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField == exampleTextField {
UIView.animate(withDuration: 0.1, delay: 0.1, options: .curveEaseInOut,
animations: {self.exampleButton.alpha = 1.0},
completion: { _ in self.exampleButton.isHidden = false
//Do anything else that depends on this animation ending
})
}
}
}
Why is the .curveEaseIn not working?
The problem is that the button is hidden. You fade it back in while it is still hidden and then in the completion handler you unhide the button which already has an alpha of 1. This makes it suddenly appear after 0.2 seconds.
Since you are hiding using an alpha of 0 and showing with an alpha of 1, there is no need to set the isHidden property at all in either animation.
I've been searching online for an answer to my question but there doesn't seem to be any solutions. I have a UILabel which contains two interchangeable icons (from FontAwesome). I would like to create an animation where it changes the UILabels text from one to the other repeatedly.
What I have so far is an animation which calls itself again and again. It seemed to look fine on the simulator but when I ran it on the phone it didn't work how I wanted it to. I seem to almost be there but my code creates some sort of flash when it fades out
func animateLabel() {
self.runningPersonLabel.text = self.runningPersonLabel.text == "ICON 1" ? "ICON 2" : "ICON 1"
self.runningPersonLabel.sizeToFit()
self.runningPersonLabel.center = modalContainer.boundsCenter
self.runningPersonLabel.frameTop = titleLabel.frameBottom + 40
UIView.animate(withDuration: 2, delay: 0, options: [.autoreverse], animations: {
self.runningPersonLabel.alpha = 1.0
}, completion: { finished in
self.runningPersonLabel.alpha = 0.0
self.animateLabel()
})
}
Try with this class:
import UIKit
#IBDesignable class FadingLabel: UILabel
{
// The secondary text
#IBInspectable var secondaryText:String?
var primaryText:String?
// Animation time, is divided by 2
#IBInspectable var animationTime:TimeInterval = 1
// Set this flag to true to stop animation
var stop = true
// Start the animation
func startAnimating()
{
stop = false
if primaryText == nil
{
primaryText = self.text
}
fadeAnim()
}
// Stop the animation
func stopAnimating(_ sender: UIButton)
{
stop = true
}
#objc private func fadeAnim()
{
if stop
{
return
}
// Fade out
UIView.animate(withDuration: animationTime / 2, animations: {
self.alpha = 0
}) { (complete) in
UIView.animate(withDuration: self.animationTime / 2, animations: {
if self.text == self.primaryText
{
self.text = self.secondaryText
}
else
{
self.text = self.primaryText
}
self.alpha = 1
}, completion: { (complete2) in
self.fadeAnim()
})
}
}
}
Usage:
put a label in your view controller;
set the label class to FadingLabel;
set the secondary text and the animation time in the storyboard inspector
call the methods startAnimating or stopAnimating as needed.
I have a text field where user should enter info. And a label which points user to text field (like a hint).
I want to stop animation and remove hint label once user presses the text field to enter data.
There is repeating animation on text label. Was created by:
override func viewDidLoad() {
super.viewDidLoad()
textInput.addTarget(self, action: #selector(CalculatorViewController.removeAnimation(_:)), forControlEvents: UIControlEvents.TouchDown)
self.hintLabel.alpha = 0.0
UIView.animateWithDuration(1.5, delay: 0, options: .Repeat
, animations: ({
self.hintLabel.alpha = 1.0
}), completion: nil
)
After it I have created a function to remove annotation
func removeAnimation(textField: UITextField) {
view.layer.removeAllAnimations()
self.view.layer.removeAllAnimations()
print("is it working?!")
}
Should work according to documentation.
My label keeps flashing even though I see the string printed in console. I guess problem is that animation is repeated but have no clue how to resolve this issue.
//Just remove the animation from the label. It will Work
func remove()
{
self.hintLabel.layer.removeAllAnimations()
self.view.layer.removeAllAnimations()
self.view.layoutIfNeeded()
}
Update:
If you want to go nuclear, you could do this, as well:
func nukeAllAnimations() {
self.view.subviews.forEach({$0.layer.removeAllAnimations()})
self.view.layer.removeAllAnimations()
self.view.layoutIfNeeded()
}
When I start typing in uitextfield i want some animation to happen and it should not reverse back until i finish typing. I am using this code to animate:
func textFieldDidBeginEditing(textField: UITextField) {
println("begin")
UIView.animateWithDuration(1.0, animations:{
self.secondTextfield.frame = CGRectMake(self.secondTextfield.frame.origin.x + 500, self.secondTextfield.frame.origin.y, self.secondTextfield.frame.size.width, self.venueTextField.frame.size.height)
})
}
What I want to do is: when i start typing in first textfield, i want the second text field to hide out from view and when i finish typing, i want it to animate back in.
What my issue is: When I start typing the animation happens and it comes back to the original position.It doesn't wait for me to finish typing.
Why not change alpha to 0, intead change its frame?
You need to identify witch textfield is editing.
You need to deal with textFieldDidEndEditing to get second textField back.
Example:
func textFieldDidBeginEditing(textField: UITextField) {
if textField == firstTextField {
UIView.animateWithDuration(1.0, animations:{
self.secondTextfield.alpha = 0
})
}
}
func textFieldDidEndEditing(textField: UITextField) {
if textField == firstTextField {
UIView.animateWithDuration(1.0, animations:{
self.secondTextfield.alpha = 1
})
}
}
While not knowing exactly what the problem is, I've found a reasonable alternative to solving your problem:
Instead of animating your frame, animate the auto-layout constraint that defines your textField's x coordinate. For instance, the image below consists of two textFields. I want to animate the bottom one when I begin typing in the top one:
The vertical constraint is a centre x alignment that I want to animate. To achieve the effect, I connected an #IBOutlet to my NSLayoutConstraint:
I had my view controller adopt UITextFieldDelegate and implemented three methods:
extension ViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(textField: UITextField) {
self.c.constant += 100 //c is my constraint
UIView.animateWithDuration(1.0) {
self.view.layoutIfNeeded()
}
}
func textFieldDidEndEditing(textField: UITextField) {
println("textFieldDidEndEditing")
self.c.constant -= 100
UIView.animateWithDuration(1.0) {
self.view.layoutIfNeeded()
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
a.resignFirstResponder()
return true
}
}
This method needs a bit of auto-layout know how, but I was able to achieve the effect you desired.