当先锋百科网

首页 1 2 3 4 5 6 7

本章实现效果一:
HTScrollView

核心代码:

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"滚动视图";
    [self createScrollView];
}

/**
 *  滚动视图只是将比它大的UIImageView视图,通过拖拽效果,将UIImageView上的图片信息完整的展示给用户
 *  为了让滚动效果明显,添加图片视图的宽或者高至少比滚动视图的宽高多一个像素
 */
- (void)createScrollView {
    //创建对象
    UIScrollView * scrollView = [[UIScrollView alloc] init];
    //显示位置
    scrollView.frame = CGRectMake(, , self.view.bounds.size.width-, );
    //添加背景颜色
    scrollView.backgroundColor = [UIColor orangeColor];
    //添加UIImageView
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
    imageView.image = [UIImage imageNamed:@"timg.jpg"];
    [scrollView addSubview:imageView];

    //设置滚动视图的滚动范围, 内容视图的大小
    scrollView.contentSize = imageView.frame.size;
    //关闭自动调节显示位置的属性
    self.automaticallyAdjustsScrollViewInsets = NO;
    //关闭边框回弹效果
    scrollView.bounces = NO;
    //隐藏滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    //设置内容视图的偏移位置,默认内容视图显示在滚动视图区域的坐标是在内容视图的(0,0)点位置
    scrollView.contentOffset = CGPointMake(, );
    //设置滚动视图的内容视图的缩放效果
    scrollView.delegate = self;
    //设置缩放的倍数
    scrollView.minimumZoomScale = ;
    scrollView.maximumZoomScale = ;
    [self.view addSubview:scrollView];
}

#pragma mark - UIScrollViewDelegate
/**
 *  最常用的协议方法,没有时间点反复调用,当滚动视图滚动时,一直调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"x = %f_____y = %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}

/**
 *  设置滚动视图的缩放效果
 */
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [[scrollView subviews] objectAtIndex:];
}

/**
 *  滚动视图拖拽开始调用该方法
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    //获取的是该方法的名称
    NSLog(@"%@",NSStringFromSelector(_cmd));
}

/**
 *  滚动视图拖拽结束调用该方法
 *  不能在该方法中获取最终滚动视图停止内容视图的偏移位置的值,也就是contentoffSet的值
 *  因为滚动视图拖拽结束的时候,还要经历一个内容视图缓慢减速停止的方法,应该在减速停止的方法里获取contentoffSet的值
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"%@",NSStringFromSelector(_cmd));
}

/**
 *  滚动视图减速停止调用的方法
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"%@",NSStringFromSelector(_cmd));
}

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

@end

本章实现效果二:
轮播图

核心代码:

#import "ViewController.h"
#import "HTConfig.h"

@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createScrollView];
}

- (void)createScrollView {
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake( * SCREEN_width, SCREEN_height);
    //设置内容视图初始状态下的偏移位置
    scrollView.contentOffset = CGPointMake(SCREEN_width, );

    for (int i = ; i < ; i++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_width * i, , SCREEN_width, SCREEN_height)];
        if (i == ) {
            imageView.image = [UIImage imageNamed:@"5.png"];
        }else if (i == ) {
            imageView.image = [UIImage imageNamed:@"1.png"];
        }else {
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
        }
        [scrollView addSubview:imageView];
    }

    scrollView.pagingEnabled = YES;
    //设置代理
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x ==  * SCREEN_width) {
        scrollView.contentOffset = CGPointMake(SCREEN_width, );
    } else if (scrollView.contentOffset.x == ) {
        scrollView.contentOffset = CGPointMake( * SCREEN_width, );
    }
}

@end

本章实现效果三:
分页控件

核心代码:

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController {
    UIScrollView * _scrollView;
    UIPageControl * _pageCtr;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"分页控件";
    [self createUI];
}

- (void)createUI {
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createScrollView];
    [self createPageController];
}

- (void)createScrollView {
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , self.view.bounds.size.width-, )];
    _scrollView.contentSize = CGSizeMake((self.view.bounds.size.width-) * , );
    _scrollView.backgroundColor = [UIColor cyanColor];

    for (int i = ; i < ; i++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * (self.view.bounds.size.width-), , self.view.bounds.size.width-, )];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i+]];
        [_scrollView addSubview:imageView];
    }

    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
    //分页效果
    _scrollView.pagingEnabled = YES;
    //设置代理
    _scrollView.delegate = self;
    //缩放比例
    _scrollView.minimumZoomScale = ;
    _scrollView.maximumZoomScale = f;
    [self.view addSubview:_scrollView];
}

- (void)createPageController {
    //创建分页控件
    _pageCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(,  - , , )];
    //背景颜色
    _pageCtr.backgroundColor = [UIColor blackColor];
    //设置分页控件的总页数
    _pageCtr.numberOfPages = ;
    //设置初始化分页空间的页码
    _pageCtr.currentPage = ;
    [self.view addSubview:_pageCtr];
}

#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    NSLog(@"%@",[[_scrollView subviews] objectAtIndex:_pageCtr.currentPage]);
    return [[_scrollView subviews] objectAtIndex:_pageCtr.currentPage];
}

//减速停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //获取当前的显示页为第几页
    int currentNum = _scrollView.contentOffset.x/_scrollView.frame.size.width;
    //设置当前的显示页码
    _pageCtr.currentPage = currentNum;
}

@end

demo地址