zoukankan      html  css  js  c++  java
  • iOS 6编程UIScrollView滚动视图和UIPageControl分页控件的简单应用

    写了一个简单的iOS App,演示UIScrollView滚动视图和UIPageControl分页控件的简单应用。

    本App功能是:在每一页显示不同背景演示的页面。UIScrollView滚动视图和UIPageControl分页控件进行关联,滚动到新的页面时,分页控件也会同步切换到新的页面,反之也如此。

    示例App 最终运行界面如下:


    开发工具:Xcode 4.5 + iOS 6 模拟器

    创建项目ColorScroll,类前缀也设置为ColorScroll,如下图所示。

    下面是本示例项目的完整源代码,代码中有比较完整的注释。

    ColorScrollViewController.h 头文件代码:

    #import <UIKit/UIKit.h>

    @interface ColorScrollViewController : UIViewController<UIScrollViewDelegate>
    // 滚动视图输出口
    @property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
    // 分页控件输出口
    @property (strong, nonatomic) IBOutlet UIPageControl *pageControl;

    // 连接分页控件的value changed事件
    - (IBAction)changePage:(id)sender;

    @end

    ColorScrollViewController.m 实现文件代码:

    #import "ColorScrollViewController.h"

    @interface ColorScrollViewController ()

    @end

    @implementation ColorScrollViewController

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray *colors = [NSArray arrayWithObjects:
    [UIColor redColor],
    [UIColor greenColor],
    [UIColor blueColor],
    [UIColor yellowColor], nil];

    self.scrollView.frame = CGRectMake(0, 0, 320, 410);

    for (int i=0; i<colors.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width *i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIView *subView = [[UIView alloc] initWithFrame:frame];
    subView.backgroundColor = [colors objectAtIndex:i];
    [self.scrollView addSubview:subView];
    }
    // 设置scroll view的contentSize属性,这个是包含所有页面的scroll view的尺寸
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*colors.count, self.scrollView.frame.size.height);

    // 告诉分页控件-总共有多少页
    self.pageControl.numberOfPages = colors.count;
    }

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat pageWidth = self.scrollView.frame.size.width;
    // 在滚动超过页面宽度的50%的时候,切换到新的页面
    int page = floor((self.scrollView.contentOffset.x + pageWidth/2)/pageWidth) ;
    self.pageControl.currentPage = page;
    }

    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

    - (IBAction)changePage:(id)sender {
    // 更新Scroll View到正确的页面
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];
    }
    @end

  • 相关阅读:
    c# 泛型集合Dictionary
    int+? int后带问题是什么意思,请看内容。
    在ASP.NET中实现Url ReWriting 示例
    #DataDirectory是什么意思呢?
    vs2008生成自定义dll,VS2008发布、生成网站时设置固定的dll文件名?
    平台安装注意事项
    快速开发平台程序运行环境
    快速开发平台介绍(动态)
    快速开发平台程序安装包20120612
    JavaFx 2.0总结
  • 原文地址:https://www.cnblogs.com/tuncaysanli/p/2727983.html
Copyright © 2011-2022 走看看