带分页的自动UIScrollView

时间:2013-06-18 12:21:06

标签: objective-c timer uiscrollview nstimer uipagecontrol

我有一个启用了分页的UIScrollView。但是,我希望此滚动视图在经过一定时间后向右滚动。有没有一种简单的方法来实现这个或者我是否需要使用计时器并以编程方式滚动滚动视图?

像这个网站的标题有:http://denederlandsewateren.nl/ [注意文字“De Nederlandse Wateren”每隔x秒改变一次]

4 个答案:

答案 0 :(得分:14)

你需要一个小方法来帮助你滚动到确切的页面,这没有实现,但它很简单,让我们假设你的滚动视图被称为myScrollview

这假设您正在水平滚动,您可以切换代码以满足您的需求

-(void)scrollToPage:(NSInteger)aPage{
    float myPageWidth = [myScrollView frame].size.width;
    [myScrollView setContentOffset:CGPointMake(aPage*myPageWidth,y) animated:YES];
}

然后你设置一个计时器,每隔5秒调用一次这个方法,例如:

[[NSTimer scheduledTimerWithTimeInterval:5
                                  target:self
                                selector:@selector(scrollPages)
                                userInfo:Nil
                                 repeats:YES] fire];

下一步是创建将由计时器触发并滚动到下一页的方法,我将其称为-(void)scrollPages,它还要求您有两个名为currentPage和{的全局变量{1}},两者都是numberOfPages

类型
NSInteger

如果某些事情没有意义,请告诉我!

答案 1 :(得分:1)

在stackoverflow上找到了这个

[scrollView setContentOffset:CGPointMake(x, y) animated:YES];

要使用UIScrollView进行幻灯片放映,您可以在滚动视图中排列所有图像,设置重复计时器,然后设置-setContentOffset:animated:当计时器触发时。

答案 2 :(得分:0)

在.h文件中为scrollview和paging

创建出口
@property (weak, nonatomic) IBOutlet UIScrollView *sliderScrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *sliderPageControll;
@property (nonatomic,strong) NSArray *sliderImagesArray;
@property (nonatomic,strong) NSString *sliderIndexString;
@property (nonatomic,strong) NSString *currentPageIndexString;
之后在.m文件中

[self.sliderScrollView setPagingEnabled:YES];
[self.sliderScrollView setBounces:NO];
[self.sliderScrollView setAlwaysBounceVertical:NO];
[self.sliderScrollView setShowsHorizontalScrollIndicator:NO];
self.sliderScrollView.alwaysBounceVertical = NO;
NSArray *imagesArray = self.sliderImagesArray;
for (int i = 0; i < [imagesArray count]; i++){
            CGFloat xOrigin = i * self.imageSliderScrollView.frame.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.sliderScrollView.frame.size.width, self.sliderScrollView.frame.size.height)];
                imageView.userInteractionEnabled = YES;
    NSString *imgStr = [serviceImgArray objectAtIndex:i];
    NSString *bikeStr = [NSString stringWithFormat:@"%@", imgStr];
    NSURL *imageURL = [NSURL URLWithString:[bikeStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [imageView sd_setImageWithURL:imageURL
                       placeholderImage:[UIImage imageNamed:@"profilepic_bg"]];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
                [self.sliderScrollView addSubview:imageView];
}
// for adding autorotate scrolling
   [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(autoStartSlide:) userInfo:nil repeats:YES];

self.sliderPageControll.numberOfPages = imagesArray.count;
self.sliderPageControll.currentPage = 0;
self.currentPageIndexString = [NSString stringWithFormat:@"%ld",(long)0];
[self.sliderScrollView setContentSize:CGSizeMake(self.sliderScrollView.frame.size.width * [imagesArray count], self.sliderScrollView.frame.size.height)];

if ([self.sliderPageControll respondsToSelector:@selector(setPageIndicatorTintColor:)]) {
    self.sliderPageControll.currentPageIndicatorTintColor = [UIColor redColor];
    self.sliderPageControll.pageIndicatorTintColor = [UIColor whiteColor];
}

CGRect frame = self.sliderScrollView.frame;
frame.origin.x = frame.size.width * [[NSString stringWithFormat:@"%@",self.sliderIndexString] integerValue];
frame.origin.y = 0;
[self.sliderScrollView scrollRectToVisible:frame animated:NO];
self.sliderPageControll.currentPage = [[NSString stringWithFormat:@"%@",self.sliderIndexString] integerValue];

之后实现scrollview委托方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSInteger scrollIndex = (NSInteger)(self.sliderScrollView.contentOffset.x / self.sliderScrollView.frame.size.width);
self.sliderPageControll.currentPage = scrollIndex;
self.currentPageIndexString = [NSString stringWithFormat:@"%ld",(long)self.sliderPageControll.currentPage];
}

这里实现你的autotrotate方法。

 - (void)autoStartSlide:(id)sender {
if(pagger.numberOfPages > pagger.currentPage+1) {
    [UIView animateWithDuration:0.5 animations:^{
        slider.contentOffset = CGPointMake(slider.contentOffset.x + slider.frame.size.width, 0);
    } completion:^(BOOL finished) {

    }];
}
else {
    slider.contentOffset = CGPointMake(slider.contentOffset.x - slider.frame.size.width, 0);
    [UIView animateWithDuration:0.5 animations:^{
        slider.contentOffset = CGPointMake(slider.contentOffset.x + slider.frame.size.width, 0);
    } completion:^(BOOL finished) {
        slider.contentOffset = CGPointMake(0, 0) ;
    }];
}
}

答案 3 :(得分:0)

这是一个兼容 Swift 5 的

var currentPage = 1;
var slideTimer : Timer?

ViewDidLoad

 slideTimer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(scrollPages), userInfo: nil, repeats: true)

viewDidDisappear

slideTimer?.invalidate()

功能

@objc func scrollPages() {
        scrollToPage(page: CGFloat(currentPage%slides.count));
        currentPage+=1;
    }
    
    func scrollToPage(page : CGFloat) {
        let pageWidth = scrollView.frame.width
        scrollView .setContentOffset(CGPoint(x: pageWidth * page, y: 0.0), animated: true)
    }