scrollViewDidEndDecelerating detect which collection view in action - ios

I use this method to organize paging:
- scrollViewDidEndDecelerating
When I scroll my UICollectionView I change some content on my screen. But I have few UICollectionView on my screen and I need it just for one.

Well, UICollectionView inherits from UIScrollView, so you could just check which scroll view did end decelerating from within the delegate method.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (scrollView == collectionViewOne) {
}else if (scrollView == collectionViewTwo) {
}else{
//something else
}
}

Related

Disable scrollViewDidScroll: when scrolling UICollectionView - iOS

I have implemented scrollViewDidScroll: inside my viewcontroller to cause some animations when I scroll the view up and down.
However, when I scroll my collectionview inside the viewcontroller (horizontally) it messes up with my animation inside scrollViewDidScroll:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
newAlpha = 1 - scrollView.contentOffset.y / 200;
self.introImageView.alpha = newAlpha;
//... -> prevent scrolling when collectionview is scrolled
}
How do I prevent calling scrollViewDidScroll: when scrolling my collectionview horizontally?
The best way is not to disable the delegate method, but make sure to only call that code when it's called by your scrollview. Here's an example
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.myScrollView) {
newAlpha = 1 - scrollView.contentOffset.y / 200;
self.introImageView.alpha = newAlpha;
} else {
//collectionView would fall here
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([scrollView isKindOfClass:[UICollectionView class]] == NO) {
newAlpha = 1 - scrollView.contentOffset.y / 200;
self.introImageView.alpha = newAlpha;
//... -> prevent scrolling when collectionview is scrolled
}
}

Detect Textview Scrolling and Tableview Scrolling

I have a TextView and a Tableview in UIView. Im trying to detect either scrolling is textview scrolling or tableview scrolling?There is any code for this? Thanks for any help in advance :)
UITableViewDelegate conforms to UIScrollViewDelegate, so all you need to implement these methods -scrollViewWillBeginDragging and -scrollViewDidScroll
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == myTableView){
// Your logic here.....
}
if (scrollView == textView)
{ // Your logic here..
}
}
UITextView & UITableView both are implemented by using UIScrollView
You can identify the which control scrolling & direction by implementing the UIScrollViewDelegate
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
if ([scrollview isKindeOfClass: [UITextView Class]]) {
//UITextView
} else if ([scrollview isKindeOfClass: [UITableView Class]]) {
// UITableView
}
// Identifying direction
CGPoint point = [scrollView.panGestureRecognizer translationInView:scrollView.superview];
if (point.y > 0) {
// Dragging down
} else {
// Dragging up
}
}
Use func scrollViewDidScroll(_ scrollView: UIScrollView) delegate and check. Set UITextViewDelegate and UITableViewDelegate and check in method
Example:
Objective-C
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if scrollView == tableView {
}
if scrollView == textView {
}
}
Swift 3.0
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == tableView {
}
if scrollView == textView {
}
}
UITableView & UITextView are a subclass of UIScrollview. So all the UIScrollView you will get when you scroll UITableView. Here you get a list of UIScrollView method.
There is UIScrollView already embedded in UITableView and UITextView
Reference
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
UITextView and UITableView are both subclasses of UIScrollView , so both of them triggers the UIScrollViewDelegate methods.
assume that __scrollView and __textView are your respective UIScrollView and UITextView,
Overriding UIScrollViewDelegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ( scrollView == __scrollView ) {
// UIScrollView is scrolled
}else if ( scrollView == __textView ){
// UITextView is scrolled
}
}

Detect when Scrollview Scrolling not tableview scrolling

I have a scrollview inside tableview (tableview's header) and I want to call this action when scroll finish:
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let pageNumber = round(bookScrollView.contentOffset.x / bookScrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
print("scroll")
}
when I scroll bookScrollView nothing happen, but when table view scroll this function run.
How I detect when bookScrollView scroll and run function?
thanks
Here, you can define, scrollOfTable, or you can also define TAG property. identify and only scroll when scrollview comes in, not tableview.
You also need to check, if your scrollView is setting delegate
self.vsScrollView.delegate = self
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if scrollView == scrollOfTable {
}
else if scrollView == scrollOfScrollView {
}
else {
//something else
}
}

How to scroll multiple tableview simultaniously

2 Tableviews in a ViewController with different size of cell height. When i scroll on the view controller , need to scroll both tableview cell at same time also both table cells also need to hide at same time.
200px height of cell in top tableview, 50px height of bottom tableview.
when i swipe 100px , top table cell scrolls 100 px and bottom table cell should be scrolled 25px.
is this parallel scrolling any possible? any suggestion..
Thanks in advance
As UITableview is derived from UIScrollview you can get the scroll amounts in the delegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
You can get content offset of the scrollview which is being scrolled and assign that to the other tableview.
In this method you should be able to access both tableviews.
The property you need to check is contentOffset.
firsttableview.contentOffset = scrollview.contentOffset
You can use this function of UIScrollView to scroll multiples UITables parallel
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == tableView1 {
self.tableView1.contentOffset = CGPointMake(0, scrollView.contentOffset.y)
self.tableView2.contentOffset = CGPointMake(0, scrollView.contentOffset.y)
}
else if scrollView == self.tableView2!
{
self.tableView1.contentOffset = CGPointMake(0, scrollView.contentOffset.y)
}
else if scrollView == self.bottomMenu_grid!
{
print(scrollView.contentOffset.x)
self.tableView2.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y)
}
}

Detect UITextView scroll location

I am trying to implement a form of a Terms & Conditions page where the "Proceed" button is only enabled once the user has scrolled to the bottom of a UITextView. So far I have set my class as a UIScrollView delegate & have implemented the method below:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(#"Checking if at bottom of UITextView");
CGPoint bottomOffset = CGPointMake(0,self.warningTextView.frame.size.height);
//if ([[self.warningTextView contentOffset] isEqualTO:bottomOffset])
{
}
}
I have commented the if statement because I am not sure how to check if the UITextView is at the bottom.
UITextView is a UIScrollView subclass. Therefore the UIScrollView delegate method you are using is also available when using UITextView.
Instead of using scrollViewDidEndDecelerating, you should use scrollViewDidScroll, as the scrollview may stop scrolling without deceleration.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height)
{
NSLog(#"at bottom");
}
}
A Swift version for this question:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height {
print( "View scrolled to the bottom" )
}
}
This should solve it. It works. I am using it.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
if (bottomEdge >= scrollView.contentSize.height)
{
// we are at the end
}
}

Resources