zoukankan      html  css  js  c++  java
  • iOS 开发之头部滚动展示视图

    效果:

    //

    //  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

  • 相关阅读:
    Thinkcmf:页面常用函数
    thinkcmf开发--关于控制器
    thinkcmf 常用操作
    Thinkcmf 二次开发
    Sublime Text 3 快捷键精华版
    php动态更改post_max_size, upload_max_filesize等值
    Jquery使用小技巧
    jQuery常用方法和函数
    三层架构
    JDBC
  • 原文地址:https://www.cnblogs.com/hanzhuzi/p/4384588.html
Copyright © 2011-2022 走看看