2011-09-01 5 views
6

uitextviewは、アプリケーションが起動されるたびに自動的にスクロールするようにします。誰かが詳細なコードで私を助けることができますか?私はiPhone SDKが初めてです。uitextviewをプログラムでスクロールさせる

+0

はどのように正確にあなたはそれがスクロールしたいですか?あなたはそれを最後までスクロールしたいのですか、途中の場所にスクロールしますか?それとも、上から下にゆっくりとスクロールしたいのですか? – mahboudz

+0

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

+0

私はuitextviewを上から下にゆっくりとスクロールしたいです。テキストビューに対話が許可されていません。つまり、ユーザーはテキストビューで何も編集できません。 – user919050

答えて

12

.hファイル

@interface Credits : UIViewController 
{ 
    NSTimer *scrollingTimer; 

    IBOutlet UITextView *textView; 


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

- (IBAction) buttonClicked ; 

- (void) autoscrollTimerFired; 

@end 

.mファイル

- (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ファイルの多くの場所で使用する必要がある変数..上記のコードでは、スクロールするTimer、textViewは.hファイルで宣言する必要があります – Maulik

+0

私はStackOverのフローで利用可能になります:D – Maulik

+1

ありがとうソリューション。カスタムキーボードを開発しました。このために、あなたのコードはUITextViewで私を助けました。 –

1

UITextViewはUIScrollviewから派生するので、-setContentOffset:animated:を使用してスクロール位置を設定できます。

スムーズに1秒間に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

NSTimerをインスタンス変数として宣言し、-viewWillAppearAnimated:で見栄えのよい繰り返し間隔で設定する必要があります。次に、-deallocと-viewWillDisappearAnimated:で無効にしてリリースしてください。 –

+0

コードが何をしているのか分からないので、完全なコードを与えることはできません。そのコードの背後にあるロジックを理解しようとしてください。それが何をしているのかは、特定の速度が与えられている場合は、下にスクロールすることです。 –

+0

.hファイルでは、他のインスタンス変数を宣言する場所で、次のようなことをする必要があります。NSTimer * scrollingTimer; @Maulikはタイマーの設定と無効化の方法を示しました。 –

関連する問題