UIScrollView

//
//  UIScrollViewController.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/18.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "UIScrollViewController.h"

@interface UIScrollViewController ()

@end

@implementation UIScrollViewController{

    UIPageControl *page;
    UIScrollView *scroll;
    CGFloat screenWidth;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    [self createView:self.view];
}

- (void)createView:(id)uiView{
    UIView *view = (UIView *)uiView;
    
    scroll = [[UIScrollView alloc] init];
    screenWidth = [[UIScreen mainScreen] bounds].size.width;
    CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
    
    
    scroll.frame = [[UIScreen mainScreen] bounds];
    scroll.backgroundColor = [UIColor grayColor];
    
    
    // 加入视图进去
    UIView  *scrollView1 = [[UIView alloc] init];
    scrollView1.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    scrollView1.backgroundColor = [UIColor blackColor];
    [scroll addSubview:scrollView1];
    
    UIView  *scrollView2 = [[UIView alloc] init];
    scrollView2.frame = CGRectMake(screenWidth, 0, screenWidth, screenHeight);
    scrollView2.backgroundColor = [UIColor blueColor];
    [scroll addSubview:scrollView2];
    
    
    UIView  *scrollView3 = [[UIView alloc] init];
    scrollView3.frame = CGRectMake(screenWidth*2, 0, screenWidth, screenHeight);
    scrollView3.backgroundColor = [UIColor purpleColor];
    [scroll addSubview:scrollView3];
    
    UIView  *scrollView4 = [[UIView alloc] init];
    scrollView4.frame = CGRectMake(screenWidth*3, 0, screenWidth, screenHeight);
    scrollView4.backgroundColor = [UIColor redColor];
    [scroll addSubview:scrollView4];
    
    
    // 设置可以滚动  --- 设置这两个就可以滚动了
    scroll.pagingEnabled = YES;
    // 设置滚动范围
    scroll.contentSize = CGSizeMake(screenWidth*4, screenHeight);
    // 设置指定视图, 高度为0,才行。
    scroll.contentOffset = CGPointMake(screenWidth*1, 0);
    
    
    //页码
    page = [[UIPageControl alloc] init];
    page.frame = CGRectMake(0, screenHeight-100, screenWidth, 50);
    page.numberOfPages = 4;
    page.currentPageIndicatorTintColor = [UIColor redColor];
    page.pageIndicatorTintColor = [UIColor blackColor];
    [page addTarget:self action:@selector(valChange:) forControlEvents:UIControlEventTouchUpInside];
    
    scroll.delegate = self;
    
    // 组件放在上下,和顺序有关。 这里也是一个数组管理。栈一样
    [view addSubview:scroll];
    [view addSubview:page];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat width = scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int index = x/width;
    page.currentPage = index;
    NSLog(@"%d",index);
}

- (void)valChange:(UIPageControl *)pageView{
    long count = pageView.currentPage;
    [scroll setContentOffset:CGPointMake(count*screenWidth, 0) animated:true];
}


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值