Gaya3
Gaya3

Reputation: 55

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 :)

Upvotes: 1

Views: 1091

Answers (6)

user4993619
user4993619

Reputation:

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
      }
}

Upvotes: 0

Anurag Sharma
Anurag Sharma

Reputation: 4824

There is UIScrollView already embedded in UITableView and UITextView Reference

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

Upvotes: 0

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

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.

Upvotes: 0

Abhishek Thapliyal
Abhishek Thapliyal

Reputation: 3708

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 {

        }
    }

Upvotes: 1

Subramanian P
Subramanian P

Reputation: 4375

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
    }
}

Upvotes: 1

Lalit kumar
Lalit kumar

Reputation: 2207

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..
    }
    }

Upvotes: 1

Related Questions