iOS UIPageView与UIScrollView

在iOS开发中,我们一定会用的类似欢迎界面的东西。其中需要翻页和下面的提示小圆点。在iOS的实现中,翻页效果是使用UIScrollView,而UIPageView只是添加下面的提示小圆点而已。

下面我贴出具体实现比较简单:

//
//  ADView.m
//  Ting3
//
//  Created by 杜甲 on 11/17/14.
//  Copyright (c) 2014 杜甲. All rights reserved.
//

#import "ADView.h"

@interface ADView()<UIScrollViewDelegate>
@property (nonatomic , strong) UIScrollView *adScrollView;
@property (nonatomic , strong) UIPageControl *adPageControl;


@end

@implementation ADView

- (id)initWithFrame:(CGRect)frame andImagePathArr:(NSArray *)pathArr
{
    if (self = [super initWithFrame:frame]) {
 
      
        
        _adScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _adScrollView.pagingEnabled = true;
        _adScrollView.bounces = false;
        _adScrollView.delegate = self;
        _adScrollView.contentSize = CGSizeMake(frame.size.width * pathArr.count, frame.size.height);
       

        [self addSubview:_adScrollView];
        
     
        
        for (int i = 0; i < pathArr.count; i++) {
            UIImageView * adImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:pathArr[i]]];
            adImageView.frame = CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height);
            [_adScrollView addSubview:adImageView];
        }
        

        
        
        _adPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_adScrollView.frame) - 30, frame.size.width, 30)];
        [_adPageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventAllEditingEvents];
        
        _adPageControl.numberOfPages = pathArr.count;
        _adPageControl.currentPage = 0;
        [self addSubview:_adPageControl];
        
        
        
    }
    return self;
}
- (void) pageTurn:(UIPageControl *)sender
{
    CGSize viewSize = _adScrollView.frame.size;
    CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);
    [_adScrollView scrollRectToVisible:rect animated:YES];
    
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.frame;
    _adPageControl.currentPage = offset.x / bounds.size.width;
    
    
    
}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值