UIScrollView及UIPageControl的使用

本文介绍了一个iOS应用中的滑动视图(UIScrollView)实现案例,包括UIScrollView的基本配置、图片加载、UIPageControl的使用以及实现翻页动画效果。

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

项目实现:
滑动视图第一页
滑动视图第二页
滑动结束后,翻转动画跳至ViewController页面
项目所含文件
// AppDelegate.h
// AppDelegate.m
// ViewController.h
// ViewController.m
// ViewController.xlb
// animation.h
// animation.m

UIScrollView 解释:滑动视图

// animation.m中
1.初始化

@interface animation ()
{
    UIScrollView *_scroll;//滑动视图,为私有变量
}

- (void)viewDidLoad
{
 //创建UIScrollView,尺寸大小同屏幕一致
    _mainScrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}

2.设置代理

@interface animation ()<UIScrollViewDelegate>
 [_scroll setDelegate:self];//viewDidLoad函数里

3.设置UIScrollView

  //viewDidLoad函数里
  [_scroll setPagingEnabled:YES];//是否分页
  [_scroll setScrollEnabled:YES];//是否滚动
  [_scroll setShowsVerticalScrollIndicator:NO];//垂直滚动条
  [_scroll setShowsHorizontalScrollIndicator:YES];//水平滚动条
    //配置内容的宽高,宽度为屏幕宽度,高度为屏幕高度2倍,即2页
  [_scroll setContentSize:CGSizeMake(0, _scroll.frame.size.height * 2)];
  [self.view addSubview:_scroll];//将_scroll加入主View

3.加入图片

    for (int a = 0; a < 2; a ++)
    {
    //加入ImageView(图片容器),并设置图片尺寸,2页高度
    UIImageView *image = [[UIImageView alloc] initWithFrame: CGRectMake (_scroll.frame.origin.x, _scroll.frame.size.height * a, _scroll.frame.size.width, _scroll.frame.size.height)];
    //设置图片显示,将名字为“star_1.jpg”“star_2.jpg”的图片导入项目。stringWithFormat是按照格式转换成字符串
    [image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"star_%d.jpg",a+1]]];
     //将imageView加到ScrollView上
    [_scroll addSubview:image];
    }

3.实现代理

// scrollView 滑动时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
 ...
}
// scrollView 结束滑动时,翻转动画效果跳至ViewController页面
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if(_pageControl.currentPage == 1)//在实心小点点为1时(即页面在第二个页面时跳转动画,否则,会在页面1与页面2中间时候跳转)
{
//设置动画
 CATransition *anima = [CATransition animation];
        [anima setDuration:1.7f];//动画翻转的时间
        [anima setType:@"oglFlip"];//过滤效果:pageCurl(翻页效果)   oglFlip(翻转效果)
        [anima setSubtype:kCATransitionFromRight];//过滤方向
        //ViewController页面入栈
    [self.navigationController pushViewController:_jumpToController animated:YES];
       //添加动画
        [self.navigationController.view.layer addAnimation:anima forKey:@"jump"];
}
}

_jumpToController 跳转页面

1.添加UINavigationController及UIScrollView

//  AppDelegate.h
@property (strong, nonatomic) UINavigationController *nav;
//  AppDelegate.m
//windows初始化,尺寸为屏幕大小
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//ViewController页面初始化
    ViewController *view = [[ViewController alloc] init];
    //view是动画完成后要跳转的页面,传给animation的initWithController函数
    animation *VC = [[animation alloc] initWithController:view];
    //nav加入UIScrollView。UIScrollView nav view三者合在一起
    self.nav = [[UINavigationController alloc] initWithRootViewController:VC];
    //加入window根视图
    [self.window setRootViewController:self.nav];
    //在window上显示
    [self.window makeKeyAndVisible];

2.设置动画显示完后的跳转页面

//  animation.h
- (id)initWithController:(UIViewController *)controller;
//  animation.m
@interface animation ()<UIScrollViewDelegate>
{
       id _jumpToController;
}
//存地址,传进来的controller即使动画完成后要跳转至的页面
- (id)initWithController:(UIViewController *)controller
{
    self = [super init];
    if( self )
    {
        _jumpToController = controller;
    }

    return self;
}

UIPageControl滑动视图上的小点点

1.初始化

@interface animation ()
{
   UIPageControl *_pageControl;//点点
}

- (void)viewDidLoad
{
 _pageControl = [[UIPageControl alloc] init];
}

2.设置

//(void)viewDidLoad
//在矩形屏幕中的(x,y)坐标,再以坐标为开始,以40为宽,20为高再设一个矩形,点点位于矩形中间

    _pageControl.frame = CGRectMake(_scroll.frame.size.width / 2, _scroll.frame.size.height - 60, 40, 20);
    [_pageControl setNumberOfPages:2];//添加2个点点
    [_pageControl setCurrentPage:0];//设置当前页面,控制实心小点点为第一页  第一个点下标为0
   //给_pageControl添加事件,当用户点击 UIPageControl的相应函数
    [_pageControl addTarget:self action:@selector(changePage) forControlEvents:UIControlEventValueChanged];
//将_pageControl加入主View
    [self.view addSubview:_pageControl];

3.用户相应

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
 //滑动时更新实现小点点 (偏移量除以一页的宽度)
    _pageControl.currentPage = _scroll.contentOffset.y / _scroll.frame.size.height;
}
//当用户点击了小点点
- (void)changePage
{
    //另UIScrollView做出相应地滑动,偏移量=每一页的宽度乘以页数r....需更改
    [_scroll setContentOffset:CGPointMake(_scroll.frame.size.width * _pageControl.currentPage, 0)];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值