UIScrollView + UIPageControl的使用

UIScrollView + UIPageControl的使用,代码片段,可以直接贴哦

可是这些代码我还没有吸收,先用再说,哈哈哈

//.h

@interface RootViewController : UIViewController<UIScrollViewDelegate> //记得要引入代理哦

@property (nonatomic, strong) UIScrollView *helpScrView;

@property (nonatomic, strong) UIPageControl *pageCtrl;


//.m

- (void)viewDidLoad

{

   CGRect bounds = self.view.frame//获取界面区域

    

    //加载蒙板图片,限于篇幅,这里仅显示一张图片的加载方法

    UIImageView* imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height)];

    [imageView1 setImage:[UIImage imageNamed:@"1.png"]];

    imageView1.alpha = 0.5f;

    

    //继续加载图片

    UIImageView* imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(bounds.origin.x+320, bounds.origin.y, bounds.size.width, bounds.size.height)];  //创建UIImageView,位置大小与主界面一样。

    [imageView2 setImage:[UIImage imageNamed:@"1.png"]];  //加载图片help01.pngimageView1中。

    imageView2.alpha = 0.5f//将透明度设为50%

    

    UIImageView* imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(bounds.origin.x+640, bounds.origin.y, bounds.size.width, bounds.size.height)];  //创建UIImageView,位置大小与主界面一样。

    [imageView3 setImage:[UIImage imageNamed:@"1.png"]];  //加载图片help01.pngimageView1中。

    imageView3.alpha = 0.5f//将透明度设为50%

    

    _helpScrView = [[UIScrollView alloc] initWithFrame:CGRectMake(bounds.origin.x, bounds.origin.y+60, bounds.size.width, bounds.size.height)];  //创建UIScrollView,位置大小与主界面一样。

    [_helpScrView setContentSize:CGSizeMake(bounds.size.width * 3, bounds.size.height)];  //设置全部内容的尺寸,这里帮助图片是3张,所以宽度设为界面宽度*3,高度和界面一致。

    _helpScrView.pagingEnabled = YES//设为YES时,会按页滑动

    _helpScrView.bounces = NO//取消UIScrollView的弹性属性,这个可以按个人喜好来定

    [_helpScrView setDelegate:self];  //UIScrollViewdelegate函数在本类中定义

    _helpScrView.showsHorizontalScrollIndicator = NO//因为我们使用UIPageControl表示页面进度,所以取消UIScrollView自己的进度条。

    [_helpScrView addSubview:imageView1];

    [_helpScrView addSubview:imageView2];

    [_helpScrView addSubview:imageView3];

    [self.view addSubview:_helpScrView];

    

    //创建UIPageControl

    _pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, bounds.size.height - 80, bounds.size.width, 30)];

    _pageCtrl.numberOfPages = [_everyDisplayView count];//总的图片页数

    _pageCtrl.currentPage = 0;   //当前页

    [_pageCtrl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];  //用户点击UIPageControl的响应函数

    [self.view addSubview:_pageCtrl];  //UIPageControl添加到主界面上。

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    //更新UIPageControl的当前页

    CGPoint offset = scrollView.contentOffset;

    CGRect bounds = scrollView.frame;

    [_pageCtrl setCurrentPage:offset.x / bounds.size.width];

}

- (void)pageTurn:(UIPageControl*)sender

{

    //UIScrollView做出相应的滑动显示

    CGSize viewSize = _helpScrView.frame.size;

    CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);

    [_helpScrView scrollRectToVisible:rect animated:YES];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值