I'm trying to make my cells snap into place, where the height of my cells is UITableViewCell height = self.view.frame.size.height - 80
I'm using this code to snap them. This works when scrolling upward, but when I scroll downward I run into a problem where if I completely drag the next cell onto the screen, it will snap back to the current cell, even if I scroll so far that only a little bit of the current cell is still showing. Anybody know what I'm doing wrong?
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
NSIndexPath *pathForTargetTopCell = [self.purchasesTableview indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.purchasesTableview.bounds), targetContentOffset->y)];
targetContentOffset->y = [self.purchasesTableview rectForRowAtIndexPath:pathForTargetTopCell].origin.y;
}
A sample UITableViewCell from my UITableView
You don't need any code to make your screen-sized cells snap into place, you can do it by selecting the "Paging Enabled" check box in IB for the table view.
Related
I have a new design for my app and it include a parallax scroll for a image display on top of tableView.
I know how to add a parallax effect with putting a image in to a cell, like this -
when table scrollViewDidScroll get called :
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint currentOffset = scrollView.contentOffset;
if (currentOffset.y > _lastContentOffset.y) {
//Scroll Up
_containerView.clipsToBounds = true;
_bottomSpaceConstraint.constant = -scrollView.contentOffset.y / 2;
_topSpaceConstraint.constant = scrollView.contentOffset.y / 2;
} else {
//Scroll Down
_topSpaceConstraint.constant = scrollView.contentOffset.y;
_containerView.clipsToBounds = false;
}
_lastContentOffset = currentOffset;
}
(the _bottomSpaceConstraint and _topSpaceConstraint are the Image top and Bottom constraint which inside the tableView cell in section 0)
But my problem is I needs to stop the image scrolling up when it reached the size of my navigation bar. (my navigation bar background is transparent) I don't want the image to go all the way top. But it is a cell which is inside my tableView so it is going all the way to top until it disappear when scrolling. I need help for stop this image get scrolled. Maybe my approach is not correct if I want to achieve this effect.
This effect is on android and its called "collapsing toolbar layout".
http://antonioleiva.com/collapsing-toolbar-layout/
Does someone know how to do this "collapsing toolbar layout" effect for iOS.
Thanks a lot for any help!
Try creating a UIView that will contain the image and set the tableHeaderView in the tableView. Then in scrollViewDidScroll: update the height and top constraints of the header view.
Target is iOS 7+ iPhone 4s, 5, 5s, 6, 6+
By design, I have a scroll view that contains a collection view showing one row of cells only.
Using this set up, works well. The user can scroll right to left or up and down and compare the cells' content.
The scroll view supports the up/down scrolling. The collection view supports the left/right scrolling.
At the top of each collection view cell, I have a titleView (shown as "Some Title"). When the user scrolls up and down, I want the titleView for each cell always floating at the top of the screen. (See image below)
I am close to an answer. I use scrollViewDidScroll and set the titleView.origin.y to scrollView.contentOffset.y.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
for (CVKRecipeCollectionViewCell *cell in self.collectionView.visibleCells) {
CGRect myFrame = cell.titleView.frame;
myFrame.origin.y = scrollView.contentOffset.y;
cell.titleView.frame = myFrame;
}
}
That works when the user scrolls up and down. However, when the user scrolls right/left, the titleView resets itself back to its original y position of 0.
How can I keep the titleView in that same y position that I specified in scrollViewDidScroll?
Code showing the issue is at:
https://bitbucket.org/finneycanhelp/kata_collection_view_and_flow_layout/branch/floating-title-view
CVKMainViewController has the scrollViewDidScroll method.
The answer is to use the scrollView's contentOffset in the scrollViewDidScroll method. The collectionView is a scroll view. So this method is being called by both the scroll view that handles up/down and the collectionView which handles the left to right scrolling.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
for (CVKRecipeCollectionViewCell *cell in self.collectionView.visibleCells) {
CGRect myFrame = cell.titleView.frame;
myFrame.origin.y = self.scrollView.contentOffset.y;
cell.titleView.frame = myFrame;
}
}
I have a tableView with dynamically generated custom cells and one of the custom cell contains a scrollView that can scroll vertically and a page control is attached to it.
The user interface structure of the tableview can be seen in the photo.
I am refreshing the position of the page control in the scrollViewDidScroll method as follows.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSIndexPath *indexPathPageControl=[NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell * cell = (UITableViewCell*)[self.sonDakikaTableView cellForRowAtIndexPath:indexPathPageControl];
CGFloat xOffset = scrollView.contentOffset.x;
CGFloat frameWidth = scrollView.frame.size.width;
int page = floor((xOffset - frameWidth / 2) / frameWidth) + 1;
UIPageControl *pageControl = (UIPageControl *)[cell.contentView viewWithTag:14];
pageControl.currentPage = page; // assigning to pagecontroll
}
It is working very fine when the user is scrolling vertical in the scrollView.
However when the user scrolls down on the tableView and the cell with vertical scrollView is not visible anymore, the integer page variable is being updated with 0 and when the user scrolls up and table view cell is visible again, the location of page control is being reset.
I will appreciate if someone can give me an idea about how to prevent this.
Thanks in advance.
i added this code
(void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (_scrollView == self.tableView) return;
and in your case
(void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (_scrollView == self.sonDakikaTableView) return;
I am trying to implement is "snap to cell" effect in my UITableView.
My UITableView has 3 equally sized cells, but for some reason the UITableView always snaps to cell 0 or cell 1 and doesn't ever snap to the third cell. its also wrong by about 50 points too low, no matter how hard you slide. I use the most obvious code to create the effect so I can't understand why it doesn't work.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
NSIndexPath *pathForTargetTopCell = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), targetContentOffset->y)];
DLog(#"indexPathForRow %d", pathForTargetTopCell.row);
targetContentOffset->y = [self.tableView rectForRowAtIndexPath:pathForTargetTopCell].origin.y;
}
Any thoughts about what could be going wrong?
EDIT:
I suspect this has something to do with the status bar and nav bar on top, since its off by exactly 64 points. Still doesn't explain why it doesn't recognise the last cell though..
For the status bar offset, do this in your VC's viewDidLoad:
if([self respondsToSelector:#selector(setAutomaticallyAdjustsScrollViewInsets:)])
[self setAutomaticallyAdjustsScrollViewInsets:NO];
edit
Just for testing, could you add 4 equally size cells and try snapping to the 3rd and 4th cells?
Modify your scrollViewWillEndDragging implementation as follows:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
NSIndexPath *pathForCenterCell = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), CGRectGetMidY(self.tableView.bounds))];
[self.tableView scrollToRowAtIndexPath:pathForCenterCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
I have a horizontal UITableView where I would like to set the paging distance. I tried this approach,
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
_tableView.decelerationRate = UIScrollViewDecelerationRateFast;
CGFloat pageSize = 320.f / 3.f;
CGPoint contentOffset = _tableView.contentOffset;
contentOffset.y = roundf(contentOffset.y / pageSize) * pageSize;
[_tableView setContentOffset:contentOffset animated:YES];
}
This works when you scroll slowly and let go but if you scroll fast, there's a lot of popping. So after wrestling with it for a bit, I'm trying a different approach...
I'm simply enabling paging on the tableView and then setting the width of the table to my desired paging size (a 3rd of the screen's width). I also set clipsToBounds = NO. When I use this approach, the scrolling works as expected but now cells outside of my smaller table width do not draw. What I would like is to force the cell on the left and right of my current cell to draw, even though they are outside of the UITableView's frame. I know cellForRowAtIndexPath get's called from a deeper level but is there some way I can trigger it myself for the cell's I selected?
I've tried using,
[[_tableView cellForRowAtIndexPath:indexPath] setNeedsDisplay];
but it does nothinggggggg!