iOS-UI-滚动视图2—分页查看功能

本文介绍如何在iOS应用中实现分页滚动视图效果,包括使用UIScrollView进行页面切换及UIPageControl的状态同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  ViewController.m

//  UI-滚动视图2—分页查看

//

//  Created by jzq_mac on 15/7/29.

//  Copyright (c) 2015 jzq_mac. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()<UIScrollViewDelegate>


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    NSArray *imageList = @[@"星星.png",@"n63img.jpeg",@"u=827694700,2201960268&fm=21&gp=0.jpg"];

//分页查看图片

    

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];

    scrollView .delegate = self;

    //分页效果

    scrollView.pagingEnabled = YES;

    

    //设置滚动条的样式

    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

    

    //设置滚动视图的偏移量 可以达到滚动视图默认在第几屏 还可以通过contentOffset来判断滚动到第几屏

    scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.view.frame), 0);

    

    //设置是否有反弹的效果(默认值是YES 允许看到地图,并有反弹效果)

    scrollView.bounces = NO;

    

    //隐藏横向滚动条

    scrollView.showsHorizontalScrollIndicator = NO;

    

    

    

    

    for (int i = 0; i < imageList.count; i++) {

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i , 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];

        imageView.image = [UIImage imageNamed:imageList[i]];

        [scrollView addSubview:imageView];

        

    }

    

    scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*imageList.count, 0);

    [self.view addSubview:scrollView];

    

//    UIPageControl(页面控制器)

    

    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-40, CGRectGetWidth(self.view.frame), 20)];

    pageControl.tag = 119;

    

    //设置pageControl总共多少页面

    pageControl.numberOfPages = imageList.count;

    

    //设置指示的当前页面

    pageControl.currentPage = 1;

    

    //当只有一个页面的时候隐藏pageControl

    pageControl.hidesForSinglePage = YES;

    

   //设置轨道的颜色,小圆点

    pageControl.pageIndicatorTintColor = [UIColor yellowColor];

    

   //设置当前的圆点颜色

    pageControl.currentPageIndicatorTintColor = [UIColor redColor];

    

    [self.view addSubview:pageControl];

}





//- (void)scrollViewDidScroll:(UIScrollView *)scrollView

//{

//    NSLog(@"%f",scrollView.contentOffset.x);

//   //在哪一个页面,通过偏移量的x的位置/屏幕的宽度 可以的到当前的页数

// }



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

 //得到一个当前页数;滚动视图的偏移量,屏幕的宽(持有的数据)

    

   // x的偏移量

    CGFloat x = scrollView.contentOffset.x;

    // 屏幕的宽

    CGFloat w = CGRectGetWidth(self.view.frame);

    NSLog(@"%f",x);

   //通过偏移量的x的位置/屏幕的宽度 可以的到当前的页数

    NSInteger currentPage1 = x/w;

    UIPageControl *pageControll = (UIPageControl *)[self.view viewWithTag:119];

//    设置当前页面

    pageControll.currentPage = currentPage1;

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值