UIPageView和UIScrollView的结合使用

来源:互联网 发布:大闹天宫魅羽进阶数据 编辑:程序博客网 时间:2024/05/07 21:57
- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor = [UIColor whiteColor];//    用户偏好设置 存储在本地 比如程序是否第一次加载//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];//    if (![user boolForKey:@"aa"]) {        [self setupFirstLanchView];//        [user setBool:YES forKey:@"aa"];//        //立即同步//        [user synchronize];//    }    /**     *  UIPageControl     表示所有的页数,表示当前正处于第几页,点击切换页数     **/    }//创建程序第一次加载要显示的视图-(void)setupFirstLanchView{    [self setupScrollView];    [self setUpPageControl];}-(void)setupScrollView{    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];    scrollView.pagingEnabled = YES;    scrollView.showsVerticalScrollIndicator = NO;    scrollView.showsHorizontalScrollIndicator = NO;    scrollView.bounces = NO;    scrollView.delegate = self;    scrollView.contentSize = CGSizeMake(320 * 6, [UIScreen mainScreen].bounds.size.height) ;    scrollView.tag = 100;    [self.view addSubview:scrollView];    [scrollView release];    for (int  i = 0 ; i < 6; i ++) {        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(320 * i, 0, 320, [UIScreen mainScreen].bounds.size.height)];        imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"v6_guide_%d",i + 1] ofType:@"png"]];        [scrollView addSubview:imageView];        [imageView release];    }}-(void)setUpPageControl{    UIPageControl *pagecontrol = [[UIPageControl alloc]initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height - 40, 300, 20)];    //页数    pagecontrol.tag = 101;    pagecontrol.numberOfPages = 6;//    //未选中圆点颜色    pagecontrol.pageIndicatorTintColor = [UIColor greenColor];//    //设置选中点的颜色    pagecontrol.currentPageIndicatorTintColor = [UIColor redColor];    //设置当前显示页 从零开始    pagecontrol.currentPage = 0;    //    添加响应事件    [pagecontrol addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];    [self.view addSubview:pagecontrol];    [pagecontrol release];}-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    //    当结束拖拽时触发    NSLog(@"%s",__FUNCTION__);    UIPageControl *page = (UIPageControl *) [self.view viewWithTag:101];    page.currentPage = scrollView.contentOffset.x/320;    NSLog(@"%f",scrollView.contentOffset.x);    NSLog(@"%ld",(long)page.currentPage);}-(void)handlePageControl:(UIPageControl *)pageControl{    NSLog(@"%ld",(long)pageControl.currentPage);//    切换pageControl对应切换scrollView    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:100];    [scrollView setContentOffset:CGPointMake(320*pageControl.currentPage, 0) animated:YES];    NSLog(@"%f",scrollView.contentOffset.x);}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

0 0
原创粉丝点击