2011-09-01 20 views
6

मैं अपना uitextview एप्लिकेशन लॉन्च होने पर स्वचालित रूप से स्क्रॉल करना चाहता हूं। क्या कोई मुझे विस्तृत कोड के साथ मदद कर सकता है? मैं आईफोन एसडीके के लिए नया हूँ।प्रोग्रामिंग के रूप में uitextview स्क्रॉल बनाना

+0

वास्तव में किस प्रकार आप इसे स्क्रॉल करना चाहते हैं? क्या आप इसे अंत तक स्क्रॉल करना चाहते हैं, या बीच में एक स्थान पर? या आप इसे धीरे-धीरे ऊपर से नीचे तक स्क्रॉल करना चाहते हैं? – mahboudz

+0

http://stackoverflow.com/questions/1088960/iphone-auto-scroll-uitextview-but-allow-manual-scrolling-also – tipycalFlow

+0

का संभावित डुप्लिकेट मैं धीरे-धीरे ऊपर से नीचे तक uitextview को स्क्रॉल करना चाहता हूं। और पाठ्यदृश्य के लिए कोई बातचीत की अनुमति नहीं है। मेरा मतलब है, उपयोगकर्ता टेक्स्टव्यू में कुछ भी संपादित नहीं कर सकता है। – user919050

उत्तर

12

ज फ़ाइल

@interface Credits : UIViewController 
{ 
    NSTimer *scrollingTimer; 

    IBOutlet UITextView *textView; 


} 
@property (nonatomic , retain) IBOutlet UITextView *textView; 

- (IBAction) buttonClicked ; 

- (void) autoscrollTimerFired; 

@end 

मीटर फ़ाइल

- (void) viewDidLoad 
{  
    // it prints the initial position of text view 
    NSLog(@"%f %f",textView.contentSize.width , textView.contentSize.height); 

    if (scrollingTimer == nil) 
    { 
     // A timer that updates the content off set after some time so it can scroll 
     // you can change time interval according to your need (0.06) 
     // autoscrollTimerFired is the method that will be called after specified time interval. This method will change the content off set of text view 
     scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06) 
         target:self selector:@selector(autoscrollTimerFired) userInfo:nil repeats:YES];   
    } 
} 

- (void) autoscrollTimerFired 
{ 
    CGPoint scrollPoint = self.textView.contentOffset; // initial and after update 
    NSLog(@"%.2f %.2f",scrollPoint.x,scrollPoint.y); 
    if (scrollPoint.y == 583) // to stop at specific position 
    { 
     [scrollingTimer invalidate]; 
     scrollingTimer = nil; 
    } 
    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1); // makes scroll 
    [self.textView setContentOffset:scrollPoint animated:NO]; 
    NSLog(@"%f %f",textView.contentSize.width , textView.contentSize.height); 

} 

आशा है कि यह आप में मदद करता है ....

+0

हाँ ... वेरिएबल्स जिन्हें आपको .m फ़ाइल में कई स्थानों पर उपयोग करने की आवश्यकता है .. उपरोक्त कोड स्क्रॉलिंग में टाइमर, टेक्स्ट व्यू आपको .h फ़ाइल – Maulik

+0

में घोषित करना होगा। मैं स्टैक ओवर प्रवाह पर उपलब्ध होगा ..: डी – Maulik

+1

के लिए धन्यवाद समाधान।मैंने एक कस्टम कीबोर्ड विकसित किया और इसके लिए आपके कोड ने मुझे UITextView में मदद की। –

1

UITextView UIScrollview से निकला है ताकि आप -setContentOffset का उपयोग कर स्क्रॉलिंग स्थिति सेट कर सकें: एनिमेटेड:।

मान लीजिए कि आप प्रति सेकंड 10 अंकों की गति से आसानी से स्क्रॉल करना चाहते हैं, तो आप ऐसा कुछ करेंगे।

- (void) scrollStepAnimated:(NSTimer *)timer { 
    CGFloat scrollingSpeed = 10.0; // 10 points per second 
    NSTimeInterval repeatInterval = [timer timeInterval]; // ideally, something like 1/30 or 1/10 for a smooth animation 

    CGPoint newContentOffset = CGPointMake(self.textView.contentOffset.x, self.textView.contentOffset.y + scrollingSpeed * repeatInterval); 
    [self.textView setContentOffset:newContentOffset animated:YES]; 
} 

बेशक आपको टाइमर सेट करना होगा और दृश्य गायब होने पर स्क्रॉलिंग को रद्द करना सुनिश्चित करना होगा।

+0

आपको एनएसटीमर को इंस्टेंस वैरिएबल के रूप में घोषित करना होगा और इसे एक अच्छी दिखने वाली दोहराव अंतराल के साथ सेट करना होगा -व्यूविल्लएयरएनिएटेड:। फिर इसे अमान्य करना और इसे रिलीज़ करना सुनिश्चित करें- -वॉलोक और -दृश्य WillDisappear अनुमानित:। –

+0

मैं आपको पूरा कोड नहीं दे सकता, क्योंकि मुझे यकीन नहीं है कि आप समझते हैं कि कोड क्या कर रहा है। उस कोड के पीछे तर्क को समझने का प्रयास करना सुनिश्चित करें। यह एक विशिष्ट गति के नीचे नीचे स्क्रॉल करने के लिए बस क्या कर रहा है। –

+0

आपकी .h फ़ाइल में आपको ऐसा कुछ करना है जहां आप अपना अन्य इंस्टेंस चर घोषित करते हैं: NSTimer * scrollingTimer; @ मौलिक ने आपको दिखाया कि टाइमर को कैसे सेट अप और अमान्य करना है। –

संबंधित मुद्दे