iOS开发 UI 自动轮播图

很多App里面都有轮播图,所以自己也试着写了一个,如果有什么不完善的地方,欢迎指点。

1.创建文件,RootView,RootViewController;

2.在RootView.h申明属性,.m里面设置:代码如下

#import "RootView.h"
#define Swidth CGRectGetWidth([UIScreen mainScreen].bounds)
#define sHeight CGRectGetHeight([UIScreen mainScreen].bounds)

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    self.backgroundColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 设置包含空间的大小
    self.scrollView.contentSize = CGSizeMake(Swidth * 10, sHeight);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentOffset = CGPointMake(Swidth, 0);
    [self addSubview:self.scrollView];
    
    //添加图片
    [self addImage:self.scrollView];
    
    // pageControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, sHeight - 50, Swidth, 50)];
    self.page.numberOfPages = 8;
    self.page.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:0.502 alpha:0.5];
    [self addSubview:self.page];
    
}

- (void)addImage:(UIView *)view{
//    UIImageView *imageView0 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"7.jpg"]];
//    imageView0.frame = CGRectMake(0, 0, Swidth, sHeight);
//    [view addSubview:imageView0];
    
    for (int i = 0; i < 10; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]]];
        imageView.frame = CGRectMake(Swidth * i, 0, Swidth, sHeight);
        [view addSubview:imageView];
    }
//    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(Swidth * 9, 0, Swidth, sHeight)];
//    imageView1.image = [UIImage imageNamed:@"0.jpg"];
//    [view addSubview:imageView1];
}

3.在主函数里面实现

#import "RootViewController.h"
#define Swidth CGRectGetWidth([UIScreen mainScreen].bounds)
#define sHeight CGRectGetHeight([UIScreen mainScreen].bounds)
@interface RootViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)NSTimer *timer;

@end

@implementation RootViewController

- (void)loadView{
    [super loadView];
    self.rv = [[RootView alloc]  initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.rv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.rv.scrollView.delegate = self;
    
    [self.rv.page addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    
    self.timer = [NSTimer timerWithTimeInterval:4.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    // Do any additional setup after loading the view.
}

- (void)pageChange:(UIPageControl *)page{
    
    self.rv.scrollView.contentOffset = CGPointMake(Swidth * (page.currentPage + 1), 0);
    [self.timer invalidate];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    int page = scrollView.contentOffset.x / Swidth;
//    NSLog(@"第%d张偏移", page);
    self.rv.page.currentPage = page - 1;
    if (page == 0) {
        self.rv.scrollView.contentOffset = CGPointMake(8 * Swidth, 0);
        self.rv.page.currentPage = 7;
        return;
    }
    if (page == 9) {
        self.rv.scrollView.contentOffset = CGPointMake(Swidth, 0);
        self.rv.page.currentPage = 0;
        return;
    }
    
    
}

- (void)nextImage
{
    NSInteger page = self.rv.page.currentPage + 1;
    if (page == self.rv.page.numberOfPages ) {
        page = 1;
    }else{
        page++;
    }
    
    [UIView animateWithDuration:1.0 animations:^{
        self.rv.scrollView.contentOffset = CGPointMake(page * Swidth, 0);
    }];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = scrollView.contentOffset.x / Swidth;
    self.rv.page.currentPage = page - 1;
    if (page == 0) {
        self.rv.scrollView.contentOffset = CGPointMake(8 * Swidth, 0);
        self.rv.page.currentPage = 7;
    }
    if (page == 9) {
        self.rv.scrollView.contentOffset = CGPointMake(Swidth, 0);
        self.rv.page.currentPage = 0;
    }

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值