Run method on first set of UICollectionViewCells, but no on subsequent cells - swift

I have a UICollectionView containing cells that are showing a preview of the camera. I've added a black UIView as a "veil" on top of the cell through storyboard.
When the camera previews begin, I animate the veil UIViews of all cells from an veil.alpha = 1 to veil.alpha = 0. This smooths out the effect of the camera preview starting.
However, this means that when a cell is initialized, but not added to the view, I have to wait until it's added to the view before I can remove the veil UIView. If I attempt beforehand, the veil UIView is nil.
The result is that as I'm scrolling, I catch a glimpse of the veil being animated away for any cells that have not been initialized beforehand.
A simple solution would be to check something like:
"If this cell is being scrolled into visibility, and the camera is already running, before the cell is visible, just set the
veil.alpha = 0."
However, I'm not sure where in the cell/controller lifecycle to put this.
I've tried setting this in prepareForReuse(), layoutSubviews() in the cell, and in cellForItemAtIndexPath in containing controller. All three of those methods cause the veils of the initial visible cells to immediately jump to veil.alpha = 0 without the animation.
Thank you.

Related

tvOS UITableView in UINavigationController causes strange fading behavior (UIView.mask)

I ran into a strange situation when a UITableView is used within the context of a UINavigationController.
tvOS uses UIView.mask to apply a "fade-out gradient" at the top and bottom of a UITableView, so that cells fade into and out of existence at the top and bottom edges of the table view.
That's fine: the fading mask always stays out of the way of the selected cell.
(Here, view.backgroundColor is set to red and tableView.backgroundColor is set to blue with 50% alpha. The constraints of the tableView are set to the safe area.)
The problem comes when you put your view controller inside of a UINavigationController. When selection is near the top, the mask view no longer seems to avoid the cell, so it looks faded. Additionally, as the user scrolls down, the fading mask takes a giant jump downward, and then as the user starts to scroll back up, that fading mask doesn't seem to get out of the way:
For reference, here is the same setup but with tableView.mask = nil:
(All fading is disabled, but you can see the cells "pop" into and out of existence at the top and bottom of the tableview. You might think you could just set tableView.masksToBounds = true, but then the selected cell gets chopped off because it grows when selected)
Surely I'm missing something obvious here? Did no one at apple put a tableview inside of a nav controller?
You were so close! The clips to bounds was missing.
tableView.clipsToBounds = true
tableView.mask = nil
Cheers :)

Animations set in collectionView keep reloading

I have a collectionView and each cell has a graph that animates once the collectionView loads. The animation is based on CAShapeLayer animation and the value is being passed from the collectionView.
The animation works somewhat fine for most cells. The two things that I am running into are:
1) When I scroll down and new cells appear, the animation for those newly appeared cells begin, but the progressLabel only appears when the animation is finished (which is odd).
2) When I scroll back to top, cells that previously already animated will partially animate again.
Is there a way to have all cells animate once the view controller loads and prevent any additional animations?
This is a follow-up to my previous question, which has the code I am using for this: Setting toValue for CAShapeLayer animation from UICollectionView cellForItemAt indexPath

Avoid cell autoresizing in editing mode iPhone

I have a problem with my custom cells.
My cell is structured like this image:
Gray view = cell.contentView
Orange view = a button added as a subview to the contentView
Yellow view = a label added as subview to the contentView
Blue view = an image view added as subview to the contentView
Green view = accessory view
The problem is that when I toggle edit to tableView, my cell indents hiding accessory view and moving right everything else. I want to avoid this.
When I toggle editing mode, my cell mustn't move its content, the editing control (minus red button on the left) must take the place of the button inside the contentView and this last one should hide itself.
I tried tableView:shouldIndentWhileEditingRowAtIndexPath: method without success because my tableView is plain. I also tried to override willTransitionToState: method but I don't know how to do what I want.
Can someone help me?
Thank you so much!
How have you added the subviews to the cell? If you add them directly to the cell instead of adding them to the content view, they won't get moved. It is the content view that is resized when the cell transitions to editing mode.
If you want to hide certain subviews, you should override setEditing:animated: in the cell subclass and hide / show as appropriate.
You may also need to add your subviews such that they are below the contentView in the view hierarchy, or bring the content view to the top after you have finished, using the bringSubviewToFront: method.
If you really don’t want the cell to resize, you could implement -layoutSubviews thusly:
-(void)layoutSubviews
{
if ([self isEditing] == NO) {
// Lay out subviews normally.
}
}
When you go into edit mode, -layoutSubviews won’t follow the code path that lays out your views. This is assuming you’ve subclassed UITableViewCell.

4 directional scrollview with paging

I need to implement a scroll view which (on a button press) will page either up, left, down or right depending on which button was pressed. Also the user can indefinately page in the same direction which will load the views in a kind of carousel. So I have 3 viewControllers.... viewController 1 is shown first....The user presses left, it shows viewController2, left again shows viewController3, left again is back to viewController 1 etc. and the same for up,down,right.
Does anyone know a good way to implement this? Im open to all suggestions.
Many thanks
Jules
Edit - second attempt at a clear explanation:
Consider this matrix.
This 3x4 matrix is the content area of your scroll view. With paging enabled, your scroll view will stop on one of these "cells", e.g. 2,1. That portion of the scroll view will be visible.
If you want each "cell" to be controlled by it's own view controller, then pre-generate all the view controllers (and their views), and then add all of their views as subviews to the scrollView.
You would populate this scroll view with whatever view you wanted to show at any given location. Set the frame of each view relative to the scrollview's origin. So if the cells were 320 pixels wide and 480 pixels tall, the frame for cell 1,3 would be CGRectMake(1*320, 3*480, 320,480).
When the scrollView ends deceleration, you can get it's contentOffset property, do some arithmetic and figure out which cell you're in.
To get the wrap-around effect, you'll have to do some trickery. You might put an extra cell at the end of each row and column, and if you find yourself in that cell, just set the scrollviews' contentOffset to the corresponding cell at the beginning of the row or column.

UITableViewController, getting bounds of visible area

I've got this question. In my app I have many cells. When user clicks on something I'm loading VC and pushing it. To indicate this behaviour I'm displaying a subview which informs that now application is loading. Problem is, when user scrolls down and taps on cell my subview still is on the top of the tableView thus user isn't seeing it.
I need to change frame of my subview so my user can see it. I want this frame to be the center of the current view. How I can do that?
Fore example: My content is 800 pixels worth of height, now I'm displaying at 400 pixels subview with specific information about loading. When my user scrolls down to a cell that is at 2400 pixels, I'm still displaying subview at 400 pixels and thus it is unviewable by user.
I was trying to calculate new frame.origin.y by checking y of a cell that is tapped. Problem is the cell at 2400 pixels could be the first cell user is seeing or the last one, so how I can get the middle of the screen?
Edited with some screens:
When I select first time:
When I select after scrolling down big time:
What it looks like you have done here is added the loading view as a subview to the UIScrollView. You need to add the loading viewController into the parentView after the scrollview.
If this cant be done because your viewController is a subclass of UIScrollview, change it to a regular UIViewController subclass but implement the scrollView protocols and then do as i have said above. Other than that you would have to map the frame of the loading ViewController to the offset on the scrollView-not a good idea.