文章标题

## UIScrollView ##

  • .h文件
@interface xxxViewContriller : XXXViewController<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    UIView *_contentView;
    UIPageControll *_pageControll;
}
  • .m文件
(void)viewDidLoad
{
    [super viewDidLoad];
    ...设置title及背景图片

    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;

    CGFloat scrollViewWidth = width - 20;
    CGFloat scrollViewHeight = height - 64 - 49 - 37;
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 64, scrollViewWidth, scrollViewHeight)];
    [self.view addSubview:_scrollView];

    _scrollView.contentSize = CGSizeMake(scrollViewWidth * 5, scrollViewHeight);
    _scrollView.pagingEnable = YES;
    _scrollView.maximumZoomScale = 3;
    _scrollView.minimunZoomScale = 0.5;
    _scrollView.delegate = self;

    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth * 5, scrollViewHeight)];
    _contentView.backgroundColor = [UIColor cyanColor];
    [_scrollView addSubview:_contentView];

    for (int i = 0; i < 5; ++i) {
        UIView *view - [[UIView alloc] initWithFrame:CGRectMake(i * scrollViewWidth, 0, scrollViewWidth, scrollViewHeight)];
        [_contentView addSubview:view];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)];
        NSString *imageName = [NSString stringWithFormat:@"bg%i", i + 1];
        imageView.image = [UIImage imageNamed:imageName];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubView:imageView];

        UILable *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:100];
        label.text = [NSString stringWithFormat:@"%i", i + 1];
        [view addSubview:label];
    }

    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, _scrollView.Frame.origin.y + scrollViewHeight, scrollViewWidth, 37)];
    _pageControl.numberOfPages = 5;
    [_pageControl addTarget:self action:@selector(pageControlClicked:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pageControl];
}

- (void)pageControlClicked:(UIPageControl *)pageControl
{
    CGFloat width = self.view.Frame.size.width;
    CGFloat scrollViewWidth = width - 20;
    [_scrollView setContentOffset:CGPointMake(pageControl.currentPage * scrollViewWidth, 0) animated:YES]; 
}

UIScrollView delegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _contentView;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat width = self.view.frame.size.width;
    CGFloat scrollViewWidth = width - 20;
    _pageControl.currentPage = _scrollView.contentOffset.x / scrollViewWidth;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值