Move UIImage horizontally with animation - ios

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...
}

Related

Animating button position change Swift

3 Buttons
Currently I am trying to change the position of 2 buttons by tapping the third one.
`#IBAction func addButton(_ sender: UIButton) {
if checked {
UIView.animate(withDuration: 0.3) {
self.taskButton.center = self.addButton.center
self.habitButton.center = self.addButton.center
sender.transform = CGAffineTransform.identity
}
checked = false
} else {
// sender.setImage(#imageLiteral(resourceName: "CancelButton"), for: .normal)
UIView.animate(withDuration: 0.3) {
sender.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 4.0)
self.taskButton.center = self.taskButtonCenter
self.habitButton.center = self.habitButtonCenter
}
checked = true
}
}`
This method is working only if I'm not using constraints. I don't know why, but if I am using it to run on a different device, the buttons are moving to a crazy position. Please help me find a better solution! Thanks
PS: I'm a begginer and I'm still trying to understand constraints!
Constraints can also be outlets, and you should use them that way for animations if you're using autolayout. For example, I'll assume you position your button with topMargin and leftMargin constraints.
Your hypothetical code for animating the change would look like this then:
topMargin.constant += 50
leftMargin.constant += 50
UIView.animate(withDuration: 0.3) {
view.layoutIfNeeded()
}
The code above would move the object placed by these constraints by 50 points to the right and down (anywhere actually, depending on how you place the constraints).

Is anyone know why the else code part can't hide my colorPicker?

I'm still new with Swift 4 and i found this on the internet & tried to make this Button to show "something" when the image button is A. But when i clicked the Button again, only its image button changed but the "something" still not hidden. Can someone help ?
I already done with other button that using this animation but the button is showing another button from Library.
But this 1 is different, not showing another button from Library but showing ChromaColorPicker
var sizeOff = UIImage(named: "Brush-Size")
var sizeOn = UIImage(named: "Brush-Size-On")
============================ somewhere else ======================
extension ViewController {
#IBAction func BrushColourClicked(_ sender: UIButton) {
if sender.currentImage == ColourOn {
sender.setImage(ColourOff, for: .normal)
} else {
sender.setImage(ColourOn, for: .normal)
} // Image first set
configureUI()
}
func configureUI() {
let colorPicker = ChromaColorPicker(frame: CGRect(x: 25.0, y: 410.0, width: 140.0, height: 140.0)) // Position & Size of Color Picker
ColourButtonCenter = colorPicker.center
colorPicker.center = BrushColour.center
colorPicker.delegate = self
colorPicker.padding = 5.0
colorPicker.stroke = 3.0
colorPicker.hexLabel.isHidden = true
colorPicker.layout()
view.addSubview(colorPicker)
colorPicker.alpha = 0
if BrushColour.currentImage == ColourOn {
UIView.animate(withDuration: 0.35, animations: {
colorPicker.alpha = 1
colorPicker.center = self.ColourButtonCenter
})
} // Animation show
else {
UIView.animate(withDuration: 0.35, animations: {
colorPicker.alpha = 0
colorPicker.center = self.BrushColour.center
})
} // Animation hide
}
}
i dont know if
colorPicker.alpha = 0
is working or not
Every time you call configureUI you're adding a new colour picker to the view, then animating the alpha on it.
So when you're trying to hide the colorPicker what's actually happening is that you're adding a new colour picker with alpha 0 and animating it to alpha 0 (doing nothing), the previous colorPicker will still be visible so it will look as though nothing as changed.
Create a variable for colorPicker once and add it to the view, then configure that instance in your configureUI function.

Use SegmentController to Make TableView Disappear and UIContainerView Appear

I'm trying to use a segment controller to switch between my tableView and a container view, but when I try to switch between them it only half works. The TableView appears and disappears, but the container view never appears.
Here is my code:
#IBAction func switchAction(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
profileTableView.isHidden = false
modelsContainerView.isHidden = true
} else {
profileTableView.isHidden = true
modelsContainerView.isHidden = false
}
}
UPDATE
If i use this code the simulation sort of works. The container view appears but it doesn't fill the screen like the tableview did.
#IBAction func switchAction(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
UIView.animate(withDuration: 0.5, animations: {
self.profileTableView.alpha = 1
self.modelsContainerView.alpha = 0
})
} else {
UIView.animate(withDuration: 0.5, animations: {
self.profileTableView.alpha = 0
self.modelsContainerView.alpha = 1
})
}
}
I can tell it's not working because I've set the container view's background color to pink. And this is what it looks like when I try to switch from TableView(which works) to container View:
All of the outlets appear to be connected. And my UI set up is a green view behind the segment controller, with a tableView below and a containerView that in the same place.
Thank you very much for your help in advanced.
Try this approach...
Seg Background view is 45-pts height, and pinned top, leading, trailing all equal to 0.
Profile Container is pinned leading, trailing, bottom all equal to 0, and the top is pinned to the bottom of Seg Background.
But you can't see Profile Container (red background), because Models Container (orange background) is on top of it, and...
Models Container is equal width and height, and centered Horizontally and Vertically, all to Profile Container.
Profile Container has Profile Table VC embedded in it.
Models Container has Models VC embedded in it.
The idea is:
When Seg 0 is selected, Profile Container is alpha 1 and not hidden, while Models Container is alpha 0 and is hidden.
When Seg 1 is selected, Profile Container is alpha 0 and is hidden, while Models Container is alpha 1 and not hidden.
class SegContainerViewController: UIViewController {
#IBOutlet weak var profileContainerView: UIView!
#IBOutlet weak var modelsContainerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// start with Profile visible
// so hide Models and set its alphs to 0
self.modelsContainerView.alpha = 0
self.modelsContainerView.isHidden = true
}
#IBAction func switchAction(_ sender: UISegmentedControl) {
// on segment select, the "other" container will be
// transparent and hidden, so
// un-hide it, then animate the alpha for both (for cross-fade)
// on animation completion, hide the now transparent container
if sender.selectedSegmentIndex == 0 {
self.profileContainerView.isHidden = false
UIView.animate(withDuration: 0.5, animations: {
self.profileContainerView.alpha = 1
self.modelsContainerView.alpha = 0
}, completion: { (finished: Bool) in
self.modelsContainerView.isHidden = true
})
} else {
self.modelsContainerView.isHidden = false
UIView.animate(withDuration: 0.5, animations: {
self.profileContainerView.alpha = 0
self.modelsContainerView.alpha = 1
}, completion: { (finished: Bool) in
self.profileContainerView.isHidden = true
})
}
}
}
Edit:
To access the Embedded View Controllers, override prepareForSegue:
var theProfileVC: ProfileTableViewController?
var theModelsVC: ModelsViewControler?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ProfileTableViewController {
// do something here if desired, like setting a property of the VC
// save a reference so we can use it later
theProfileVC = vc
}
if let vc = segue.destination as? ModelsViewControler {
// do something here if desired, like setting a property of the VC
// save a reference so we can use it later
theModelsVC = vc
}
}
I've also updated the GitHub repo with an example of this.
I put this up as a sample project, if you'd like to dig into it: https://github.com/DonMag/SegmentsAndContainers

Animation doesn't work very well in Swift

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

iPad App Navigation with SlideView - change size of UIView?

i want to create a slide out menu which has a normal width of about 50 pixels, and if the user press the expand button i will also show the labels for the button.
Like in this example:
What is the correct way to create such a menu? I though about using 2 views and set the size of contentview to width-50pixels.
But i am unable to change the frame of my UIView in the ViewDidLoad function. (this is an example)
override func viewDidLoad() {
super.viewDidLoad()
self.view.frame = CGRect(x:50, y:0, width:974, height:768)
sidebarIsOpen = false
}
And if the user click on the expand Button
#IBAction func expandButtonClicked(sender : AnyObject) {
var x = self.sidebarIsOpen! ? 50 : 300
UIView.animateWithDuration(0.2, animations: {
self.view.frame = CGRect(x:x, y:0, width:300, height:768)
}, completion: { _ in
self.sidebarIsOpen = !(self.sidebarIsOpen!)
})
}
If i click the button again, everything is fine. But on ViewDidLoad i am unable to move the contentview to right.
Thanks in advance
Ill found already the solution by myself.
Created 2 views and animate the "contentview" on button click.
#IBOutlet weak var menuView: UIView!
#IBAction func toggleMenuButton(sender: UIBarButtonItem) {
var x = self.sidebarIsOpen! ? 50 : 200
UIView.animateWithDuration(0.2, animations: {
self.contentView.frame = CGRect(x:x, y:0, width:974, height:768)
}, completion: { _ in
self.sidebarIsOpen = !(self.sidebarIsOpen!)
})
}

Resources