码迷,mamicode.com
首页 > 其他好文 > 详细

UI_UIScrollView/UIpageControl

时间:2015-07-07 22:46:30      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:scrollview   滑动   pagecontrl   ios   ui   

创建UIScrollView

@property(nonatomic, retain) UIScrollView *scrollView;
@property(nonatomic, retain) UIImageView *opeImageView;
@property (nonatomic, retain)UIPageControl *pageControl;
#pragma mark - 创建滑动视图  UIScrollView
- (void)createScrollView
{
    self.scrollView = [[[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.backgroundColor = [UIColor grayColor];

    // contentSize 决定了scrollView 滑动范围的大小
    _scrollView.contentSize = CGSizeMake(6 * KScreenWidth, KScreenWidth);

    // 是否显示滚动条 默认显示
    self.scrollView.showsHorizontalScrollIndicator = YES; // 水平
    self.scrollView.showsVerticalScrollIndicator = YES;  // 竖直

    // 整页反动 默认 NO
    _scrollView.pagingEnabled = YES;

    // 回弹效果 默认 YES
    _scrollView.bounces = YES;

    // 设置 scrollView 的偏移量 默认是 (0,0)
    _scrollView.contentOffset = CGPointMake(KScreenWidth, 0);

    // 点击滚动条回到顶部
    _scrollView.scrollsToTop = YES;

    // 是否允许拖动 默认 YES
    _scrollView.scrollEnabled = YES;

    [self addSubview:_scrollView];
    [self.scrollView release];
}

创建UIPageControl

#pragma mark - 创建 UIPageControl
- (void)createPageControl
{
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(120, KScreenHeight - 100, 160, 30)];

    // 设置小点的个数
    self.pageControl.numberOfPages = 6;

    // 设置小点颜色
    self.pageControl.pageIndicatorTintColor = [UIColor redColor];

    // 改变选中小点的颜色
    self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];

    [self addSubview:self.pageControl];
    [self.pageControl release];
}

创建引导多张图片

#pragma mark - 创建引导多张图片
- (void)createLeadPageImage
{
    for (int i = 0; i < 5; i++) {

        NSString *name = [NSString stringWithFormat:@"bg_%d.jpg",i];

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
        imageView.frame = CGRectMake(i * KScreenWidth, 0, KScreenWidth, KScreenHeight);
        [self.scrollView addSubview:imageView];
        [imageView release];
    }
}

创建 ImageView

#pragma mark - 创建 ImageView
- (void)createOpeImageView
{
    self.opeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_4"]];
    self.opeImageView.frame = CGRectMake(0, 0, 375, 667);
    [self.scrollView addSubview:_opeImageView];
    [_opeImageView release];
}

实现代理

@interface RootViewController () <UIScrollViewDelegate>
- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"%@", self.view);


    // 设置scrollView 的代理
    self.rootView.scrollView.delegate = self;

    // 设置最大的缩放比例
    self.rootView.scrollView.maximumZoomScale = 3;

    // 设置最小的缩放比例
    self.rootView.scrollView.minimumZoomScale = 0.3;
}

代理方法

#pragma mark - scroolView 滑动的时候会触发方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"滑动");

    // 图片带动小点移动
    NSInteger index =  self.rootView.scrollView.contentOffset.x / KScreenWidth;
    self.rootView.pageControl.currentPage = index;
    // pageControl 绑定监听事件
    [self.rootView.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
}
// 实现 pageControl 绑定监听事件

- (void)pageControlAction:(UIPageControl *)sender
{

    [self.rootView.scrollView setContentOffset:CGPointMake(KScreenWidth * sender.currentPage, 0) animated:YES];
}
#pragma mark - scroolView 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"开始拖拽了");
}

#pragma mark - scroolView 结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"结束拖拽");
}


#pragma mark - scroolView 开始减速触发方法
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"开始减速触发方法");
}


#pragma mark - scroolView 结束减速触发方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"结束减速????");
}

缩放

#pragma mark - scrollView 缩放执行过程中
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    if (self.rootView.scrollView.zoomScale < 1.0) {
        self.rootView.opeImageView.center = _rootView.center;
    }
}


#pragma mark - scroolView 指定缩放的内容
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.rootView.opeImageView;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UI_UIScrollView/UIpageControl

标签:scrollview   滑动   pagecontrl   ios   ui   

原文地址:http://blog.csdn.net/yadong_zhao/article/details/46794973

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