IOS_图片轮播器实现

本篇博客详细介绍了如何在iOS应用中实现大图滚动展示,并通过UIPageControl进行页面控制,包括创建UIScrollView、添加UIImageView、配置UIPageControl及定时滚动功能。

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

//
//  MYScrollViewController.m
//  大图滚动展示
//
//  Created by apple on 15/12/1.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "MYScrollViewController.h"

@interface MYScrollViewController ()<UIScrollViewDelegate>

@property(nonatomic, weak) UIScrollView *scrollView;
@property(nonatomic, weak) UIPageControl *pageControl;
@property(nonatomic, weak) NSTimer *timer;

@end

@implementation MYScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //添加UIScrollView添加到view
    [self setScrollVIewToView];
    
    //添加UIImageView到UIScrollView上
    [self setImageViewToScrollView:5];
    
    //添加UIPageControl到view上
    [self setPageControlToView];
    
    //创建timer
    [self createTimer];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIScrollView delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat offsetX = scrollView.contentOffset.x;
    
    int page = offsetX / self.scrollView.frame.size.width;
    
    self.pageControl.currentPage = page;
}

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    [self.timer invalidate];
}

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    [self createTimer];
}

#pragma mark - MySelf Methods

//创建UIScrollView添加到view
- (void) setScrollVIewToView{

    //创建UIScrollView
    UIScrollView *scroll = [[UIScrollView alloc] init];
    scroll.frame = CGRectMake(10, 50, self.view.frame.size.width - 20, 200);
    //设置scroll的delegate
    scroll.delegate = self;
    self.scrollView = scroll;
    scroll.backgroundColor = [UIColor purpleColor];
    //设置scroll的contentSize
    scroll.contentSize = CGSizeMake(3 * scroll.frame.size.width, 0);
    //实现UIScrollView的分页效果
    scroll.pagingEnabled = YES;
    //隐藏水平指示器
    scroll.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scroll];
}

//创建UIImageView添加到UIScrollView
- (void) setImageViewToScrollView:(int) imgNum{

    CGFloat imgW = self.view.frame.size.width - 20;
    CGFloat imgH = 200;
    CGFloat imgY = 0;
    
    //循环创建3个UIImageView
    for (int i = 0; i < imgNum; i++) {
        
        CGFloat imgX = i * imgW;
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
        NSString *imgName = [NSString stringWithFormat:@"%d", i+1];
        imgView.image  = [UIImage imageNamed:imgName];
        [self.scrollView addSubview:imgView];
    }
}

//创建UIPageControl到UIView
- (void) setPageControlToView{

    //pageControl
    UIPageControl *pageContr = [[UIPageControl alloc] init];
    pageContr.pageIndicatorTintColor = [UIColor blueColor];
    pageContr.currentPageIndicatorTintColor = [UIColor redColor];
    pageContr.currentPage = 0;
    pageContr.numberOfPages = self.scrollView.subviews.count;
    self.pageControl = pageContr;
    
    CGFloat pageContrX = 40;
    CGFloat pageContrY = CGRectGetMaxY(self.scrollView.frame) - 35;
    CGFloat pageContrW = self.view.frame.size.width - 80;
    CGFloat pageContrH = 35;
    
    pageContr.frame = CGRectMake(pageContrX, pageContrY, pageContrW, pageContrH);
    [self.view addSubview:pageContr];
}

//ScrollImage
- (void) scrollImage{

    NSInteger page = self.pageControl.currentPage;
    
    if (page == self.pageControl.numberOfPages - 1) {
        page = 0;
    }else{
        page++;
    }
    
    CGFloat offSet = page * self.scrollView.frame.size.width;
    [self.scrollView setContentOffset:CGPointMake(offSet, 0) animated:YES];
    
}

//创建timer
- (void) createTimer{

    //创建一个定时器
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
    self.timer = timer;
    
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值