2012-07-19 16 views
22

के साथ मैं तेजी से स्वीप, like this के बाद तेजी से स्क्रॉल करने के लिए UIScrollView चाहता हूं। हालांकि, जब पेजिंग चालू होती है, तो स्क्रॉल one page at a time काम करता है। क्या तेजी से स्क्रॉल करना संभव है, जब इशारा पहचानकर्ताओं का उपयोग करके मैन्युअल रूप से कार्यान्वित किए बिना उंगली की झटके से पेजिंग सक्षम हो?UIScrollView की संवेदनशीलता/स्क्रॉल गति पेजिंग

+0

कभी इसका समाधान ढूंढें? – Eric

+0

मेरी इच्छा है कि इसका कोई अच्छा समाधान हो। ऐसा लगता है कि इसे कार्यान्वित करने के दो तरीके हैं: मैन्युअल पहचानकर्ताओं को मैन्युअल रूप से कार्यान्वित करना या मैन्युअल रूप से कार्यान्वयन पेजिंग। – UrK

+0

हां यह संभव है कि मैंने 6 महीने से पहले लागू किया था। आप मैन्युअल रूप से इशारा पहचानकर्ता बन सकते हैं .. – JohnWhite

उत्तर

40

यह काफी सरल है, हालांकि आपको पेजिंग पहलू को स्वयं लागू करना होगा (जो काफी सरल है)। आपको इशारा पहचानकर्ताओं की आवश्यकता नहीं है।

स्विफ्ट

पहले, अपने UIScrollView मंदी दर को समायोजित:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast 

मान लें कि हम सामग्री की एक सरणी है - यह yourPagesArray फोन करते हैं।

:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 
{ 
    //This is the index of the "page" that we will be landing at 
    let nearestIndex = Int(CGFloat(targetContentOffset.pointee.x)/scrollView.bounds.size.width + 0.5) 

    //Just to make sure we don't scroll past your content 
    let clampedIndex = max(min(nearestIndex, yourPagesArray.count - 1), 0) 

    //This is the actual x position in the scroll view 
    var xOffset = CGFloat(clampedIndex) * scrollView.bounds.size.width 

    //I've found that scroll views will "stick" unless this is done 
    xOffset = xOffset == 0.0 ? 1.0 : xOffset 

    //Tell the scroll view to land on our page 
    targetContentOffset.pointee.x = xOffset 
} 

आप निम्न प्रतिनिधि विधि को लागू करने, मामले को संभालने के लिए की आवश्यकता होगी, जहां उपयोगकर्ता किसी भी पुस्तक मंदी पैदा करने के बिना धीरे उनकी उंगली लिफ्टों: अपने UIScrollViewDelegate में, निम्न विधि को लागू (अद्यतन:।। आप नवीनतम आईओएस एसडीके के तहत ऐसा करने की जरूरत नहीं हो सकता है ऐसा लगता है ऊपर प्रतिनिधि विधि की तरह अब कहा जाता है जब वहाँ शून्य वेग)

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) 
{ 
    if !decelerate 
    { 
     let currentIndex = floor(scrollView.contentOffset.x/scrollView.bounds.size.width) 

     let offset = CGPoint(x: scrollView.bounds.size.width * currentIndex, y: 0) 

     scrollView.setContentOffset(offset, animated: true) 
    } 
} 

ऑब्जेक्टिव-सी

अपने मंदी दर निर्धारित करना:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast; 

फिर अपने स्क्रॉल दृश्य प्रतिनिधि कार्यान्वयन:

- (void) scrollViewWillEndDragging:(UIScrollView *)scroll withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    //This is the index of the "page" that we will be landing at 
    NSUInteger nearestIndex = (NSUInteger)(targetContentOffset->x/scrollView.bounds.size.width + 0.5f); 

    //Just to make sure we don't scroll past your content 
    nearestIndex = MAX(MIN(nearestIndex, yourPagesArray.count - 1), 0); 

    //This is the actual x position in the scroll view 
    CGFloat xOffset = nearestIndex * scrollView.bounds.size.width; 

    //I've found that scroll views will "stick" unless this is done 
    xOffset = xOffset==0?1:xOffset; 

    //Tell the scroll view to land on our page 
    *targetContentOffset = CGPointMake(xOffset, targetContentOffset->y); 
} 

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{ 
    if(!decelerate) 
    { 
     NSUInteger currentIndex = (NSUInteger)(scrollView.contentOffset.x/scrollView.bounds.size.width); 

     [scrollView setContentOffset:CGPointMake(scrollView.bounds.size.width * currentIndex, 0) animated:YES]; 
    } 
} 
1

मुझे लगता है कि हम scrollView.userInteractionEnabled सेट कर सकते हैं = नहीं जब हमारे उंगली स्पर्श यह। और फिर जब एनीमेशन बंद हो जाता है, तो इसे खोलें। यह मेरे लिए काम करता है। आशा है कि यह आपकी मदद करेगा।

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 
    scrollView.userInteractionEnabled = NO; 
} 

// at the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    scrollView.userInteractionEnabled = YES; 
} 
संबंधित मुद्दे