UIScrollView和UIPageController

时间:2023-03-08 19:26:42

UIScrollView和UIPageController

实现代码:

//
// ViewController.m
// UIpageControl
//
// Created by dllo on 16/3/10.
// Copyright © 2016年 dllo. All rights reserved.
// #import "ViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height @interface ViewController ()<UIScrollViewDelegate> @property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIPageControl *pageControl;
@property (nonatomic, retain)UILabel *titleLabel; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor yellowColor];
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(, , , )];
self.pageControl.backgroundColor = [UIColor redColor];
[self.view addSubview:self.pageControl];
[self.pageControl release];
self.pageControl.numberOfPages = ;
self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor cyanColor];
[self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged]; self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(, ,WIDTH, )];
[self.view addSubview:self.scrollView];
[self.scrollView release];
self.scrollView.contentSize = CGSizeMake(WIDTH * , ); for (NSInteger i = ; i < ; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * i + WIDTH , , WIDTH, HEIGHT)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg", i]];
[self.scrollView addSubview:imageView];
}
UIImageView *firstImageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , WIDTH, HEIGHT)];
firstImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h6.jpeg"]];
[self.scrollView addSubview:firstImageView];
[firstImageView release]; UIImageView *lastImageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * , , WIDTH, HEIGHT)];
lastImageView.image = [UIImage imageNamed:@"h0.jpeg"];
[self.scrollView addSubview:lastImageView];
[lastImageView release]; //创建label
self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
[self.view addSubview:self.titleLabel];
[_titleLabel release];
self.titleLabel.backgroundColor = [UIColor grayColor];
[self.titleLabel setTextColor:[UIColor whiteColor]];
self.titleLabel.textAlignment = ;
self.titleLabel.text = @"1 / 7";
self.scrollView.contentOffset = CGPointMake(WIDTH, );
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
}
- (void)pageAction:(UIPageControl *)page {
NSLog(@"%ld", self.pageControl.currentPage);
// self.scrollView.contentOffset = CGPointMake(WIDTH * page.currentPage + WIDTH, 0);
[self.scrollView setContentOffset:CGPointMake(WIDTH *page.currentPage + WIDTH, ) animated:YES];
[self.titleLabel setText:[NSString stringWithFormat:@"%ld / 7", (NSInteger)(page.currentPage + )]];
}
//滑动结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x == ) {
scrollView.contentOffset = CGPointMake(WIDTH * , );
}else if(scrollView.contentOffset.x == * WIDTH){
scrollView.contentOffset = CGPointMake(WIDTH , );
} self.pageControl.currentPage = scrollView.contentOffset.x / WIDTH - ;
[self.titleLabel setText:[NSString stringWithFormat:@"%ld / 7", (NSInteger)(self.pageControl.currentPage + )]];
}
- (void)dealloc{
[_scrollView release];
[_pageControl release];
[super dealloc];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end