UIScrollView和UIPageController

来源:互联网 发布:大闸蟹 二恶英 知乎 编辑:程序博客网 时间:2024/05/21 08:59

这里写图片描述

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    [self.window makeKeyAndVisible];    //创建导航栏控制器    UINavigationController* nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];    //设置应用程序的根控制器    self.window.rootViewController = nav;    return YES;}
#import "ViewController.h"#define Width [UIScreen mainScreen].bounds.size.width#define Heigth [UIScreen mainScreen].bounds.size.heigth@interface ViewController ()<UIScrollViewDelegate>@property (retain)UIScrollView* scrollView;@property (retain)UIPageControl* pageControl;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.view.backgroundColor = [UIColor whiteColor];    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake((Width-320)/2, 65, 320, 480)];    self.scrollView.backgroundColor = [UIColor cyanColor];    [self.view addSubview:self.scrollView];    //要设置代理⚠️    self.scrollView.delegate=self;    for (NSInteger i = 0; i < 4; i++) {        UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 480)];        [self.scrollView addSubview:imageView];        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"美女0%ld.jpg",i+1]];        //防止内容视图下滑        self.automaticallyAdjustsScrollViewInsets = NO;        //设置滚动范围        self.scrollView.contentSize = CGSizeMake(320*4, 480);        //取消反弹效果        self.scrollView.bounces = NO;        //隐藏水平方向的滑块        self.scrollView.showsHorizontalScrollIndicator = NO;        //设置分页显示        self.scrollView.pagingEnabled = YES;        //创建UIPaneControl对象        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((Width-100)/2, 450+64, 100, 30)];        self.pageControl.backgroundColor = [UIColor clearColor];        //设置总页数        self.pageControl.numberOfPages = 4;        //设置当前页        self.pageControl.currentPage = 0;        //让self.pageControl绑定事件        [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];        [self.view addSubview:self.pageControl];    }    //左边按钮    UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];    leftBtn.frame = CGRectMake(0, 0, 60, 30);    leftBtn.backgroundColor = [UIColor grayColor];    [leftBtn setTitle:@"上一页" forState:UIControlStateNormal];    [leftBtn addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];    //右边按钮    UIButton* rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];    rightBtn.frame = CGRectMake(0, 0, 60, 30);    rightBtn.backgroundColor = [UIColor grayColor];    [rightBtn setTitle:@"下一页" forState:UIControlStateNormal];    [rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];}-(void)leftAction:(UIBarButtonItem*)sender{    NSLog(@"上一页被点!!!!");    NSInteger page = self.pageControl.currentPage;    if (page > 0) {        page--;        [UIView animateWithDuration:0.5 animations:^{            self.pageControl.currentPage = page;            self.scrollView.contentOffset = CGPointMake(320*page, 0);        }];    }}-(void)rightAction:(UIBarButtonItem*)sender{    NSLog(@"下一页被点!!!!");    NSInteger page = self.pageControl.currentPage;    if (page < 3) {        page++;        [UIView animateWithDuration:0.5 animations:^{            self.pageControl.currentPage = page;            self.scrollView.contentOffset = CGPointMake(320*page, 0);        }];    }}-(void)pageAction:(UIPageControl*)page{    NSLog(@"%ld",(long)page.currentPage);    [UIView animateWithDuration:0.5 animations:^{        self.scrollView.contentOffset = CGPointMake(page.currentPage*320, 0);    }];}#pragma mark-UIScrollViewDelegate-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    NSLog(@"hehe");    //获取偏移量    CGPoint point = scrollView.contentOffset;    NSLog(@"%@",NSStringFromCGPoint(point));    //计算出当前展示的是第几张照片    NSInteger page = point.x/300;    self.pageControl.currentPage = page;}