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

  • 相关阅读:
    善用VS中的Code Snippet来提高开发效率
    c#获取远程文件更新时间
    图解VS2008单元测试及查看代码覆盖率
    常用关于 JavaScript 中的跨域访问方法
    Jquery中使用setInterval和setTimeout
    外链图片也有风险吗?
    设计模式学习总结抽象工厂模式(Abstract Factory Pattern)
    斐波拉杰博弈 取石子(五)
    后缀表达式 NYOJ 257
    杭电 1085 Holding BinLaden Captive!
  • 原文地址:https://www.cnblogs.com/tuncaysanli/p/2727983.html
Copyright © 2011-2022 走看看