簡體   English   中英

具有自動滾動功能的ScrollView

[英]ScrollView with automatic scrolling feature

我想要滾動視圖(或textView)自動滾動,就像顯示電影的學分時一樣。 我已經嘗試了一些方法,但沒有一個通過,我似乎找不到足夠的答案,任何幫助將不勝感激!

提前致謝!

此代碼將在10秒內滾動您的UIScrollView 100pt:

self.scrollView.scrollEnabled = NO;

CGFloat scrollHeight = 100;
[UIView animateWithDuration:10
                      delay:0
                    options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     self.scrollView.contentOffset = CGPointMake(0, scrollHeight);
                 }
                 completion:nil];

試試這個。

[your table-name scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:pageval inSection:0] atScrollPosition:0 animated:YES];

indexPathForRow您可以傳遞行號並將其遞增直到行尾。

我認為這樣做會有所幫助。

希望這會有所幫助

**

當您使用textview時,您可以試試這個

**

count=txtvw.frame.size.height; //integer counter initialized as textview's height

//call "scrolltextview" method at regular time interval. (here it is calling method in 0.3 second timespan)
self.timer = [NSTimer scheduledTimerWithTimeInterval:.3f
                                                  target:self
                                                selector:@selector(scrolltextview)
                                                userInfo:nil
                                                 repeats:YES];



 -(void)scrolltextview
{


     //iterate "count" every time the method is called by the lineheight of textview's font.
        count=count+ txtvw.font.lineHeight;


    if(count<=txtvw.text.length)
    {
    NSRange range = NSMakeRange(count - 1, 1);

        [txtvw scrollRangeToVisible:range]; // scroll with range
    }
    else {
        [timer invalidate]; // if count match with the condition than invalidate the timer so method not called now.
    }

}



我在NSTimer對象中使用它。 希望這會有所幫助。

class AutoScrollView: UIScrollView {
    var timer: Timer?
    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        timer?.invalidate()
        timer = Timer.scheduledTimer(timeInterval: 1.0 / 30.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
    }
    @objc func timerAction() {
        contentOffset.y = contentOffset.y + 1
        if contentOffset.y >= contentSize.height - bounds.height {
            contentOffset.y = 0
        }
    }
}

上面會自動滾動滾動視圖並在結束時循環播放。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM