I am trying to animate the first apparition of a cell in a collection view as simple as a ZoomOut + Fade IN of the cells when the collection view is first called.
I have NOT subclassed UICollectionViewFlowLayout so far as I did not think it was necessary for such small action and I do not need to further invalidate the layouts once the cells are displayed. There is actually no animations of the cell beyond their first apparition. Reading the apple's developper guidelines on the subject seems to confirm I should not go to the subclass route but still I have not found how to perform the animation I want.
I have played along the lines of defining the cells using UICollectionViewDelegateFlowLayout methods then performing a CGAffineTransform sequence in collectionView(_ collectionView: willDisplay cell: forItemAt indexPath: IndexPath) of UICollectionViewDelegate and the proper transformation does occur. Then I was thinking of reversing this transformation by calling a simple CGAffineTransform.Identity() in an animation but I have not found yet where (in which of the CollectionView classes method) to place it.
Thank you in advance for your king help!
Related
I am wondering what some of the differences are between invalidateLayout() and reloadData()
I have read the documentation here and here but am left with the following questions:
I have data as an array such that cellForItemAt indexPath can reference the array directly with dataArray[indexPath.item] and each data object in the array can populate the cell. reloadData() will ensure that when a change is made to the dataArray this is reflected in the collectionView. With some minor testing it appears that invalidateLayout() will also observe this change. Is this always true?
Both functions appear to have the same effect with regards to resizing the cells, is this always true?
When invalidateLayout() is called, will cellForItemAt indexPath also be called for each cell as with reloadData()?
If all the above are true, then would you ever even need to use reloadData() since Apple says to use it sparingly?
Any other comments on the differences between the two functions on a fundamental level would be helpful (when to use each one, etc)
After further testing...
cellForItemAt indexPath will be called only for cells which were not on screen but are now on screen as a result of the invalidateLayout().
This appears to be true
See answer to question 1. This could cause an issue if you have moved cells around and the data in each cell is not in the same order as the data source.
I am now using reloadData() a lot less
In general, it looks like reloadData() also calls invalidateLayout() (or at least has a similar effect) and will trigger cellForItemAt indexPath for every cell. invalidateLayout(), however, will only call cellForItemAt indexdPath if the cell is now within the screen bounds and needs to be reloaded as a result of the layout change.
I created collectionView as below:
I would like to create a collection view as same as this layout, with one biggest header cell like the one with red iPhone7. Now I wonder which approach is better, to create an extra cell or handle this in UICollectionViewFlowLayout?. My data is an array which fetched from JSON, so i would like to make the big cell is the first item.
Honestly, I see that second approach is quite complicated.
So can anyone help me to find best solution for this.
Thank you so much for reading this and have a nice day ahead.
Go to attribute inspector of UIColectionView.
From Accessories select Section Header
Once header view is visible , add your views to it.
Then in following method tweak accordingly:
override func collectionView(_ collectionView: UICollectionView ,viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader{
return headerView
}
}
I am still fairly new to programming and I do not quite understand UICollectionViews. Can a programmer add buttons in the cells of an UICollectionView? Or only UIImages? I added a button into one of the cells and when I ran the iOS simulator nothing appeared. I also just dragged in an UICollectionView and I am not using an UICollectionView view controller. I dragged the UICollectionView into code as an outlet too and constrained the view, cell and button. Attached is a screenshot of my view controller and iOS simulator. Thanks!!
I was recently new to collection views, and they can be confusing. I found a video tutorial that explains it all really well, but it was not updated for Swift 3 so I wrote made this GitHub project to help explain it to new users.
You can certainly add buttons to to the cells of a UICollectionView. Each cell is like a little UIView in and of itself. However, I believe you need to create a new class for the cell and create an IBOutlet for the button. You also need to create a couple of functions inside your main ViewController.swift file to actually view the UICollectionView. It can be complicated at first, but stick it out! In a little while, you'll be able to do them with ease. Follow the tutorial in a new project to practice, then you might figure out what the problem is.
Sorry that that last one was unhelpful, I'm new to this. Thanks for the tip, manetsus!
I created a new project and tried to add a button to the UICollectionViewCell. Here's what I did:
I added a button to the cell, then created an IBAction by control+dragging from the button to ViewController.swift. I ran it, and it worked. Each button within the UICollectionView cells performs the IBAction in ViewController.swift.
If you want cell-specific actions, consider using:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
//Do something with the cell...
}
I've created a UICollectionView with an image in each cell. I'm wondering how I can make a trigger action which will make the image fullscreen like in facebook, intagram or other apps. What is the best way in Swift to achieve this?
the simplest solution in your case would be to put some code in
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
}
in this function you already have selected indexPath.row so it should be easy to access the right image in your datasource. for example, if you are using array of images imagesArray, the right image would be imagesArray[indexPath.row].
after that you can create new UIViewController with UIImageView to show this image, or just UIImageView that will be shown over this collection view.
(I think you should also add UICollectionViewDelegate and UICollectionViewDatasource to your current controller for this to work).
The simple solution is that you can add an gesture detection on the image view inside the collection view.
I have an image inside my tableview cell.
What I want to do is when I click on that cell I want to parse both text & image to detail view.
I can do for text but I can't get the point about image.
So, simply my question is how can I parse image inside tableview cell to detail view in iOS.
Thanks
You can use it as the same way you're using it. Use indexPath to specify the row you wanna manipulate.
Here's how you do it. implement UITableViewDelegate in your ViewController, and implement the following method.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
The short answer, you access the image the same way that you set it. In the case of the standard UITableViewCell, it is the imageView property that you used.
For these API type questions though, there is no better place to start, than the documentation