Is there any way while the user is scrolling an uitableview to know when one of the many uitableviewcells is offscreen by 70% when reaching the top? Should i use willdisplaycell for this? Any help appreciated.
Implement scrollViewDidScroll...
get the frame of the row / cell you want to track
compare it to the scroll offset to see how much of it is visible
In this simple example, rowToTrack is an NSInteger, such as 2 for the 3rd row, sectionToTrack is an NSInteger, such as 0 for the 1st section:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// index path for row we want to track
NSIndexPath *p = [NSIndexPath indexPathForRow:rowToTrack inSection:sectionToTrack];
// get frame for that row
CGRect r = [self.tableView rectForRowAtIndexPath:p];
// get bottom of row frame
CGFloat t1 = CGRectGetMaxY(r);
// get scroll content offset
CGFloat t2 = scrollView.contentOffset.y + scrollView.adjustedContentInset.top;
// height of visible portion of row frame
CGFloat visibleHeight = t1 - t2;
// as a percentage between 0 and 1
CGFloat pct = MAX(0.0, MIN(1.0, visibleHeight / r.size.height));
NSLog(#"Row %ld is %0.2f percent visible", rowToTrack, pct * 100);
}
Related
I wanna ask if its possible to change the alpha value of a navigation bar when the user is scrolling a tableview.
I have the algorithm, but i need something help to get changes on real time.
/* This is the offset at the bottom of the scroll view. */
CGFloat totalScroll = scrollView.contentSize.height - scrollView.bounds.size.height;
/* This is the current offset. */
CGFloat offset = - scrollView.contentOffset.y;
/* This is the percentage of the current offset / bottom offset. */
CGFloat percentage = offset / totalScroll;
/* When percentage = 0, the alpha should be 1 so we should flip the percentage. */
scrollView.alpha = (1.f - percentage);
It's probably too late, but for future reference, you could do something like this:
func scrollViewDidScroll(scrollView: UIScrollView) {
self.navigationController!.navigationBar.alpha = 1 - (self.tableView.contentOffset.y / (self.tableView.contentSize.height - self.tableView.frame.size.height));
}
On this delegate you have the values of contentOffset of scrollview or tableView, and you can observe the value of this property to make the behavior you desire.
Hope it helps!
do you know a way in order to detect when a user has scrolled to the end (or the beginning) of an UICollectionView (in order to load new content). Since the UICollectionView scrolls horizontally and not vertically, I am not interested to the scrolling to top/bottom but to leftmost/rightmost.
The only way I found so far is to subclass the UICollectionView and then register the view controller as a delegate that is informed of when the collection view willLayoutSubviews and perform calculations accordingly.
But let's say I don't want to subclass UICollectionView, is there a way to detect the scrolling to the end/beginning of UICollectionView?
EDIT:
since UICollectionView inherits from UIScrollView, I can use its delegate methods. How does it work with with horizontal scrolling?
Thank you
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsetX = scrollView.contentOffset.x;
CGFloat contentWidth = scrollView.frame.size.width;
NSLog(#"offset X %.0f", offsetX);
// If your UICollectionView has 3 cell, the contentWidth is 320,
// When you scroll from beginning to end of the UICollectionView, it will print:
// offset X 0
// offset X 1
// offset X 2
// ......
// offset X 320
// offset X 640
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([scrollView isKindOfClass:[UICollectionView class]] == YES) {
CGFloat threshold = 100.0 ;
CGFloat contentOffset = scrollView.contentOffset.x;
CGFloat maximumOffset = scrollView.contentSize.width - scrollView.frame.size.width;
if ((maximumOffset - contentOffset <= threshold) && (maximumOffset - contentOffset != -5.0) ){
//Your code
}
}
}
I want to change the height of my UITableView based on the scrolled content. Right now I do it by getting the scrollViewDidScroll event and then getting scrolled value and then change the height. Here is my code (for simplification I omitted the irrelevant code):
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
float currentOffset = scrollView.contentOffset.y
double delta = currentOffset - storedOffset;
if(delta != 0)
{
delta = abs(delta);
CGRect newListFrame = myTableView.frame;
float newListHeight = newListFrame.size.height + delta;
newListFrame.size.height = newListHeight;
myTableView.frame = newListFrame;
}
storedOffset = currentOffset;
}
But this approach is wrong because with this approach my UITableView's content is scrolled only a little bit and that's not what I want. I just want to get the value of that list that would be scrolled without actually scrolling it. Is there any way to do that? I thing I could get raw finger moved event but can I get it on UITableVIew? Can I do something like this using a UITableView method?
Hi I have 10 images in UIScrollview now i can scroll forward like from 1 to 2,3,4,etc but in my project i need to scroll images backward direction also like from 1 to 10 to left hand side
this is my code
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
CGFloat pageWidth = cardsScrollView.frame.size.width;
NSUInteger page = floor((self.cardsScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if(page + 1 == [flashCardArry count]){
for (NSUInteger i = 0; i < [flashCardArry count]; ++i) {
int nElements = [flashCardArry count] - i;
int n = (arc4random() % nElements) + i;
[flashCardArry exchangeObjectAtIndex:i withObjectAtIndex:n];
}
[self.cardsScrollView scrollRectToVisible:CGRectMake(self.cardsScrollView.frame.size.width * 0 , 0, self.cardsScrollView.frame.size.width, self.cardsScrollView.frame.size.height) animated:NO];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGFloat pageWidth = cardsScrollView.frame.size.width;
NSUInteger page = floor((self.cardsScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
You can simply do that by adding the imageViews from 10 to 1 on your scrollView and then you have se the contentOffset of you scrollview to point to the last image which is image 1.
So user wont be able to scroll right at first, he can scroll to left for next images.
So. In case if you want cyclic scrollview you would need next things:
1) hide scrollView indicators
scrl.showsHorizontalScrollIndicator = NO;
scrl.showsVerticalScrollIndicator = NO;
2) Add one item to beginning of the scroll view, and one item at the end
[5'] | [0][1][2][3][4][5] | [0']
| --- full_size--- |
3) Once user scrolls to 5' item we updating content offset by adding full_size
4) Once user scrolls to 0' item we updating content offset by subtracting full_size
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint contentOffset = scrollView.contentOffset;
// First item
if (contentOffset.x < page_size) { // We are now faking last page
scrollView.contentOffset += full_size; // Jumping to actual last page
}
// Last item
if (contentOffset.x > full_size) { // We are now faking first page
scrollView.contentOffset -= full_size; // Jumping to actual first page
}
}
5) To decrease amount of jumps (especially on the borders), you can add more 2 or more fake items from both side
I'm using iCarousel to have a scroll of images. THe code is:
- (CATransform3D)carousel:(iCarousel *)_carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
CGFloat count = 5;
CGFloat spacing = 0.9f;
CGFloat arc = M_PI * 0.3f;
CGFloat radius = fmaxf(140.0 * spacing / 2.0f, 140.0 * spacing / 2.0f / tanf(arc/2.0f/count));
CGFloat angle = offset / count * arc;
radius = -radius;
angle = -angle;
transform = CATransform3DTranslate(transform, radius * sin(angle),radius * cos(angle) - radius, 0.0f);
return transform;
}
But when I scroll the images there is an ugly effect, the transition is not smooth and the images comes out in a jerky way, but I'd like to come out smootly. Can you help me? Thanks.
Edit: The problem is that when I scroll the images the transition is not smooth and the images comes out in front of the images on the back with a detachment from the other images. Pratically, the images comes in front of the others only when the scrolling did end and this causes a bad effect.
We had a similar issue in our last implementation of iCarousel. Here's how we fixed it:
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value {
if (option == iCarouselOptionSpacing) {
return value;// * 1.05f;
} else if(option == iCarouselOptionWrap) {
return NO;
} else if(option == iCarouselOptionVisibleItems) {
return 3;
}
return value;
}
Specifically, what you're probably needing is that last else if statement where we specify the number of visible items. It defaults to 1 which means that as you swipe through the new images come out in a jerky way. By specifying 3, you guarantee that the most previous item, the current item, and the next item are always loaded in memory so scrolling between them is always smooth. If this doesn't solve your problem, increase the number 3 to whatever works.
Also, don't forget to set the delegate of the iCarousel to self.
Best of luck.