[课堂实践与项目]使用pageControl进行多页面转化最终进入应用

这篇博客介绍了如何在实际课堂项目中运用PageControl来实现多页面间的转换。作者通过在UIScrollView中添加UIImageView,并利用PageControl来响应页面切换,展示了具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.这可是一个新的我以前还不曾做过的知识.欣喜~

看看效果~这个人可是我姐~ 不邪恶~


先看看.h文件,我们是在scrollView中放置了  imageView,imageView切换的时候 将触发下面的pageControl进行改变响应的点.

.h文件

//
//  LCPageControlAndScrollViewController.h
//  使用pageControl进行多页面转化最终进入应用
//
//  Created by lichan on 13-12-9.
//  Copyright (c) 2013年 com.lichan. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LCPageControlAndScrollViewController : UIViewController<UIScrollViewDelegate>


@property (retain,nonatomic)UIScrollView *scrollView;
@property (retain,nonatomic)UIPageControl *pageControl;
@property (retain,nonatomic)NSMutableArray *images;

@end

2..m文件

//
//  LCPageControlAndScrollViewController.m
//  使用pageControl进行多页面转化最终进入应用
//
//  Created by lichan on 13-12-9.
//  Copyright (c) 2013年 com.lichan. All rights reserved.
//

#import "LCPageControlAndScrollViewController.h"

@interface LCPageControlAndScrollViewController()

    -(void)setupPage;  //这个是依赖我们添加的图片设置pageControl的数量等


@end

@implementation LCPageControlAndScrollViewController



- (void)viewDidLoad
{
     //初始化加载~~
    [super viewDidLoad];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 440)];
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 438, 320, 40)];
    
    self.images = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"psb.jpeg"],[UIImage imageNamed:@"psb-2.jpeg"],[UIImage imageNamed:@"psb-3.jpeg"],[UIImage imageNamed:@"psb-4.jpeg"],[UIImage imageNamed:@"psb-5.jpeg"],[UIImage imageNamed:@"psb-6.jpeg"],[UIImage imageNamed:@"psb-7.jpeg"], nil];
    

    
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
    self.view.backgroundColor = [UIColor blueColor];
    
    [self setupPage];
    


}

-(void)setupPage
{
    //设置 scrollview的委托
    self.scrollView.delegate = self; //由于改变视图之后我们就将改变pageControl ,所以我们在显示结束引入这个代理
    self.scrollView.backgroundColor = [UIColor blackColor];
    
    self.scrollView.canCancelContentTouches = NO;//???
    
    self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    
    self.scrollView.clipsToBounds = YES;//自动裁切超出部分
    
    self.scrollView.scrollEnabled = YES;//是否可以缩放
    
    self.scrollView.pagingEnabled = YES;//画面的切换
    
    self.scrollView.directionalLockEnabled = NO;
    
    self.scrollView.alwaysBounceHorizontal = NO;
    self.scrollView.alwaysBounceVertical = NO;
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    //设置页数的index
    NSUInteger pages = 0;
    
    //设置scrollView的坐标
    int originX = 0;
    
    for (UIImage *image in self.images) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        imageView.backgroundColor =  [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
        
        imageView.image = image;
        
        CGRect rect = self.scrollView.frame;
        
        rect.origin.x = originX;
        rect.origin.y = 0;
        
        rect.size.width = self.scrollView.frame.size.width;
        rect.size.height = self.scrollView.frame.size.height;
       
        imageView.frame = rect;
        
        //设置图片内容的显示模式是自适应模式
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        [self.scrollView addSubview:imageView];
        
        originX +=self.scrollView.frame.size.width;
        
        pages++;
        NSLog(@"%d",pages);
        
        
    }
    
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    
    self.pageControl.numberOfPages = pages;
    
    self.pageControl.currentPage = 0;
    self.pageControl.tag = 110;
    
    
    [self.scrollView setContentSize:CGSizeMake(originX, self.scrollView.bounds.size.height)];
    
}

- (void)changePage:(id)sender
{
    NSLog(@"当前的索引值是 %i",self.pageControl.currentPage);
    CGRect rect = self.scrollView.frame;
    
    rect.origin.x = self.pageControl.currentPage *self.scrollView.frame.size.width;
    
    rect.origin.y = 0;
    
    [self.scrollView scrollRectToVisible:rect animated:YES];
    
}

#pragma mark ---UIScrollViewDelegate----
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWith = self.scrollView.frame.size.width;
    
    int page = floor((scrollView.contentOffset.x - pageWith/2)/pageWith)+1;
    
    self.pageControl.currentPage = page;

}


/* 关于  UIPageControl
 You use the UIPageControl class to create and manage page controls. A page control displays a horizontal series of dots, each of which corresponds to a page in the application’s document (or other data-model entity). The currently viewed page is indicated by a white dot.
 
 Managing the Page Navigation
 
 
 currentPage  property
 numberOfPages  property
 hidesForSinglePage  property
 Updating the Page Display
 pageIndicatorTintColor  property
 currentPageIndicatorTintColor  property
 defersCurrentPageDisplay  property
 
 – updateCurrentPageDisplay
 */


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值