UITextView
UIScrollView
का एक उपवर्ग है, इसलिए जवाब contentOffset
संपत्ति है, जो क्या बदला जा रहा है, सन्निवेश वाली नहीं या सामग्री आकार है शामिल है। यदि पहली बार दृश्य दिखाई देने पर स्क्रॉल स्थिति सही होती है, तो आप बाद में याद करने के लिए सामग्री ऑफ़सेट स्टोर कर सकते हैं।
YourViewController.h कतरना
@interface YourViewController : UIViewController <UITextViewDelegate, UIScrollViewDelegate>
@property(nonatomic, weak) IBOutlet UITextView *textView;
@end
YourViewController.m झलकी
@implementation YourViewController {
@private
BOOL _freezeScrolling;
CGFloat _lastContentOffsetY;
}
// UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
// tell the view to hold the scrolling
_freezeScrolling = YES;
_lastContentOffsetY = self.textView.contentOffset.y;
}
// UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView {
_freezeScrolling = NO;
}
// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (_freezeScrolling) {
// prevent the scroll view from actually scrolling when we don't want it to
[self repositionScrollView:scrollView newOffset:CGPointMake(scrollView.contentOffset.x, _lastContentOffsetY)];
}
}
// UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// scroll prevention should only be a given scroll event and turned back off afterward
_freezeScrolling = NO;
}
// UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
// when the layout is redrawn, scrolling animates. this ensures that we are freeing the view to scroll
_freezeScrolling = NO;
}
/**
This method allows for changing of the content offset for a UIScrollView without triggering the scrollViewDidScroll: delegate method.
*/
- (void)repositionScrollView:(UIScrollView *)scrollView newOffset:(CGPoint)offset {
CGRect scrollBounds = scrollView.bounds;
scrollBounds.origin = offset;
scrollView.bounds = scrollBounds;
}
क्या भी कोड नमूने में ध्यान देना महत्वपूर्ण है के ऊपर अंतिम विधि है। किसी भी प्रकार के setContentOffset:
को कॉल करना वास्तव में स्क्रॉलिंग ट्रिगर करेगा, जिसके परिणामस्वरूप scrollViewDidScroll:
पर कॉल किया जाएगा। तो setContentOffset:
पर कॉल करना एक अनंत लूप में परिणाम देता है। स्क्रॉल सीमा निर्धारित करना इसके लिए कामकाज है।
संक्षेप में, हम दृश्य नियंत्रक को स्क्रॉलिंग से UITextView
को रोकने के लिए बताते हैं जब हम पाते हैं कि उपयोगकर्ता ने संपादन के लिए टेक्स्ट चुना है। हम वर्तमान सामग्री ऑफ़सेट भी स्टोर करते हैं (क्योंकि हम जानते हैं कि स्थिति वह है जो हम चाहते हैं)। यदि UITextView
स्क्रॉल करने का प्रयास करता है, तो स्क्रॉल बंद होने तक सामग्री को ऑफसेट होने पर हम scrollViewDidEndDecelerating:
या scrollViewDidEndScrollingAnimation:
ट्रिगर करते हैं)। जब उपयोगकर्ता संपादन कर लेता है तो हम स्क्रॉलिंग को भी अनफ्रीज़ करते हैं।
याद रखें, यह एक मूल उदाहरण है, इसलिए आपको इच्छित सटीक व्यवहार के आधार पर कोड को ट्विक करने की आवश्यकता होगी।
एसडीके का कौन सा संस्करण आप उपयोग कर रहे हैं? 3.0? – hanleyp
हां, मैं एसडीके 3.0 – Gero