开发者

How to auto scroll UIScrollView using timer?

I have a UIScroll开发者_如何学运维View to show images. I need a timer to show the images one after another, after every 5 seconds. How to initiate UIScrollView event to move to the next image after 5 seconds using the timer?


Add the variable int h to your interface, and yourScrollView as a property. Then:

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

}


modifying the above code

 [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);
  }


  }


to get current value as w (x value) or h (y value) of content. This helps with manual/semi automatic scrolling with the current reference point in your content of your UIScollView.

CGFloat w = scroller.contentOffset.x;

or

CGFloat h = scroller.contentOffset.y;

then of course:

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


Swift version

Add the following properties

var offSet: CGFloat = 0

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

let imageArray = [your images]

Modify the 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)
    }
}

create this function for automatic scroll

    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)
    }
}

Add this function for manual scroll

    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
}

Note: Though the question is specific to automatic scroll, I have added manual scroll as well. So that both automatic scroll and manual scroll work in tandem. Also, I have added a UIPageControl. Please remove the imagePageControl if you don't need it.


Maybe, [scroll scrollRectToVisibile:animated:] is better than [scroll setContentOffset]. Because setContentOffset will lead to scrolling out of edge. Just have a try!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜