iOS最笨的办法实现无限轮播图(网络加载)

本文介绍了一种在iOS应用中实现无限轮播图的方法。通过自定义UIView,并利用UIScrollView及NSTimer来达到轮播效果。文章提供了完整的代码实现,包括图片加载、页面控制器的显示与更新等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS最笨的办法实现无限轮播图(网络加载)

简单的做了一下:

使用方法: 把 请求返回的 图片地址(字符串类型)放进数组中就行 
可以使用SDWebImage(我就是用的这个)等。。需要自己导入并引用,然后修改部分代码 
.h文件

//  ScrollViewTimerView.h
//  ScrollViewTimer
//
//  Created by 郑鹏 on 2016/12/9.
//  Copyright © 2016年 郑鹏. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol ScrollViewTimerViewDelegate <NSObject>

- (void)didSelectScrollViewWithSelectIndex:(NSInteger)selectIndex;

@end

@interface ScrollViewTimerView : UIView
@property (nonatomic, weak) id<ScrollViewTimerViewDelegate> littleSunDelegate;

- (instancetype)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration;
@property (nonatomic, strong) NSMutableArray * wheelImgArray;//轮播图 数组

//是否需要
@property (nonatomic, assign) BOOL isHidePageControl;



@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

.m文件

//
//  ScrollViewTimerView.m
//  ScrollViewTimer
//
//  Created by 郑鹏 on 2016/12/9.
//  Copyright © 2016年 郑鹏. All rights reserved.
//

#import "ScrollViewTimerView.h"
#import "UIImageView+WebCache.h"

@interface ScrollViewTimerView ()<UIScrollViewDelegate>{

    NSTimer *_animationTimer;
    UIScrollView *_littleSunScrollView;
    UIPageControl *_pageControl;
}

@end

@implementation ScrollViewTimerView

- (instancetype)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration{
    self = [super initWithFrame:frame];

    _littleSunScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    _littleSunScrollView.delegate = self;
    _littleSunScrollView.pagingEnabled = YES;
    _littleSunScrollView.showsHorizontalScrollIndicator = NO;
    _littleSunScrollView.showsVerticalScrollIndicator = NO;
    _littleSunScrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    [self addSubview:_littleSunScrollView];
    _animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationDuration
                                                           target:self
                                                         selector:@selector(animationTimerDidFired:)
                                                         userInfo:nil
                                                          repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:_animationTimer forMode:UITrackingRunLoopMode];
    [self pauseTimer];

    return self;
}
- (void)animationTimerDidFired:(NSTimer *)timer{
    CGPoint newOffset = CGPointMake(_littleSunScrollView.contentOffset.x  + CGRectGetWidth(self.frame), _littleSunScrollView.contentOffset.y);
    [_littleSunScrollView setContentOffset:newOffset animated:YES];

    if (_littleSunScrollView.contentOffset.x >= (_wheelImgArray.count+1)*self.frame.size.width) {
        [self startTimerAfterTimeInterval:-2.0f];
        [_littleSunScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
    }
}


#pragma mark- UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self pauseTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self startTimerAfterTimeInterval:2.0f];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (_littleSunScrollView.contentOffset.x >= (_wheelImgArray.count+1)*self.frame.size.width) {
        [_littleSunScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
    }else if (_littleSunScrollView.contentOffset.x <= 0) {
        [_littleSunScrollView setContentOffset:CGPointMake(_wheelImgArray.count*self.frame.size.width, 0) animated:NO];
    }
    if (!_isHidePageControl) {
        _pageControl.currentPage = [self getCurrentPageBy:scrollView.contentOffset.x];
    }
}



//公开的属性设置
- (void)setWheelImgArray:(NSMutableArray *)wheelImgArray{
    if (_wheelImgArray != wheelImgArray) {
        _wheelImgArray = wheelImgArray;
        _littleSunScrollView.contentSize = CGSizeMake(self.frame.size.width * (wheelImgArray.count + 2), self.frame.size.height);
        for (NSInteger i = 0; i < wheelImgArray.count+2; i++) {
            if (i == 0) {
                [self creatImgViewWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) WithTag:20161212 + wheelImgArray.count - 1 WithImgName:wheelImgArray[wheelImgArray.count - 1]];
            }else if(i !=  wheelImgArray.count+1){
                [self creatImgViewWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height) WithTag:20161212 + i-1 WithImgName:wheelImgArray[i-1]];
            }else if(i == wheelImgArray.count+1){
                [self creatImgViewWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height) WithTag:20161212 WithImgName:wheelImgArray[0]];
            }
        }


        _pageControl = [[UIPageControl alloc] init];
        _pageControl.numberOfPages = _wheelImgArray.count;
        _pageControl.frame = CGRectMake(0, self.frame.size.height - 20, self.frame.size.width, 10);
        _pageControl.currentPage = 0;
        _pageControl.userInteractionEnabled = NO;
        [_pageControl setPageIndicatorTintColor:[UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:0.5]];
        [_pageControl setCurrentPageIndicatorTintColor:[UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:1]];
        [self addSubview:_pageControl];

        [self startTimerAfterTimeInterval:2.0f];
    }
}
- (void)creatImgViewWithFrame:(CGRect)frame WithTag:(NSInteger)tag WithImgName:(NSString *)imgName{

    UIImageView *imgView = [[

转载于:https://www.cnblogs.com/shenlaiyaoshi/p/8497105.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值