I want to have a collection view with randomly sized cells but each cell is circle. An example mockup is shown below.. The question is
how would I go about doing this?
Edit: The design doesn't have to be a UICollectionView, I just assumed this would be the best way to do it.. I'm definitely open to any way about doing this.
I also think using a UICollectionView is a good idea. You would definitely have to implement a custom flow layout to be able to organize these random sized cells. Maybe taking a look at CCHexagonFlowLayout and MosaicLayout could help. As for drawing the circles inside the cells, you could have a square UIView in the cell and set circleView.layer.cornerRadius to half of its height.
The collection view is a pretty good idea. Using the flow layout, you can implement collectionView:layout:sizeForItemAtIndexPath:. Answer a random square size (see arc4random_uniform()).
Within each cell, inscribe a circle. The simplest way to do that (at least for a start) is a UIImageView whose frame fills the cell bounds and whose content mode is set to UIViewContentModeScaleAspectFit. All you need then is a picture of a circle.
Related
I want to make a kind of grid layout (not scrollable) in an iOS app, similar to this:
I can do it using multiple UIView but that won't be a good solution. If I use `UICollectionView', I believe it will become scrollable? I want fixed grid items, which resize them according to screen size.
Any guidelines please?
Thanks!
You should use collection view with scrolling and bouncing disable.
It is all about your collectionview's flowlayout's item size.
You have to calculate item size receptively your screen's size.
And you can set that item size to flowlayout and can use that flowlayout with your collection view.
For example, You can refer this so post.
If you just want fixed 3x2 layout scaling to any screen size then multiple UIView is a good solution for this and exactly what you should do.
Also make a RestaurantView UIView subclass to configure and display the image/rating/title/description/etc details for each restaurant.
-
If you want the number of restaurants to change depending on screensize then use a UICollectionView with ResturantCollectionViewCell instead.
I have a UICollectionView that has two layout's (list style like uitableview and gallery style which is a full screen horizontal item scroller).
The cell contains a single image and two labels (title & description). In list layout the image is on the left and labels on the right. In gallery layout the image is full width & 50% height with labels beneath. Like this:
The default animation when switching between these two layouts is too simply resize and move the cells to their new positions in the layout. However I want to make a much more specific animation.
Specifically these are the rough steps I want the animation to take when going from list to gallery view:
Fade out all but the first visible cell (aka cell 1)
Fade out the labels in cell 1
Grow the width of the image in cell 1 to 100%
Grow height of cell 1 to full screen (with the image height following the cell growing - to 40% height)
Fade back in the labels for cell 1
Show the other cells (although they should be off screen - so this may not be neccessary)
The steps would pretty much reverse for gallery -> list layout change.
The UICollectionViewFlowLayout is perfect for the layouts that i want to achieve, I just want to have a much finer control over the animation so that I can produce this "stepped animation" (or would this be like keyframe animation?)
It probably doesn't matter much - but i'm using auto layout constraints (via SnapKit) wherever possible and i'd like to maintain that where i can.
Of course I don't expect someone to program all this for me, however Google isn't helping me work out where to start.
Could someone please point me in the right direction of which methods I need to be implementing/overriding on which classes, and maybe a hint of pseudo-code?
According to Apple Documents:
If you want to specify different sizes for your cells, you must implement the collectionView:layout:sizeForItemAtIndexPath: method on the collection view delegate. You can use the provided index path information to return the size of the corresponding item. During layout, the flow layout object centers items vertically on the same line, as shown in Figure 3-2. The overall height or width of the line is then determined by the largest item in that dimension.
https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW1
Now I have a collection view with 2 columns and cells varying in height. The problem is that the smaller cell is centered with the taller cell on its left/right, but not float up with the cell under it floats up too.
How can I make that happen?
What I have:
What I want:
It seems like that I should subclass UICollectionViewFlowLayout, but how should I accomplish this?
The type of collection view layout you desire is called "waterfall layout". The implementation is a little bit tricky, since you need to override the basic behaviour of UICollectionViewFlowLayout.
I suggest you to take a look at this tutorial by Ray Wenderlich for building a waterfall layout from the ground, or - if you desire an already packed library - using a library on GitHub, like CHTCollectionViewWaterfallLayout, WaterfallCollectionView or CollectionViewWaterfallLayout
What you want to achieve is called "staggered layout". It was introduced by Pinterest in their iOS app.
You will need custom collection view layout, this tutorial will explain how. Alternatively, you can use one of the existing solutions, for example CHTCollectionViewWaterfallLayout.
I'm working on an app that is making use of a collecitonview. It's pretty simple as far as the colletionview goes...just displays an array of uiimages in a single-line, horizontal direction view. Here are my questions...
Do I need to have a custom flow layout just to center the images when scrolling? I was able to center the first image the way I want by using insetForSectionAtIndex, however, when I scroll (horizontally), the images are aligned to the left edge of the screen. I was able to fix that by subclassing UICollectionViewFlowLayout and overriding targetContentOffsetForProposedContentOffset. However, I think there has to be a way to do such a simple task without using a custom layout. Also, using this method, while it centers the images when scrolling, it does not allow paging (no snapping the next image in place), which I would prefer. I thought that insetForSectionAtIndex would be called when scrolling through each image, but that does not seem to be the case. Is there any way to force that?
The other question is sort of related to the first...how do I place the cells exactly where I want them? I have the images centered as far as the left and right edges, but I need to move the cells up (- negative) on y axis. Is overriding layoutAttributesForElementsInRect the only way to do this?
Thanks in advance!
In the storyboard, after you add the imageView to the contentView of the cell, use this to center it:
imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
I am working on an iOS app that has a UICollectionView. I would like to make a simple custom layout in which the middle cell on each row is slightly lower than the one on the right and left. I've been looking over tutorials and the apple documentation but most of it is for a much more complicated set up.
Is there is a quick way to stagger the middle cell. Thanks for any input!
Currently the collectionView looks like this...
I simply want the center cells shifted down by 40 pixels or so.
Subclass UICollectionViewFlowLayout and alter the response that the superclass gives so that the cells are positioned where you want them.
Here's an example of a UICollectionViewFlowLayout subclass:
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p466collectionViewFlowLayout2/ch21p748collectionViewFlowLayout2/MyFlowLayout.swift
It shifts the cells left (so that they are left-justified instead of full-justified across the screen). It is not difficult to see how to adapt this to shift certain cells down.