iOS 开发之头部滚动展示视图

 

//

//  RootViewController.m

//  头部滚动展示视图

//  头部滚动广告视图

 

#define SCREEN_SIZE [UIScreen mainScreen].bounds.size

#define KImageCnt 5

#define KImage_H  250

 

#import "RootViewController.h"

 

@interface RootViewController ()<UIScrollViewDelegate>

 

@property (nonatomic, strong) UIScrollView * scrollView;

@property (nonatomic, strong) UIPageControl * pageControl;

@property (nonatomic, strong) NSTimer * timer;

 

@end

 

@implementation RootViewController

 

/**

 *  scrollView

 */

- (UIScrollView *)scrollView

{

    if (_scrollView == nil) {

        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, KImage_H)];

        _scrollView.pagingEnabled = YES;

        _scrollView.showsHorizontalScrollIndicator = NO;

    }

    

    return _scrollView;

}

 

/**

 *  PageControl

 */

- (UIPageControl *)pageControl

{

    if (_pageControl == nil) {

        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.scrollView.frame.size.height - 30, SCREEN_SIZE.width, 20)];

        

        _pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:65 / 255.0 green:168 / 255.0 blue:100/255.0 alpha:1.0];

        _pageControl.pageIndicatorTintColor = [UIColor grayColor];

    }

    return _pageControl;

}

 

/**

 *  初始化UI

 */

- (void)setupUI

{

    [self.view addSubview:self.scrollView];

    [self.view addSubview:self.pageControl];

    

    CGFloat imageY = 0;

    CGFloat imageW = SCREEN_SIZE.width;

    CGFloat imageH = KImage_H;

    

    for (int i = 0; i < KImageCnt; i++) {

        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * imageW, imageY, imageW, imageH)];

        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image_%i.jpg", i+1]];

        [self.scrollView addSubview:imageView];

    }

    

    // 设置scrollView属性

    self.scrollView.contentSize = CGSizeMake(KImageCnt * imageW, 0);

    

    self.scrollView.delegate = self;

    self.pageControl.numberOfPages = KImageCnt;

    

    // 设置自动播放

    [self turnOnTimer];

}

 

/**

 *  打开定时器

 */

- (void)turnOnTimer

{

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(playImage) userInfo:nil repeats:YES];

    

    // 为了防止单线程弊端

    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

}

 

/**

 *  关闭定时器

 */

- (void)turnOffTimer

{

    [self.timer invalidate];

    self.timer = nil;

}

 

/**

 *  自动播放图片

 */

- (void)playImage

{

    int i = self.pageControl.currentPage;

    if (i == KImageCnt - 1) {

        i = -1;

    }

    i++;

    [self.scrollView setContentOffset:CGPointMake(i * self.scrollView.frame.size.width, 0) animated:YES];

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self setupUI];

}

 

#pragma mark - UIScrollViewDelegate

 

/**

 *  用户准备拖拽的时候关闭定时器

 */

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    [self turnOffTimer];

}

 

/**

 *  用户停止拖拽的时候新开启一个定时器

 */

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

{

    [self turnOnTimer];

}

 

// 判断定时器滚动的时候 判断滚动的位置 以让pageControl显示当前的page

// 在总宽度上加上半个scrollView的宽度 是为了实现拖动到一半时左右的效果

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    self.pageControl.currentPage = (self.scrollView.frame.size.width * 0.5 + self.scrollView.contentOffset.x) / self.scrollView.frame.size.width;

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    

}

 

@end

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。