2010-09-12 47 views

回答

28

變量int^h添加到您的界面,並yourScrollView作爲屬性。然後:

[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 

- (void) onTimer { 

    // Updates the variable h, adding 100 (put your own value here!) 
    h += 100; 

    //This makes the scrollView scroll to the desired position 
    yourScrollView.contentOffset = CGPointMake(0, h); 

} 
+0

其不滾動順利.. – 2012-10-10 06:35:18

+6

爲了順利動畫滾動,使用這種方法: '[yourScrollView setContentOffset:CGPointMake(0,h)的動畫:YES];' – henryeverett 2012-12-18 11:47:25

+0

是它詮釋ħ變量的類型? – 2016-03-11 12:27:40

3

將內容的當前值作爲w(x值)或h(y值)。這有助於在您的UIScollView的內容中使用當前參考點進行手動/半自動滾動。

CGFloat w = scroller.contentOffset.x; 

CGFloat h = scroller.contentOffset.y; 

然後當然:

h += 100; // h-=100 for decrement 
yourScrollView.contentOffset = CGPointMake(0, h); 
7

修改上述代碼

[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 

-(void) onTimer { 
    // NSLog(@"content offset %f",scrollVw.contentOffset.y); 
    if (scrollVw.contentOffset.y<MAX_ALLOWED_OFFSET) { 
    //scroll to desire position 
    scrollVw.contentOffset = CGPointMake(0, scrollVw.contentOffset.y+1); 
    } 


    } 
0

也許,[滾動scrollRectToVisibile:動畫:]優於[滾動setContentOffset]。 因爲setContentOffset會導致滾動出邊緣。 只是有一個嘗試!

2

斯威夫特版本

添加以下屬性

var offSet: CGFloat = 0 

@IBOutlet weak var imagePageControl: UIPageControl! 
@IBOutlet weak var imageScrollView: UIScrollView! 

let imageArray = [your images] 

修改viewDidLoad()

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.offSet = 0 
    let timer = Timer.scheduledTimer(timeInterval: 8, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true) 

    imagePageControl.numberOfPages = bannerArray.count 
    imageScrollView.isPagingEnabled = true 
    imageScrollView.contentSize.height = 200 
    imageScrollView.contentSize.width = self.view.bounds.width * CGFloat(bannerArray.count) 
    imageScrollView.showsHorizontalScrollIndicator = false 

    imageScrollView.delegate = self 

    for (index, image) in imageArray.enumerated() { 
     let image = image 
     let imageView = UIImageView(image: image) 
     imageView.contentMode = .scaleAspectFill 
     imageView.frame.size.width = self.view.bounds.size.width 
     imageView.frame.size.height = 200 
     imageView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width 
     imageScrollView.addSubview(imageView) 
    } 
} 

自動滾動創建此功能

func autoScroll() { 
    let totalPossibleOffset = CGFloat(imageArray.count - 1) * self.view.bounds.size.width 
    if offSet == totalPossibleOffset { 
     offSet = 0 // come back to the first image after the last image 
    } 
    else { 
    offSet += self.view.bounds.size.width 
    } 
    DispatchQueue.main.async() { 
     UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: { 
      self.imageScrollView.contentOffset.x = CGFloat(self.offSet) 
     }, completion: nil) 
    } 
} 

添加此功能爲手動滾動

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    let page = scrollView.contentOffset.x/scrollView.frame.size.width 
    imagePageControl.currentPage = Int(page) 
    self.offSet = page * scrollView.frame.size.width // this updates offset value so that automatic scroll begins from the image you arrived at manually 
} 

注意:雖然問題是特定於自動滾動,我也添加了手動滾動以及。因此,自動滾動和手動滾動都可以協同工作。 此外,我添加了一個UIPageControl。如果你不需要它,請刪除imagePageControl