实现代码:

//
// 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. 标准IO的缓冲问题

    在看APU时,第8章进程时, #include <stdio.h> #include <unistd.h> ; char buf[] = "a write to st ...

  2. 我的NopCommerce之旅(6): 应用启动

    一.基础介绍 Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选文件,该文件包含响应 ASP.NET 或 HTTP 模块所引发的应用程序级别和会话级别事件的代码. Appl ...

  3. CORDIC原理与FPGA实现(2)

    CORDIC算法实现极坐标(polar)到直角坐标系(Cartesian)的变换. 1: function [horizonal,vertical]=polar2car(mag, pha); 2: x ...

  4. 深入理解maven及应用--转

    (一):生命周期和插件 在项目里用了快一年的maven了,最近突然发现maven项目在eclipse中build时非常慢,因为经常用clean install命令来build项目,也没有管那么多,但最 ...

  5. java 16 - 9 增强for的概述和使用

    JDK5的新特性:自动拆装箱,泛型,增强for,静态导入,可变参数,枚举 增强for:是for循环的一种. 格式: for(元素数据类型 变量 : 数组或者Collection集合) { 使用变量即可 ...

  6. 详解反射->Type.System

    反射先了解 一:system.Type 获取基本信息: Type.Name   //类名 Type.FullName //完整路径 Type.Namespace //空间名 public class ...

  7. 常用类库——StringBuffer类

    掌握目标: 1,掌握StringBuffer与String的区别. 2,掌握StringBuffer常用方法. 3,掌握StringBuffer实际应用. 1,认识StringBuffer. Stri ...

  8. 会报编译器警告的Xcode 6.3新特性:Nullability Annotations

    最近在用Xcode 6.3写代码,一些涉及到对象的代码会报如下编译器警告: 1 Pointer is missing a nullability type specifier (__nonnull o ...

  9. 第四章 在MVC4.0中对脚本以及样式表的引用变化

    原文:http://www.cnblogs.com/xdotnet/archive/2012/07/21/aspnet40_webpage20.html 一.可以直接使用“~”,而无需使用Href对象 ...

  10. 如何将NTFS格式的移动硬盘挂接到Mac OS上进行读写(Read/Write)操作

    现在硬盘便宜,很多同学都有移动硬盘,如果你同时使用Windows与Mac OS的话,移动硬盘最好不要使用NTFS文件系统,否则在Mac OS上,你只能读你的移动硬盘,不能写. 但是实际上的情况是,移动 ...