UIScrollView में स्क्रॉल करने की दिशा ढूँढना?


183

मेरे पास UIScrollViewकेवल क्षैतिज स्क्रॉलिंग की अनुमति है, और मैं यह जानना चाहता हूं कि उपयोगकर्ता स्क्रॉल किस दिशा (बाएं, दाएं) में है। मैंने जो किया वह विधि को उप-विभाजित करना UIScrollViewऔर अधिरोहित करना था touchesMoved:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    float now = [touch locationInView:self].x;
    float before = [touch previousLocationInView:self].x;
    NSLog(@"%f %f", before, now);
    if (now > before){
        right = NO;
        NSLog(@"LEFT");
    }
    else{
        right = YES;
        NSLog(@"RIGHT");

    }

}

लेकिन यह तरीका कभी-कभी मुझे हिलाने पर बिल्कुल नहीं मिलता। तुम क्या सोचते हो?


नीचे मेरी प्रतिक्रिया देखें - आपको ऐसा करने के लिए स्क्रॉल दृश्य प्रतिनिधियों का उपयोग करना चाहिए।
मेम

यहाँ सबसे अच्छा जवाब: stackoverflow.com/questions/11262583/…
iksnae

जवाबों:


392

दिशा का निर्धारण काफी सरल है, लेकिन ध्यान रखें कि इशारे के दौरान दिशा कई बार बदल सकती है। उदाहरण के लिए, यदि आपके पास पेजिंग चालू करने के साथ स्क्रॉल दृश्य है और उपयोगकर्ता अगले पृष्ठ पर जाने के लिए स्वाइप करता है, तो प्रारंभिक दिशा सही हो सकती है, लेकिन यदि आपने उछाल चालू किया है, तो यह संक्षेप में किसी भी दिशा में नहीं जाएगा तो संक्षेप में छोड़ दिया जा रहा है।

दिशा निर्धारित करने के लिए, आपको UIScrollView scrollViewDidScrollप्रतिनिधि का उपयोग करना होगा । इस नमूने में, मैंने एक वैरिएबल बनाया है जिसका lastContentOffsetउपयोग मैं पिछले एक के साथ वर्तमान सामग्री ऑफसेट की तुलना करने के लिए करता हूं। यदि यह अधिक है, तो स्क्रॉलव्यू सही स्क्रॉल कर रहा है। यदि यह कम है तो स्क्रॉल दृश्य बाईं ओर स्क्रॉल कर रहा है:

// somewhere in the private class extension
@property (nonatomic, assign) CGFloat lastContentOffset;

// somewhere in the class implementation
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    ScrollDirection scrollDirection;

    if (self.lastContentOffset > scrollView.contentOffset.x) {
        scrollDirection = ScrollDirectionRight;
    } else if (self.lastContentOffset < scrollView.contentOffset.x) {
        scrollDirection = ScrollDirectionLeft;
    }

    self.lastContentOffset = scrollView.contentOffset.x;

    // do whatever you need to with scrollDirection here.    
}

मैं दिशा को परिभाषित करने के लिए निम्नलिखित एनम का उपयोग कर रहा हूं। ScrollDirectionNone में पहला मान सेट करने से चर को इनिशियलाइज़ करते समय उस दिशा को डिफ़ॉल्ट बनाने का अतिरिक्त लाभ होता है:

typedef NS_ENUM(NSInteger, ScrollDirection) {
    ScrollDirectionNone,
    ScrollDirectionRight,
    ScrollDirectionLeft,
    ScrollDirectionUp,
    ScrollDirectionDown,
    ScrollDirectionCrazy,
};

1
मैं अंतिम कॉन्टेंटऑफसेट कैसे प्राप्त कर सकता हूं
Dev

@JasonZhao Heh - जब मैं कोड का गहनता से परीक्षण कर रहा था तो मुझे कुछ अजीब परिणाम मिल रहे थे क्योंकि मैंने इस बात पर ध्यान नहीं दिया था कि स्क्रॉलव्यू बाउंस के कारण स्क्रॉल दिशा..हर..बीज़ हो जाती है। इसलिए, जब मैंने अप्रत्याशित परिणाम पाए तो मैंने उसे एनम में शामिल कर लिया।
मेमों

1
@Dev यह कोड में हैlastContentOffset = scrollView.contentOffset.x;
memmons

@ akivag29 lastContentOffsetटाइप पर अच्छी पकड़ । संपत्ति बनाम स्थैतिक चर के बारे में: या तो समाधान एक अच्छा विकल्प है। मेरी कोई वास्तविक प्राथमिकता नहीं है। अच्छी बात है।
ज्ञापन

2
@JosueEspinosa सही स्क्रॉल दिशा प्राप्त करने के लिए, आप इसे स्क्रॉलव्यूबिलबिनरैग्रेगिंग में डालते हैं: अंतिम कॉलऑनसेटऑफ़सेट कॉल करने की विधि।
स्ट्रॉबेरी

73

... मैं यह जानना चाहता हूं कि उपयोगकर्ता किस दिशा (बाएं, दाएं) को स्क्रॉल करता है।

उस स्थिति में, iOS 5 और इसके बाद के संस्करण पर, UIScrollViewDelegateउपयोगकर्ता के पैन इशारे की दिशा निर्धारित करने के लिए उपयोग करें:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{ 
    if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x > 0) {
        // handle dragging to the right
    } else {
        // handle dragging to the left
    }
}

1
सबसे अच्छा समाधान हो सकता है, लेकिन वास्तव में धीमी शुरुआत के साथ, dx 0. के बराबर होगा
चालबाज77777

ज़रूर करता है। इसे अधिक तरीके से अपग्रेड किया जाना चाहिए क्योंकि यह अधिक "मूल" या मुख्य रूप से उचित तरीका है क्योंकि किसी भी संपत्ति-संग्रहीत मूल्यों का उपयोग करने की आवश्यकता है।
मिकी

क्या यह वही समस्या नहीं है जैसा कि stackoverflow.com/a/26192103/62 में है जहाँ यह सही ढंग से काम नहीं करेगा क्योंकि उपयोगकर्ता द्वारा पैनिंग बंद करने के बाद गति स्क्रॉल करना किक करता है?
लीरॉन याहदव

59

का उपयोग करते हुए scrollViewDidScroll:वर्तमान दिशा खोजने के लिए एक अच्छा तरीका है।

यदि आप उपयोगकर्ता द्वारा स्क्रॉलिंग समाप्त करने के बाद दिशा जानना चाहते हैं , तो निम्नलिखित का उपयोग करें:

@property (nonatomic) CGFloat lastContentOffset;

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    self.lastContentOffset = scrollView.contentOffset.x;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (self.lastContentOffset < scrollView.contentOffset.x) {
        // moved right
    } else if (self.lastContentOffset > scrollView.contentOffset.x) {
        // moved left
    } else {
        // didn't move
    }
}

3
यह एक बढ़िया अतिरिक्त जस्टिन है, हालांकि मैं एक संपादन जोड़ना चाहूंगा। यदि आपके स्क्रॉलव्यू में पेजिंग सक्षम है, और आप एक ड्रैग करते हैं, जो अपने प्रारंभिक स्थान पर वापस जा रहा है, तो वर्तमान "और" स्थिति यह विचार करेगी कि "लेफ्ट लेफ्ट" है, भले ही यह "नो चेंज" हो।
स्कॉट लिबरमैन

1
@ScottLieberman को आपका अधिकार, मैंने तदनुसार कोड अपडेट किया है।
जस्टिन टान्नर

50

इस पर नज़र रखने के लिए कोई अतिरिक्त चर जोड़ने की आवश्यकता नहीं है। बस का उपयोग UIScrollViewके panGestureRecognizerइस तरह संपत्ति। दुर्भाग्य से, यह केवल तभी काम करता है जब वेग 0 न हो:

CGFloat yVelocity = [scrollView.panGestureRecognizer velocityInView:scrollView].y;
if (yVelocity < 0) {
    NSLog(@"Up");
} else if (yVelocity > 0) {
    NSLog(@"Down");
} else {
    NSLog(@"Can't determine direction as velocity is 0");
}

आप ऊपर, नीचे, बाएँ और दाएँ का पता लगाने के लिए x और y घटकों के संयोजन का उपयोग कर सकते हैं।


9
दुर्भाग्य से यह सिर्फ खींचते समय काम करता है। वेग 0 है जब स्पर्श हटाए जाते हैं, तब भी जब स्क्रॉल किया जाता है।
हाइको

अच्छा +1। दूसरा भाग वेग 0 क्योंकि, उपयोगकर्ता अब और नहीं खींच रहा है, इसलिए इशारा वेग 0 है
पवन

आप चर में दिशा स्टोर कर सकते हैं। केवल वेग बदलने पर! = 0. मेरे लिए काम करता है
लेसज़ेक ज़र्ना


40

समाधान

func scrollViewDidScroll(scrollView: UIScrollView) {
     if(scrollView.panGestureRecognizer.translationInView(scrollView.superview).y > 0)
     {
         print("up")
     }
    else
    {
         print("down")
    } 
}

1
के velocityInViewबजाय के साथ एक कोशिश दे translationInView। यह समस्या को हल करता है जो एक ही आंदोलन में दिशा बदलते समय उत्पन्न होती है।
enreas

2
यह समाधान एकदम सही काम कर रहा है। जब मैं अगली स्क्रीन पर जाऊं और वापस आऊं तो स्क्रॉल अप और डाउन ऑपरेशंस में सबसे ज्यादा स्वीकृत उत्तर कुछ समय के लिए काम नहीं कर रहा है।
अंजनेयुलु बत्तुला

सबसे अच्छा, thnx!
बोहरिन

17

स्विफ्ट 4:

क्षैतिज स्क्रॉलिंग के लिए आप बस यह कर सकते हैं:

if scrollView.panGestureRecognizer.translation(in: scrollView.superview).x > 0 {
   print("left")
} else {
   print("right")
}

के साथ ऊर्ध्वाधर स्क्रॉलिंग परिवर्तन .xके लिए.y


14

IOS8 स्विफ्ट में मैंने इस विधि का उपयोग किया:

override func scrollViewDidScroll(scrollView: UIScrollView){

    var frame: CGRect = self.photoButton.frame
    var currentLocation = scrollView.contentOffset.y

    if frame.origin.y > currentLocation{
        println("Going up!")
    }else if frame.origin.y < currentLocation{
        println("Going down!")
    }

    frame.origin.y = scrollView.contentOffset.y + scrollHeight
    photoButton.frame = frame
    view.bringSubviewToFront(photoButton)

}

मेरे पास एक गतिशील दृश्य है जो स्थानों को बदल देता है क्योंकि उपयोगकर्ता स्क्रॉल करता है ताकि दृश्य ऐसा प्रतीत हो सके कि यह स्क्रीन पर उसी स्थान पर रुका है। जब उपयोगकर्ता ऊपर या नीचे जा रहा है तो मैं भी नज़र रख रहा हूं।

यहाँ भी एक वैकल्पिक तरीका है:

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if targetContentOffset.memory.y < scrollView.contentOffset.y {
        println("Going up!")
    } else {
        println("Going down!")
    }
}

1
Y के बजाय x का उपयोग करें?
एसकर्रॉथ

वास्तव में मैं UiCollectionView के लिए इसका पता लगाना चाहता हूं। मेरे पास क्षैतिज स्क्रॉलिंग है। तो मैं crollViewDidEndDeceleratingसही में पता लगाऊंगा ?
उमैर अफजल


7
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

    CGPoint targetPoint = *targetContentOffset;
    CGPoint currentPoint = scrollView.contentOffset;

    if (targetPoint.y > currentPoint.y) {
        NSLog(@"up");
    }
    else {
        NSLog(@"down");
    }
}

2
स्क्रॉल विधि के पेजिंग सक्षम संपत्ति का मूल्य हां होने पर इस विधि को नहीं कहा जाता है।
एको

5

वैकल्पिक रूप से, मुख्य पथ "contentOffset" का निरीक्षण करना संभव है। यह उपयोगी है जब आपके लिए स्क्रॉल दृश्य के प्रतिनिधि को सेट / बदलना संभव नहीं है।

[yourScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

प्रेक्षक जोड़ने के बाद, आप अब कर सकते हैं:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    CGFloat newOffset = [[change objectForKey:@"new"] CGPointValue].y;
    CGFloat oldOffset = [[change objectForKey:@"old"] CGPointValue].y;
    CGFloat diff = newOffset - oldOffset;
    if (diff < 0 ) { //scrolling down
        // do something
    }
}

जरूरत पड़ने पर प्रेक्षक को हटाने के लिए याद रखें। उदाहरण के लिए आप पर्यवेक्षक को व्यूप्लेऐपियर में जोड़ सकते हैं और इसे व्यूविलेस्पेयर में हटा सकते हैं


5

स्विफ्ट में:

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
        print("down")
    } else {
        print("up")
    }
}

आप इसे स्क्रॉलव्यूडीडस्कॉल में भी कर सकते हैं।


4

यहाँ व्यवहार के लिए मेरा समाधान है जैसे @followben उत्तर, लेकिन धीमी शुरुआत के साथ नुकसान के बिना (जब रंग 0 है)

@property (assign, nonatomic) BOOL isFinding;
@property (assign, nonatomic) CGFloat previousOffset;

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.isFinding = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.isFinding) {
        if (self.previousOffset == 0) {
            self.previousOffset = self.tableView.contentOffset.y;

        } else {
            CGFloat diff = self.tableView.contentOffset.y - self.previousOffset;
            if (diff != 0) {
                self.previousOffset = 0;
                self.isFinding = NO;

                if (diff > 0) {
                    // moved up
                } else {
                    // moved down
                }
            }
        }
    }
}

1

मैंने कुछ उत्तर की जाँच की और UIScrollView श्रेणी में एक बूंद में सब कुछ लपेटकर AnswerBot उत्तर पर विस्तार से बताया। "LastContentOffset" को इसके बजाय uiscrollview के अंदर सहेजा जाता है और फिर इसके कॉल करने की बस बात होती है:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  [scrollView setLastContentOffset:scrollView.contentOffset];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  if (scrollView.scrollDirectionX == ScrollDirectionRight) {
    //Do something with your views etc
  }
  if (scrollView.scrollDirectionY == ScrollDirectionUp) {
    //Do something with your views etc
  }
}

Https://github.com/tehjord/UIScrollViewScrollingDirection पर स्रोत कोड


1

मैं @ मेमन्स के उत्तर के आधार पर, कुछ फ़िल्टरिंग करना पसंद करता हूँ

उद्देश्य-सी में:

// in the private class extension
@property (nonatomic, assign) CGFloat lastContentOffset;

// in the class implementation
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (fabs(self.lastContentOffset - scrollView.contentOffset.x) > 20 ) {
        self.lastContentOffset = scrollView.contentOffset.x;
    }

    if (self.lastContentOffset > scrollView.contentOffset.x) {
        //  Scroll Direction Left
        //  do what you need to with scrollDirection here.
    } else {
        //  omitted 
        //  if (self.lastContentOffset < scrollView.contentOffset.x)

        //  do what you need to with scrollDirection here.
        //  Scroll Direction Right
    } 
}

जब में परीक्षण किया - (void)scrollViewDidScroll:(UIScrollView *)scrollView:

NSLog(@"lastContentOffset: --- %f,   scrollView.contentOffset.x : --- %f", self.lastContentOffset, scrollView.contentOffset.x);

img

self.lastContentOffset बहुत तेजी से बदलता है, मूल्य अंतर लगभग 0.5f है।

यह आवश्यक नहीं है।

और कभी-कभी, जब सटीक स्थिति में संभाला जाता है, तो आपका उन्मुखीकरण शायद खो जाता है। (कार्यान्वयन कथनों को कभी-कभी छोड़ दिया जाता है)

जैसे कि :

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

    CGFloat viewWidth = scrollView.frame.size.width;

    self.lastContentOffset = scrollView.contentOffset.x;
    // Bad example , needs value filtering

    NSInteger page = scrollView.contentOffset.x / viewWidth;

    if (page == self.images.count + 1 && self.lastContentOffset < scrollView.contentOffset.x ){
          //  Scroll Direction Right
          //  do what you need to with scrollDirection here.
    }
   ....

स्विफ्ट 4 में:

var lastContentOffset: CGFloat = 0

func scrollViewDidScroll(_ scrollView: UIScrollView) {

     if (abs(lastContentOffset - scrollView.contentOffset.x) > 20 ) {
         lastContentOffset = scrollView.contentOffset.x;
     }

     if (lastContentOffset > scrollView.contentOffset.x) {
          //  Scroll Direction Left
          //  do what you need to with scrollDirection here.
     } else {
         //  omitted
         //  if (self.lastContentOffset < scrollView.contentOffset.x)

         //  do what you need to with scrollDirection here.
         //  Scroll Direction Right
    }
}

0

जब पेजिंग चालू होती है, तो आप इन कोड का उपयोग कर सकते हैं।

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.lastPage = self.currentPage;
    CGFloat pageWidth = _mainScrollView.frame.size.width;
    self.currentPage = floor((_mainScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    if (self.lastPage < self.currentPage) {
        //go right
        NSLog(@"right");
    }else if(self.lastPage > self.currentPage){
        //go left
        NSLog(@"left");
    }else if (self.lastPage == self.currentPage){
        //same page
        NSLog(@"same page");
    }
}

0

कोड्स मुझे खुद ही बताते हैं। CGFloat अंतर 1 और अंतर 2 समान कक्षा निजी इंटरफ़ेस में घोषित किए गए। अगर अच्छा हो तो कंटेटसाइज एक जैसा ही रहता है।

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

        CGFloat contentOffSet = scrollView.contentOffset.y;
        CGFloat contentHeight = scrollView.contentSize.height;

        difference1 = contentHeight - contentOffSet;

        if (difference1 > difference2) {
            NSLog(@"Up");
        }else{
            NSLog(@"Down");
        }

        difference2 = contentHeight - contentOffSet;

       }

0

ठीक है मेरे लिए यह कार्यान्वयन वास्तव में अच्छा काम कर रहा है:

@property (nonatomic, assign) CGPoint lastContentOffset;


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _lastContentOffset.x = scrollView.contentOffset.x;
    _lastContentOffset.y = scrollView.contentOffset.y;

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (_lastContentOffset.x < (int)scrollView.contentOffset.x) {
        // moved right
        NSLog(@"right");
    }
    else if (_lastContentOffset.x > (int)scrollView.contentOffset.x) {
        // moved left
        NSLog(@"left");

    }else if (_lastContentOffset.y<(int)scrollView.contentOffset.y){
        NSLog(@"up");

    }else if (_lastContentOffset.y>(int)scrollView.contentOffset.y){
        NSLog(@"down");
        [self.txtText resignFirstResponder];

    }
}

तो यह ड्रैग एंड के बाद खारिज करने के लिए textView को फायर करेगा


0
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
NSLog(@"px %f py %f",velocity.x,velocity.y);}

स्क्रॉलव्यू की इस प्रतिनिधि पद्धति का उपयोग करें।

यदि y वेग का सह-समन्वय है + तो स्क्रॉल स्क्रॉल नीचे की ओर स्क्रॉल होता है और यदि यह -ve स्क्रॉलव्यू स्क्रॉल ऊपर की ओर होता है। इसी तरह बाएं और दाएं स्क्रॉल को एक्स-ऑर्डिनेट का उपयोग करके पता लगाया जा सकता है।


0

छोटा और आसान होगा, बस वेग मान की जाँच करें, यदि यह शून्य से अधिक है तो इसकी स्क्रॉलिंग दाईं ओर छोड़ दी गई है:

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    var targetOffset = Float(targetContentOffset.memory.x)
    println("TargetOffset: \(targetOffset)")
    println(velocity)

    if velocity.x < 0 {
        scrollDirection = -1 //scrolling left
    } else {
        scrollDirection = 1 //scrolling right
    }
}

0

यदि आप UIScrollView और UIPageControl के साथ काम करते हैं, तो यह विधि PageControl के पृष्ठ दृश्य को भी बदल देगी।

  func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    let targetOffset = targetContentOffset.memory.x
    let widthPerPage = scrollView.contentSize.width / CGFloat(pageControl.numberOfPages)

    let currentPage = targetOffset / widthPerPage
    pageControl.currentPage = Int(currentPage)
}

धन्यवाद @Esq स्विफ्ट कोड।


0

स्विफ्ट 2.2 सरल समाधान जो बिना किसी नुकसान के एकल और कई दिशाओं को ट्रैक करता है

  // Keep last location with parameter
  var lastLocation:CGPoint = CGPointZero

  // We are using only this function so, we can
  // track each scroll without lose anyone
  override func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    let currentLocation = scrollView.contentOffset

    // Add each direction string
    var directionList:[String] = []

    if lastLocation.x < currentLocation.x {
      //print("right")
      directionList.append("Right")
    } else if lastLocation.x > currentLocation.x {
      //print("left")
      directionList.append("Left")
    }

    // there is no "else if" to track both vertical
    // and horizontal direction
    if lastLocation.y < currentLocation.y {
      //print("up")
      directionList.append("Up")
    } else if lastLocation.y > currentLocation.y {
      //print("down")
      directionList.append("Down")
    }

    // scrolled to single direction
    if directionList.count == 1 {
      print("scrolled to \(directionList[0]) direction.")
    } else if directionList.count > 0  { // scrolled to multiple direction
      print("scrolled to \(directionList[0])-\(directionList[1]) direction.")
    }

    // Update last location after check current otherwise,
    // values will be same
    lastLocation = scrollView.contentOffset
  }

0

सभी ऊपरी उत्तरों में समस्या को हल करने के दो प्रमुख तरीके उपयोग कर रहे हैं panGestureRecognizerया contentOffset। दोनों विधियों में उनके विपक्ष और पेशेवरों हैं।

विधि 1: panGestureRecognizer

जब आप panGestureRecognizer@followben जैसे सुझाव का उपयोग करते हैं, यदि आप अपने स्क्रॉल दृश्य को प्रोग्रामेटिक रूप से स्क्रॉल नहीं करना चाहते हैं, तो यह ठीक से काम करता है।

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{ 
    if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x > 0) {
        // handle dragging to the right
    } else {
        // handle dragging to the left
    }
}

विपक्ष

लेकिन अगर आप निम्नलिखित कोड के साथ स्क्रॉल दृश्य को स्थानांतरित करते हैं, तो ऊपरी कोड इसे पहचान नहीं सकता है:

setContentOffset(CGPoint(x: 100, y: 0), animation: false)

विधि 2: contentOffset

var lastContentOffset: CGPoint = CGPoint.zero

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (self.lastContentOffset.x > scrollView.contentOffset.x) {
        // scroll to right
    } else if self.lastContentOffset.x < scrollView.contentOffset.x {
        // scroll to left
    }
    self.lastContentOffset = self.scrollView.contentOffset
}

विपक्ष

यदि आप सामग्री को बदलना चाहते हैं, तो प्रोग्रामिक रूप से, स्क्रॉल के दौरान (जैसे कि जब आप अनंत स्क्रॉल बनाना चाहते हैं), तो यह विधि समस्या बनाती है, क्योंकि हो सकता है contentOffsetकि आप सामग्री दृश्य स्थानों को बदलने के दौरान बदल जाएं और इस समय में, ऊपरी कोड जंप करें जिसमें आप दाईं ओर स्क्रॉल करें या बाएं।


0
//Vertical detection
    var lastVelocityYSign = 0
            func scrollViewDidScroll(_ scrollView: UIScrollView) {
                let currentVelocityY =  scrollView.panGestureRecognizer.velocity(in: scrollView.superview).y
                let currentVelocityYSign = Int(currentVelocityY).signum()
                if currentVelocityYSign != lastVelocityYSign &&
                    currentVelocityYSign != 0 {
                    lastVelocityYSign = currentVelocityYSign
                }
                if lastVelocityYSign < 0 {
                    print("SCROLLING DOWN")
                } else if lastVelocityYSign > 0 {
                    print("SCOLLING UP")
                }
            }

मोस 6 से जवाब https://medium.com/@Mos6yCanSwift/swift-ios-determine-scroll-direction-d48a2327a004

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.