2010-09-12 13 views

Odpowiedz

28

Dodaj zmienną int h do interfejsu, a yourScrollView jako własność. Następnie:

[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

jej nie przejść płynnie .. –

+6

płynnie animowanie zwój użyciu tej metody jest '[yourScrollView setContentOffset: CGPointMake (0 h) animowany: TAK]; ' – henryeverett

+0

jest on typu int H zmiennej ?? –

3

, aby uzyskać aktualną wartość jako w (x wartość) lub h (wartość y) zawartości. Pomaga to w ręcznym/półautomatycznym przewijaniu z aktualnym punktem odniesienia w treści twojego UIScollView.

CGFloat w = scroller.contentOffset.x; 

lub

CGFloat h = scroller.contentOffset.y; 

to oczywiście:

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

modyfikowanie wyżej kodu

[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

Może [przewijania scrollRectToVisibile: animowany:], jest lepsza niż [ przewiń setContentOffset]. Ponieważ setContentOffset spowoduje przewijanie poza krawędzią. Po prostu spróbuj!

2

wersja Swift

Dodaj następujące właściwości

var offSet: CGFloat = 0 

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

let imageArray = [your images] 

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

utworzyć tę funkcję automatycznego przewijania

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

Dodaj tę funkcję do ręcznego przewijania

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 
} 

Uwaga: Chociaż pytanie dotyczy tylko automatycznego przewijania, dodałem również ręczne przewijanie. Tak więc zarówno automatyczne przewijanie, jak i ręczne przewijanie działają w tandemie. Dodałem również UIPageControl. Proszę usunąć imagePageControl, jeśli go nie potrzebujesz.

Powiązane problemy