I have a UICollectionView(One Row, Vertical align) with a UIPageControl associate with it.
The UIPageControl will have number of dots as the the number of items in my collection view.
I want to change/scroll the position of the collectionView when a dot is pressed.
Moreover, I want change the selected dot according to the scrolling of the collection view.
Any idea about some kind of algorithm for the scrollViewDidScroll method too accomplish that?
Thanks in advance
I got a simple solution.
Under the method scrollViewDidScroll just check the visible cell in the middle of the screen:
CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
[self.pageControl updateStateForPageNumber: (int)visibleIndexPath.row];
If I am understanding it correctly, I think you can do it via collectionView:cellForItemAtIndexPath: method of the UICollectionViewDataSource
Whenever an item is requested from the data source you can select the relevant dot on UIPageControl.
Did this help ?
Related
I have a UICollectionView with 1 row 1 column. I want to get the first item (indexPath basically) when I am scrolling it horizontally.
For example, I have got 100 items displayed horizontally in my UICollectionView and when I scroll from right to left, whichever item is the first one visible, I need its indexPath.
Which means the indexPath will be constantly changing while its scrolling.
How to achieve this? Please Guide. Thanks!
collectionView.indexPathsForVisibleItems() returns an array of NSIndexPath for visible cells. You can get the left most one by sorting them by NSIndexPath.item.
Edti:
To be notified while scrolling, implement the UIScrollViewDelegate method scrollViewDidScroll(_: UIScrollView). Remember that UICollectionView is a subclass of UIScrollView.
I think you need implement UIScrollView delegate method and get visible cell inside
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
UICollectionViewCell *firstCell = [[collectionView visibleCells] firstObject];
NSIndexPath *firstIndexPath = [collectionView indexPathForCell: firstCell];
NSLog(#"%#", firstIndexPath);
}
Hope this help
I have a UICollectionView that i need to detect which cell is in view.
I already have the content offset already using the code below:
float scrollPoint = self.collectionView.contentOffset.y;
now where i am stuck is at finding which cells are at that point.
How do I find out which cell(s) are at that point.
Use - indexPathForItemAtPoint:(CGPoint)point:
NSIndexPath* path = [self.collectionView indexPathForItemAtPoint:contentOffset];
if (path) {
// Do stuff.
}
Documentation.
Actually, your contentOffset will give you the wrong cell. You probably just want to use the point 0, 0 or whatever to get the cell that's at the left/top of the scrollview.
I need some help.
I have UIcollectionView, I am using scrollToItemAtIndexPath:atScrollPosition:animated: Method. I need somehow to recognize if collection view will scroll to position or if item on needed position and will no scroll.
Another words, if scrolling will not start, scroll view delegate methods will not be called.
Thanks for help
How about checking if the indexPath you're interested in is in [UICollectionView indexPathsForVisibleItems]?
You can also use layoutAttributesForItemAtIndexPath to get the exact frame of the cell to determine if its partially shown.
You can use
CGRect cellFrame = [collectionView.visibleCells.lastObject frame];
CGRect collectionViewVisibleRect = collectionView.bounds;
collectionViewVisibleRect.origin.y = collectionView.contentOffset.y;
if (CGRectContainsRect(collectionViewVisibleRect, cellFrame))
//no scroll
else
//scroll
That does not takes into account contentInset that you might have.
Is it possible to recognise gesture from scrollView so when one scrollView is dragged to simulate same dragging on another scrollView? For example I have tableView with custom cell that have scrollView in it, and I want to simulate dragging on every item in list.
EDIT
I succeeded to recognize movement of scrollView, but now I cant set other scrollViews from table to do same amount of scrolling.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int position_x = scrollView.contentOffset.x;
int i=0;
int indexPath = [self.tableView indexPathForSelectedRow].row;
static NSString *hlCellID = #"EPGCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:hlCellID];
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:16];
for(i=0; i<EPGList.count;i++){
if(i!=indexPath){
scrollView.contentOffset = CGPointMake(position_x, scrollView.frame.size.height);
}
}
}
Problem is that indexPath is not returning correct value when i am clicking on scrollView. It returns correct value only when I am clicking on cell space outside of scrollview.
Yes, yes it is possible. You need to have a delegate assigned to the scrollViews within the cells. Preferrably the same class that implements hour tableview data source and table view delegate protocols. You must implement UIScrollViewDelegate and the delegate must implement the scrollViewDidScroll: method.
Within that scrollViewDidScroll delegate method, you check the contentOffset property for its x and/or y values. Normally people go for the vertical scrolling value, but if you're implementing them in a UITableViewCell, you're probably doing horizontal scrolling.
The you use the value, I am assuming contentOffset.x, to update other scroll views within the other cells of your table! Make sense?
I have just posted an answer to another UIScrollView question yesterday. Please read what I wrote there, it might help you more.
Animation in UIScrollView iOS app
I have a TableView in a ViewController, when I select a cell, I want to check if its below a certain y limit.
that y limit is in terms of the view of the viewController.
I took cell's origin and converted it in terms of ViewController's view.
Here I have used the method of UIView class : convertPoint:fromView:
Here I have passed tableView itself as the fromView parameter considering it is the direct parent view of a cell and the frame of the cell will be interms of the tableView.
I see some increase in the y-value of the new point that I get than what I should have been getting.
Is there a better way to achieve this ?
CGRect rectOfCellInTableView = [tableView rectForRowAtIndexPath:indexPath];
CGRect rectOfCellInSuperview = [tableView convertRect:rectOfCellInTableView toView:[tableView superview]];
NSLog(#"cell rect is : %#",NSStringFromCGRect(rectOfCellInSuperview));
try this..
CGRect newPosition = [theCell convertRect:theCell.frame toView:yourViewController.view];
this will give you position of cell in terms of yourViewController
In Swift 4
let rectOfCellInTableView = tableView.rectForRow(at: indexPath)
let rectOfCellInSuperview = tableView.convert(rectOfCellInTableView, to: tableView.superview)
print("cell rect is : \(rectOfCellInSuperview)")
let newPosition = cell.convert(cell.frame, to: yourViewController.view)
Thanks to Warewolf & Prashant Nikam