2011-09-01 7 views
6

Tôi muốn tự động tạo uitextview để cuộn tự động bất cứ khi nào ứng dụng được khởi chạy. Bất cứ ai có thể giúp tôi với một mã chi tiết? Tôi mới sử dụng iPhone SDK.Đặt cuộn uitextview theo lập trình

+0

Làm thế nào Chính xác những gì bạn muốn nó di chuyển? Bạn có muốn nó cuộn đến cuối hoặc đến một điểm ở giữa không? Hay bạn muốn nó di chuyển chậm từ trên xuống dưới? – mahboudz

+0

có thể trùng lặp của http://stackoverflow.com/questions/1088960/iphone-auto-scroll-uitextview-but-allow-manual-scrolling-also – tipycalFlow

+0

tôi muốn cuộn uitextview từ trên xuống dưới, từ từ. và không có tương tác nào được phép cho chế độ xem văn bản. ý tôi là, người dùng không thể chỉnh sửa bất cứ điều gì trong textview. – user919050

Trả lời

12

.h tập tin

@interface Credits : UIViewController 
{ 
    NSTimer *scrollingTimer; 

    IBOutlet UITextView *textView; 


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

- (IBAction) buttonClicked ; 

- (void) autoscrollTimerFired; 

@end 

.m tập tin

- (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); 

} 

Hy vọng nó sẽ giúp bạn ....

+0

vâng ... các biến mà bạn cần sử dụng ở nhiều nơi trong tệp .m .. ở trên mã cuộnTimer, textView u phải khai báo trong .h tệp – Maulik

+0

tôi sẽ có sẵn trên luồng StackOver ..: D – Maulik

+1

Cảm ơn bạn đã giải pháp.Tôi đã phát triển một bàn phím tùy chỉnh và cho mã của bạn đã giúp tôi trong UITextView. –

1

UITextView xuất phát từ UIScrollview để bạn có thể đặt vị trí cuộn bằng cách sử dụng -setContentOffset: animated :.

Giả sử bạn muốn cuộn trơn tru với tốc độ 10 điểm mỗi giây, bạn sẽ làm điều gì đó tương tự.

- (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]; 
} 

Tất nhiên bạn phải cài đặt hẹn giờ và đảm bảo hủy cuộn khi chế độ xem biến mất, v.v.

+0

Bạn phải khai báo NSTimer dưới dạng biến mẫu và thiết lập nó với khoảng thời gian lặp lại đẹp trong -viewWillAppearAnimated :. Sau đó, hãy chắc chắn để làm mất hiệu lực và phát hành nó trong -dealloc và -viewWillDisappearAnimated :. –

+0

Tôi không thể cung cấp cho bạn mã đầy đủ, bởi vì tôi không chắc chắn bạn hiểu mã đang làm gì. Hãy chắc chắn cố gắng hiểu logic đằng sau mã đó. Những gì nó đang làm là chỉ để di chuyển đến phía dưới cho một tốc độ cụ thể. –

+0

Trong tệp .h của bạn, bạn phải làm một cái gì đó như thế này, nơi bạn khai báo các biến cá thể khác của bạn: NSTimer * scrollingTimer; @Maulik chỉ cho bạn cách thiết lập và làm mất hiệu lực bộ hẹn giờ. Mã số –