码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-04-01 19:47:51      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

效果:

 

//

//  RootViewController.m

//  头部滚动展示视图

//

//  Created by 寒竹子 on 15/4/1.

//  Copyright (c) 2015年 摩天居士. All rights reserved.

//  头部滚动广告视图

 

#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

 

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

标签:

原文地址:http://www.cnblogs.com/hanzhuzi/p/4384588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!