How to change UICollectionView height on button click - ios

I’m trying to change height of a collectionview and a view when I click a button. But when I click the button the height of the view changes but not the height of the collectionview.
Here is my code:
#IBAction func button(_ sender: Any) {
UIView.animate(withDuration: 1, animations: {
self.backView.frame.size.height -= 160
self.collectionView.frame.size.height -= 160
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut,
animations: self.view.layoutIfNeeded, completion: nil)
}, completion: nil)
}
What can I do to also resize the collectionview?

If you used constraints on that collection view, you need to change the constant of the height constraint, and not its frame.
Like this:
#IBAction func button(_ sender: Any) {
UIView.animate(withDuration: 1, animations: {
self.backView.frame.size.height -= 160
self.collectionViewHeightConstraint.constant = <value>
self.view.layoutIfNeeded()
}, completion: nil)
}

Related

Animation not working in swift

I have a button and a collection view (horizontal) inside keyboard input view. Auto Layout is used. The button is hidden by default using Leading Constraint set to -50. When user start using collection view and contentOffset.x of collection view is greater than 80, The button will show. The code is working fine but animation is not working.
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if self.collectionView.contentOffset.x > 80 {
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseIn, animations: {
self.sideButtonLeadingConstraint.constant = 0
self.view.layoutIfNeeded()
}, completion: nil)
} else {
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseIn, animations: {
self.sideButtonLeadingConstraint.constant = -50
self.view.layoutIfNeeded()
}, completion: nil)
}
}
}
First, you shouldn't change you constraints inside animations block. Second, scrollViewDidScroll method is called a large number of times, and you should set some limitations for calling animation code inside it. Try something like this:
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let needsShow = collectionView.contentOffset.x > 80 && sideButtonLeadingConstraint.constant != 0
let needsHide = collectionView.contentOffset.x <= 80 && sideButtonLeadingConstraint.constant != -50
if needsShow {
sideButtonLeadingConstraint.constant = 0
} else if needsHide {
sideButtonLeadingConstraint.constant = -50
}
if needsShow || needsHide {
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseIn, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
}
Try as follows by updating the constant outside the animation block. It will do the update with animation effect.
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if self.collectionView.contentOffset.x > 80 {
self.sideButtonLeadingConstraint.constant = 0
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseIn, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
else {
self.sideButtonLeadingConstraint.constant = -50
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseIn, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}}

hide/show uiimageview with animation when clicking button

how can I implement that when I click on button, my Image moves out of the screen with animation and when I click again on the button , comes back to its place?
On the button click event, simply animate the x coordinate of origin of the imageView according to your requirement.
Example:
#IBAction func onTapButton(_ sender: UIButton)
{
if sender.isSelected
{
UIView.animate(withDuration: 2.0, animations: {
self.imageView.frame.origin.x = 0.0
})
}
else
{
UIView.animate(withDuration: 2.0, animations: {
self.imageView.frame.origin.x = UIScreen.main.bounds.width
})
}
sender.isSelected = !sender.isSelected
}
The above code will work as follows:
when selecting button, imageView's x coordinate of origin is moved to extreme right of the screen (UIScreen.main.bounds.width)
when de-selecting button, imageView's x coordinate of origin is moved to extreme left of the screen (0.0)
Use this code to move left-
func moveToLeft()
{
var frame = imageView.frame
frame.origin.x = -imageView.frame.size.width //Adjust this value according to your need
UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in
self.imageView.frame = frame
}, completion: {(_ finished: Bool) -> Void in
/*done*/
})
}
Use this code to move right-
func moveToRight()
{
var frame = imageView.frame
frame.origin.x = 0 //Adjust this value according to your need
UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in
self.imageView.frame = frame
}, completion: {(_ finished: Bool) -> Void in
/*done*/
})
}

How to reset animation in iOS?

Here is my code:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
cloud.center.x -= view.bounds.width
cloud1.center.x -= view.bounds.width
cloud2.center.x -= view.bounds.width
cloud3.center.x -= view.bounds.width
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 12) {
self.cloud.center.x += self.view.bounds.width*2
UIView.animate(withDuration: 12, delay: 9, options: [],
animations: {
self.cloud1.center.x += self.view.bounds.width*2
},
completion: nil
)
UIView.animate(withDuration: 12, delay: 18, options: [],
animations: {
self.cloud2.center.x += self.view.bounds.width*2
},
completion: nil
)
UIView.animate(withDuration: 12, delay: 22, options: [],
animations: {
self.cloud3.center.x += self.view.bounds.width*2
},
completion: nil
)
}
}
I have got a redo which resets everything in the page apart from my animation which just stays in one position. How can I reset the animation in order for it to re-play the animation again?
Your animation code moves your 4 cloud views to the right by self.view.bounds.width*2, by adding that amount to their center positions.
To undo that change, simply subtract the same amount.

Label animation in Swift

I want to create a transition effect on my label so that it comes out from the left side of the view and centre itself in the middle of the screen. I am doing:
override func viewDidLoad() {
super.viewDidLoad()
label.center.x -= view.bounds.width
}
to place the label out of the view, but this is not working as the label is still present in the view. Please help.
Because label.center.x -= view.bounds.width will make it appear at the left of the view, so you can't see any effect on it.
You can use UIView.animateWithDuration to make animation effect:
UIView.animateWithDuration(0.5, delay: 0, options: [.CurveEaseOut], animations: {
label.center.x += view.bounds.width
self.view.layoutIfNeeded()
}, completion: nil)
And you should put them in your viewDidAppear instead of viewDidLoad, so everytime your view is resume, it will animate your label. Your code will look like this:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
label.center.x = view.center.x // Place it in the center x of the view.
label.center.x -= view.bounds.width // Place it on the left of the view with the width = the bounds'width of the view.
// animate it from the left to the right
UIView.animateWithDuration(0.5, delay: 0, options: [.CurveEaseOut], animations: {
label.center.x += view.bounds.width
self.view.layoutIfNeeded()
}, completion: nil)
}
This will make your label appear from the left to the right.
SWIFT 5
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
label.center.x = view.center.x // Place it in the center x of the view.
label.center.x -= view.bounds.width // Place it on the left of the view with the width = the bounds'width of the view.
// animate it from the left to the right
UIView.animate(withDuration: 1, delay: 0, options: [.curveLinear], animations: {
self.label.center.x += self.view.bounds.width
self.view.layoutIfNeeded()
}, completion: nil)
}
Here is a solution for Swift 3
UIView.animate(withDuration: 1.0) {
label = self.view.frame.size.width / 2
}
make sure that the label is initially positioned away from the screen.

how to Scroll(horizontally) in collection view on button press?

I have a collection view with images.I want to perform scrolling on button press . i have searched on google I found out only to set contentoffset but I want to scroll with animations(scroll should bounce if content is past the scrolling area).I have added a image to show you what exactly i have to do, in picture there is a collection view and little arrows on both sides are buttons.
#IBAction func moveScrollLeft(sender: UIButton) {
UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.imageCollectionView.contentOffset.x -= 50
}, completion: nil)
print(imageCollectionView.contentOffset.x)
}
#IBAction func moveScrollRight(sender: UIButton) {
UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.imageCollectionView.contentOffset.x += 50
}, completion: nil)
print(imageCollectionView.contentOffset.x)
}
Use UICollectionview's scrollToItemAtIndexPath(_:atScrollPosition:animated:)
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/occ/instm/UICollectionView/scrollToItemAtIndexPath:atScrollPosition:animated:
For example:
let indexPath = NSIndexPath(forRow: 3, inSection: 0)
imageCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .CenteredHorizontally, animated: true)
Please check this code i have modify your code:
#IBAction func moveScrollLeft(sender: UIButton) {
self.imageCollectionView.contentOffset.x -= 50
UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
}, completion: nil)
self.view.layoutIfNeeded()
print(imageCollectionView.contentOffset.x)
}
#IBAction func moveScrollRight(sender: UIButton) {
self.imageCollectionView.contentOffset.x += 50
UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
print(imageCollectionView.contentOffset.x)
}

Resources