iOS--UIScrollView和UIPageControl配合使用完成引用介绍页面

本文介绍了如何在iOS应用中使用UIScrollView和UIPageControl结合,实现一个引导页面。主要步骤包括设置UIScrollView的内容大小,添加图片,配置UIPageControl,以及实现滑动监听和页面切换效果。
摘要由CSDN通过智能技术生成
// 效果图
IOS--UIScrollView和UIPageControl配合使用完成引用介绍页面

IOS--UIScrollView和UIPageControl配合使用完成引用介绍页面


// 直接代码
// 头文件

//

//  RootViewController.h

//  UIScrollView

//

//  Created by LiZe on 13-9-6.

//  Copyright (c) 2013 BlackCode. All rights reserved.

//

// 需要遵守UIScrollViewDelegate协议


#import


@interface RootViewController : UIViewController <</span>UIScrollViewDelegate>


@property (nonatomicretainUIScrollView *userGuideScrollView;

@property (nonatomicretainUIPageControl *scrollPageControl;


@end



// 目标文件

//

//  RootViewController.m

//  UIScrollView

//

//  Created by LiZe on 13-9-6.

//  Copyright (c) 2013 BlackCode. All rights reserved.

//

 

// 实现思路

//  1. 在主视图中添加一个高度和宽度都和主视图相同的UIScrollView,然后设置它内容的大小为有多少屏幕图片的大小(高度和主视图相同,宽度就是有几张图片,就用主视图宽度乘以几)

//  2. 把准备好的图片添加到UIscrollView中,然后把UIscrollView添加到当前的View

//  3. 然后添加UIPageControl在视图上面

//  4. 需要实现一个UIPageControl的监听方法,点击时会改变视图

//  5. 实现UIScrollViewDelegate协议中的方法,实现滑动UIScrollView的时候,让UIPageControl也进行相应的移动

//  6. 最后重写dealloc方法,释放属性内存

//


#import "RootViewController.h"


@interface RootViewController ()


@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    

    // 设置背景色

    self.view.backgroundColor = [UIColor grayColor];

    

    // 添加UIScrollView

    self.userGuideScrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)] autorelease]; // 初始化位置和大小

    self.userGuideScrollView.contentSize = CGSizeMake(self.view.frame.size.width* 11, self.view.frame.size.height); // 设置存放的内容的大小

    _userGuideScrollView.pagingEnabled = YES; // 设置scrollView按一整页滑动

    _userGuideScrollView.delegate = self; // 设置代理为当前

    _userGuideScrollView.userInteractionEnabled = YES; // 设置可以与用户交互

    _userGuideScrollView.showsHorizontalScrollIndicator = YES; // 显示滑动时候横向的滚动条

    // 添加图片到UIScrollView

    for (int i = 1; i <= 11; i++) {

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:[NSString stringWithFormat:@"image%d", i]]];

        imageView.frame = CGRectMake((i - 1) * self.view.frame.size.width, 0,self.view.frame.size.width, self.view.frame.size.height);

        [_userGuideScrollView addSubview:imageView];

        [imageView release], imageView = nil;

    }

    // 添加到当前的View

    [self.view addSubview:_userGuideScrollView];


    

    // 初始化UIPageController

    self.scrollPageControl = [[[UIPageControl alloc] init] autorelease];

    _scrollPageControl.frame = CGRectMake(0, self.view.frame.size.height - 60,self.view.frame.size.width, 30); // 设置位置

    _scrollPageControl.numberOfPages = 11; // 设置有多少个小点点

    _scrollPageControl.currentPage = 0; // 设置当前是哪个点点

    _scrollPageControl.pageIndicatorTintColor = [UIColor blueColor]; // 没有选中的点点的颜色

    _scrollPageControl.currentPageIndicatorTintColor = [UIColor yellowColor]; //当前点点的颜色

    [_scrollPageControl addTarget:self action:@selector(scrollPageControlAction:)forControlEvents:UIControlEventValueChanged]; // 设置监听事件

    _scrollPageControl.highlighted = YES;

    [self.view addSubview:_scrollPageControl];

 

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



#pragma mark - 实现---scrollPageControlAction:监听事件

- (void)scrollPageControlAction:(UIPageControl *) sender {

    //UIScrollView做出相应的滑动显示

    CGSize viewSize = _userGuideScrollView.frame.size;

    CGRect rect = CGRectMake(sender.currentPage * viewSize.width0, viewSize.width, viewSize.height);

    [_userGuideScrollView scrollRectToVisible:rect animated:YES];

}



#pragma mark - 重写----scrollViewDelegate中的代理方法 实现滑动

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    // 计算偏移量

    int index = abs(scrollView.contentOffset.x / scrollView.frame.size.width);

    // 根据偏移量来设置pageControl

    _scrollPageControl.currentPage = index;

}



#pragma mark - 重写----dealloc方法

- (void)dealloc {

    // 释放属性的内存

    [_scrollPageControl release], _scrollPageControl = nil;

    [_userGuideScrollView release], _userGuideScrollView = nil;

    // 调用父类的dealloc方法

    [super dealloc];

}



@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值