实现代码:

//
// ViewController.m
// UIpageControl
//
// Created by dllo on 16/3/10.
// Copyright © 2016年 dllo. All rights reserved.
// #import "ViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height @interface ViewController ()<UIScrollViewDelegate> @property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIPageControl *pageControl;
@property (nonatomic, retain)UILabel *titleLabel; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor yellowColor];
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(, , , )];
self.pageControl.backgroundColor = [UIColor redColor];
[self.view addSubview:self.pageControl];
[self.pageControl release];
self.pageControl.numberOfPages = ;
self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor cyanColor];
[self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged]; self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(, ,WIDTH, )];
[self.view addSubview:self.scrollView];
[self.scrollView release];
self.scrollView.contentSize = CGSizeMake(WIDTH * , ); for (NSInteger i = ; i < ; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * i + WIDTH , , WIDTH, HEIGHT)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg", i]];
[self.scrollView addSubview:imageView];
}
UIImageView *firstImageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , WIDTH, HEIGHT)];
firstImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h6.jpeg"]];
[self.scrollView addSubview:firstImageView];
[firstImageView release]; UIImageView *lastImageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * , , WIDTH, HEIGHT)];
lastImageView.image = [UIImage imageNamed:@"h0.jpeg"];
[self.scrollView addSubview:lastImageView];
[lastImageView release]; //创建label
self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
[self.view addSubview:self.titleLabel];
[_titleLabel release];
self.titleLabel.backgroundColor = [UIColor grayColor];
[self.titleLabel setTextColor:[UIColor whiteColor]];
self.titleLabel.textAlignment = ;
self.titleLabel.text = @"1 / 7";
self.scrollView.contentOffset = CGPointMake(WIDTH, );
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
}
- (void)pageAction:(UIPageControl *)page {
NSLog(@"%ld", self.pageControl.currentPage);
// self.scrollView.contentOffset = CGPointMake(WIDTH * page.currentPage + WIDTH, 0);
[self.scrollView setContentOffset:CGPointMake(WIDTH *page.currentPage + WIDTH, ) animated:YES];
[self.titleLabel setText:[NSString stringWithFormat:@"%ld / 7", (NSInteger)(page.currentPage + )]];
}
//滑动结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x == ) {
scrollView.contentOffset = CGPointMake(WIDTH * , );
}else if(scrollView.contentOffset.x == * WIDTH){
scrollView.contentOffset = CGPointMake(WIDTH , );
} self.pageControl.currentPage = scrollView.contentOffset.x / WIDTH - ;
[self.titleLabel setText:[NSString stringWithFormat:@"%ld / 7", (NSInteger)(self.pageControl.currentPage + )]];
}
- (void)dealloc{
[_scrollView release];
[_pageControl release];
[super dealloc];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

UIScrollView和UIPageController的更多相关文章

  1. 图像和滚动 、 编程规范和Xcode(一)

    1 在界面上以各种模式显示图片 1.1 问题 在ios开发中经常需要展示图片以满足需求和美化界面,本案例将学习如何以代码的方式使用UIImageView视图控件来展示图片,如图-1所示: 图-1 1. ...

  2. 你真的了解UIScrollView吗?

    一:首先查看一下关于UIScrollView的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding& ...

  3. 【原】Masonry+UIScrollView的使用注意事项

    [原]Masonry+UIScrollView的使用注意事项 本文转载请注明出处 —— polobymulberry-博客园 1.问题描述 我想实现的使用在一个UIScrollView依次添加三个UI ...

  4. UIScrollView的封装

    UIScrollView的封装 效果 特点 1.用法简单,尺寸大小,随意设置位置 2.可以有多个数据源的数据,可以定制不通的界面(如同上图,一个有文字,一个没有文字) 3.能够实现点击事件 用法 1. ...

  5. UI第十七节——UIScrollView

    // 实例化一个ScrollView    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen main ...

  6. UI控件(UIScrollView)

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建一个scrollview UIScrollV ...

  7. UIScrollView的delaysContentTouches与canCencelContentTouches属性

    UIScrollView有一个BOOL类型的tracking属性,用来返回用户是否已经触及内容并打算开始滚动,我们从这个属性开始探究UIScrollView的工作原理: 当手指触摸到UIScrollV ...

  8. iOS 视图:重绘与UIScrollView(内容根据iOS编程编写)

    我们继续之前的 Hypnosister 应用,当用户开始触摸的时候,圆形的颜色会改变. 首先,在 JXHypnosisView 头文件中声明一个属性,用来表示圆形的颜色. #import " ...

  9. 学习笔记之-------UIScrollView 基本用法 代理使用

    //contentSize.contentInset和contentOffset 是 scrollView三个基本的属性. // 滚动 self.ScrollView.contentSize =sel ...

随机推荐

  1. c#发送http请求

    直接代码,自己备用 /** * @method:生成验证码 */ [JSONMethod] [Description ( "生成验证码" )] [DomTemplate ( )] ...

  2. c++ initialize_list

    看到这么一个东西,可以实现花括号( "{" "}" )初始化容器类. 使用时需包含头文件 #include <initialize_list> 我们 ...

  3. Combine small files to Sequence file

    Combine small files to sequence file or avro files are a good method to feed hadoop. Small files in ...

  4. [转]Worksheet.Change Event (Excel)

    本文转自:https://msdn.microsoft.com/en-us/library/office/ff839775.aspx#AboutContributor Example   The fo ...

  5. ZooKeeper系列4:ZooKeeper API简介及编程

    问题导读: 1.ZooKeeper API 共包含几个包? 2.如何使用ZooKeeper API 创建zookeeper应用程序? 1)ZooKeeper API 简介   ZooKeeper AP ...

  6. 分析循环 Analysis of Loops-------geeksforgeeks 翻译

    之前我们讨论了渐进分析,最佳最坏平均情况的分析以及渐进符号.在这一篇中我们分析一下迭代的简单程序. 1. O(1): 如果程序中没有包含任何的循环,递归或者任何的非常数时间的函数,我们就说这个程序的时 ...

  7. 第2章 面向对象的设计原则(SOLID):5_迪米特法则

    5. 迪米特法则(Law of Demeter,LoD) 5.1 定义 (1)应尽量减少其他对象之间的交互,对象只和自己的朋友交谈,即对其他依赖的类越少越好(不要和太多的类发生关系). (2)尽量不要 ...

  8. 获取本机的IP地址(局域网)与主机名称

    编写内容保存为bat @echo off &setlocal enabledelayedexpansion Rem '/*========获取本机的IP地址(局域网)=========*/ e ...

  9. 关于OAuth2.0的文章收集

    http://blog.csdn.net/seccloud/article/details/8192707

  10. 各浏览器对typeof运算符的实现差异

    1,IE6/7/8中typeof运算符对BOM对象如window,document,location,history等对象的方法返回“object”,标准浏览器都返回“function”. 1 2 3 ...