我有一个启用了分页的 UIScrollView。但是,我希望此滚动视图在经过一定时间后向右滚动。有没有一种简单的方法来实现这一点,还是我需要使用计时器并以编程方式滚动滚动视图?
这个网站的标题中有类似的内容:http: //denederlandsewateren.nl/ [注意文本“De Nederlandse Wateren”每 x 秒更改一次]
我有一个启用了分页的 UIScrollView。但是,我希望此滚动视图在经过一定时间后向右滚动。有没有一种简单的方法来实现这一点,还是我需要使用计时器并以编程方式滚动滚动视图?
这个网站的标题中有类似的内容:http: //denederlandsewateren.nl/ [注意文本“De Nederlandse Wateren”每 x 秒更改一次]
您需要一个小方法来帮助您滚动到确切的页面,这没有实现,但它非常简单,假设您的滚动视图被调用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
and的全局变量numberOfPages
,两者都是类型NSInteger
-(void)scrollPages{
[self scrollToPage:currentPage%numberOfPages];
currentPage++;
}
如果有什么不明白的,请告诉我!
在stackoverflow上找到了这个
[scrollView setContentOffset:CGPointMake(x, y) animated:YES];
要使用 UIScrollView 进行幻灯片放映,您可以在滚动视图中排列所有图像,设置重复计时器,然后 -setContentOffset:animated: 当计时器触发时。
在您的 .h 文件中为滚动视图和分页创建出口
@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];
之后实现滚动视图委托方法
- (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) ;
}];
}
}
这是一个兼容 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)
}