iOS 有关自动轮播图片

时间:2022-01-19 16:11:12
  //初始化当前视图
_currentImageView = [[UIImageView alloc] init];
[_currentImageView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:]]];
_currentImageView.frame = CGRectMake(myScroller.frame.size.width, , myScroller.frame.size.width,);
// _currentImageView.contentMode = UIViewContentModeScaleAspectFill;
[myScroller addSubview:_currentImageView]; _nextImageView = [[UIImageView alloc] init];
[_nextImageView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:]]];
_nextImageView.frame = CGRectMake(myScroller.frame.size.width * , , myScroller.frame.size.width,);
// _nextImageView.contentMode = UIViewContentModeScaleAspectFill;
[myScroller addSubview:_nextImageView]; //初始化上一个视图
_previousView = [[UIImageView alloc] init];
[_previousView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:]]];
_previousView.frame = CGRectMake(, ,myScroller.frame.size.width,);
// _previousView.contentMode = UIViewContentModeScaleAspectFill; [myScroller addSubview:_previousView]; } {
SMPageControl *pageControl = [[SMPageControl alloc] init];
[pageControl setFrame:CGRectMake(, myScroller.frame.size.height+ - 10.0f,myScroller.frame.size.width,10.0f)];
[pageControl setNumberOfPages:];
[pageControl setBackgroundColor:[UIColor clearColor]];
[pageControl setCurrentPage:];
[pageControl setCurrentPageIndicatorTintColor:[UIColor lightGrayColor]];
[pageControl setPageIndicatorTintColor:[UIColor grayColor]];
myPageControl=pageControl;
[self.view addSubview:myPageControl];
} if (![myTimer isValid]) {
myTimer=[NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(updateAuto) userInfo:nil repeats:YES];
} #pragma mark 时钟动画调用方法
- (void)updateAuto
{
if (_isDraging) {
return;
} CGPoint offset = myScroller.contentOffset; offset.x += self.view.frame.size.width;
if (offset.x > self.view.frame.size.width*) {
offset.x = self.view.frame.size.width;
} [myScroller setContentOffset:offset animated:YES];
} #pragma mark - 代理方法
#pragma mark 准备开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
_isDraging = YES;
} #pragma mark 视图停止滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
_isDraging = NO;
} #pragma mark 已经拖动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ static int i = ;//当前展示的是第几张图片 float offset = scrollView.contentOffset.x;
if (_nextImageView.image == nil || _previousView.image == nil) { //加载下一个视图
[_nextImageView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:i == kCount ? : i+]]]; //加载上一个视图
[_previousView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:i == ? kCount : i - ]]];
} if (offset == ) {
_currentImageView.image = _previousView.image;
scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, );
_previousView.image = nil; if (i == ) {
i = kCount;
}else{
i -= ;
} } if (offset == scrollView.bounds.size.width * ) {
_currentImageView.image = _nextImageView.image;
scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, );
_nextImageView.image = nil; if (i == kCount) {
i = ; //如果当前图片索引在最后一个,则重置为0,否则+1
}else{
i += ;
}
}
[myPageControl setCurrentPage:i];
}