UICollectionView Scrolling Horizontally - ios

I am implementing a UICollectionView in my iOS app. I have it so each cell is the width of the screen and I want to have it so when scrolling horizontally it locks onto the visible cell and moves it to the center of the screen. The code bellow I have only works for the first cell. I don't understand how to get this to work for any cell the user has visible.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int index = 0;
for (TextCell * t in [_tabBarCollectionView visibleCells]) {
if ((t.center.x>0)&&(t.center.x<[[UIScreen mainScreen]bounds].size.width)) {
[_tabBarCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
break;
}
index++;
}
}

You can turn paging on for the collection view and it will have that effect. Go to the xib file or storyboard that has the collection added and enable paging under its properties.

Related

Scroll one cell at a time in collection View

Using the willDisplayCell method I got the index of the last visible cell:
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
indexVisibleCell = [indexPath indexAtPosition:[indexPath length] - 1];
}
where indexVisibleCell is an NSUInteger that is declared in the class' interface. My problem is that I want to shift only one cell when user taps on the right or left button. I have applied this code for that purpose:
- (IBAction)rightButton:(id)sender {
NSUInteger indexOfScrolledCell;
indexOfScrolledCell = indexVisibleCell;
if (indexVisibleCell < 9) {
NSIndexPath *path = [NSIndexPath indexPathForRow:indexOfScrolledCell+1 inSection:0];
[self.obj_CollectionView1 scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
}
as I show 3 cells at a time. By using that rightButton action method it shifts 3 cells at a time, not one. Please help me figure out how to scroll one cell at time.
Try using UICollectionView with paging enabled. Use following code, hopefully it works out for you.
[yourCollectionView setPagingEnabled:YES];
Use visibleCells to get your visible Cells, get the leftmost Cell index to figure out the next index, then scroll to that Cell on the left (similar logic to your code).
willDisplayCell is not reliable since it is called both times when you scroll left/right, and you're thinking that index is the left/right most is not correct.
Edit:
Or you need to compare the index in willDisplayCell correctly, to get the correct left/right most index, based on your logic.
For right button you can use following
[self.obj_CollectionView1 scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
and for left button following
[self.obj_CollectionView1 scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
I am changing UICollectionViewScrollPosition so that when you scroll to right new item shows up on right side of collection view and when you scroll to left new item shows on left side of collection view.

Visible UITableViewCells not resizing on rotate

I have a UITableView with custom cells. When I rotate the device, the visible cells do not resize. Scrolling to new cells works fine and when I scroll back, all is well, but is there a way to get the initial, visible cells to smoothly resize on rotate?
[self.tableview reloadData] from the UITableViewController; works, but I don't think that is the optimal solution, as it requires a network call for data retrieval.
I am already calling the following code from the UITableViewController in order to layout some gradient layers I use, but this does nothing to resize the ImageView.
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
for (ProjectCell *cell in [[self.tableView.subviews firstObject] subviews]) {
cell.laidOut = NO;
[cell layoutSubviews];
}
}
Thanks
I believe this will do what you want:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
As long as the cells you want to reload are the visible cells.

iOS UITableView animating only first visible cell

I want to implement an UITableView with some animation while the user is scrolling. Only the first visible (top most) cell has to be an different layout. Before the second cell reaches the top of UITableView I want to start the small animation. (changing background alpha and width of cells contentView etc.)
How could I handle this?
[UPDATE]
What I have tried so far:
using UIScrollView delegate method scrollViewDidScroll:(UIScrollView *)scrollView
together with UITableView´s visibleCells property using the first item of it to get the
reference with no luck
This is what I have done:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
[self scrollingFinish];
}
}
..
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self scrollingFinish];
}
..
- (void)scrollingFinish {
NSIndexPath *firstVisibleIndexPath = [[self.tableView indexPathsForVisibleRows] objectAtIndex:0];
NSLog(#"first visible cell's section: %i, row: %i", (int) firstVisibleIndexPath.section, (int) firstVisibleIndexPath.row);
APFCategoryCell * cell = (APFCategoryCell*) [self.tableView cellForRowAtIndexPath:firstVisibleIndexPath];
[cell animateCell];
[self.tableView scrollToRowAtIndexPath:firstVisibleIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
The problem appears, if the user is scrolling down because the animated cell is still visible and the next one gets animated. So two or more cells are animated. I could going on and implement an method in my custom cell like - (void) resetAnimatedCell but maybe there is an much more elegant way for doing this?

I need help animating UICollectionView cell size when collection view size changes

I have a UICollectionView that takes up the entire screen, and inside it are cells that are the same exact size as the collection view. When the user hits a button, the collection view animates to take up only half the screen (and another view is shown beside it). This is fine, but the cells themselves are still the size of the original collection view.
I can call invalidateLayout on the collection view's layout, and this will cause collectionView:layout:sizeForItemAtIndexPath: to be called and the cells are all recalcualted, and the sizes are changes, but these do not animate, they simply snap to the new size.
Is there a way to animate these cell size changes at the same time, or in the same animation block that animates the size change to the parent collection view?
[_collectionView performBatchUpdates:^{
for (int i =0; i<count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UICollectionViewCell *cell = [_collectionView cellForItemAtIndexPath:indexPath];
cell.layer.transform = CATransform3DMakeScale(0.9, 0.9, 1);
}
} completion:nil];

Scrolling a UITableView cell up and then expand it

I have a UITableView with custom cells in it. The cells expands when I tap on them. I am facing an issue when I tap on the cell which is half cut by the bottom screen edge of the device. When I tap on this cell, I want cell to move up and then expand to show the details but right now it just expands and I do not know if is expanded or not. I need to manually scroll the table view up to see it.
Below is my cell tapping code. I implemented the related cell expansion methods and cell is expanding properly.
- (void)cartTableView:(UITableView *)iTableView didSelectRowAtIndexPath:(NSIndexPath *)iIndexPath {
[self.temporaryCartTable scrollToRowAtIndexPath:iIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[self expandOrCollapseCartCellAtIndex:iIndexPath];
}
- (void)expandOrCollapseCartCellAtIndex:(NSIndexPath *)iIndexPath {
self.selectedIndexPath = iIndexPath;
[self.temporaryCartTable beginUpdates];
[self.temporaryCartTable reloadRowsAtIndexPaths:#[iIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.temporaryCartTable endUpdates];
}
After expanding, call scrollToRowAtIndexPath, which should position your cell correctly.
You need to check if rect of the cell you're extending (rectForRowAtIndexPath:) is fully within the bounds of the table view (based on contentSize and contentOffset, using CGRectContainsRect and if it's not - and only partially visible - then you need to scroll it to the bottom/top of the table view.

Resources