一、xib文件构成

二、自定义控件类(xib文件与自定义控件类的文件名字相同,并且将xib文件中父类控件的类名改成自定义控件类的名称)

***********自定义控件类需要的属性*************

 #import <UIKit/UIKit.h>

 @interface ChaosPageView : UIView

 /** 图片数据 */
@property(nonatomic,strong) NSArray *images;
/** pageControl的当前页圆点颜色 */
@property(nonatomic,strong) UIColor *currentColor;
/** pageControl的其他页圆点的颜色 */
@property(nonatomic,strong) UIColor *otherColor;
/** 创建分页控件的类方法的声明 */
+(instancetype)pageView; @end

***********自定义控件类的实现*************

 //
// ChaosPageView.m
// scrollView分页
//
// Created by admin on 16/3/9.
// Copyright © 2016年 admin. All rights reserved.
// #import "ChaosPageView.h" @interface ChaosPageView () <UIScrollViewDelegate> // 扩展类 @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @end @implementation ChaosPageView +(instancetype)pageView
{
// 从xib中读取控件
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
} #pragma mark - 重写的set方法
-(void)setImages:(NSArray *)images
{
// 重写set方法一定记住先将变量赋值给成员变量
_images = images; // 添加之前先清除
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; for (int i = ; i<images.count; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:images[i]];
[self.scrollView addSubview:imageView];
}
} -(void)setCurrentColor:(UIColor *)currentColor
{
// 重写set方法一定记住先将变量赋值给成员变量
_currentColor = currentColor;
self.pageControl.currentPageIndicatorTintColor = currentColor;
} -(void)setOtherColor:(UIColor *)otherColor
{
// 重写set方法一定记住先将变量赋值给成员变量
_otherColor = otherColor;
self.pageControl.pageIndicatorTintColor = otherColor;
} -(void)layoutSubviews
{
[super layoutSubviews];
// 设置scrollView尺寸
self.scrollView.frame = self.bounds;
// 获取图片尺寸
CGFloat imgX = self.scrollView.frame.size.width;
CGFloat imgY = self.scrollView.frame.size.height;
// 设置scrollView范围
self.scrollView.contentSize = CGSizeMake(_images.count * imgX, );
self.scrollView.showsHorizontalScrollIndicator = NO; // 设置pageControl
CGFloat pageW = ;
CGFloat pageH = ;
CGFloat pageX = imgX - pageW;
CGFloat pageY = imgY - pageH;
self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH); // 设置每张图片的frame
for (int i = ; i < self.scrollView.subviews.count; i++) {
self.scrollView.subviews[i].frame = CGRectMake(i * imgX, , imgX, imgY);
}
self.pageControl.numberOfPages = self.images.count;
self.scrollView.pagingEnabled = YES;
} #pragma mark - <UIScrollViewDelegate>
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 计算当前页数的方法--用x方向的偏移量除以图片的宽度,结果四舍五入
// 四舍五入的方法--得到的结果+0.5,然后结果取整
self.pageControl.currentPage = (int)self.scrollView.contentOffset.x / self.scrollView.frame.size.width + 0.5;
} @end

三、定时器的使用

*********监听图片的滚动***********

iOS边练边学--UIScrollView和xib文件实现简单分页+定时器初使用的更多相关文章

  1. iOS边练边学--UIScrollView的属性简单使用,代理的简单介绍以及内容缩放

    一.什么是UIScrollView *移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也是相当有限 *当展示的内容较多,超出一个屏幕时,用户可通过滚动收拾来查看屏幕以外的内容 *普通的UI ...

  2. iOS边练边学--Http网络再学习,简单介绍

    一.URL 什么是URL URL中常见的协议 二.Http Http的基本通信过程 发送Http请求的方法 GET 和 POST 对比 GET 和 POST 的选择 三.iOS中的Http学习 iOS ...

  3. iOS边练边学--多线程介绍、NSThread的简单实用、线程安全以及线程之间的通信

    一.iOS中的多线程 多线程的原理(之前多线程这块没好好学,之前对多线程的理解也是错误的,这里更正,好好学习这块) iOS中多线程的实现方案有以下几种 二.NSThread线程类的简单实用(直接上代码 ...

  4. iOS边练边学--xib文件初使用

    一.Xib和storyboard对比 *共同点: 1>都用来描述软件界面 2>都用Interface Builder工具来编辑 3>本质都是转换成代码去创建控件 *不同点 1> ...

  5. iOS边练边学--自定义非等高的cell

    一.使用xib或者storyboard自定义非等高的cell实现方式差不多,这里简单介绍一下通过xib文件实现的方法 <1.1>创建一个继承自UITableViewCell的子类,比如Ch ...

  6. iOS边练边学--自定义等高的cell

    一.storyboard自定义cell <1>创建一个继承自UITableViewCell的子类,比如ChaosDealCell <2>在storyboard中 <2.1 ...

  7. iOS边练边学--CALayer,非根层隐式动画,钟表练习

    一.CALayer UIView之所以能显示在屏幕上,完全是因为他内部的一个图层 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView的layer属性 ...

  8. iOS边练边学--UIGestureRecognizer手势识别器简单介绍

    iOS 3.2之后,苹果退出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度. 一.UIGestureRecognizer UIGestureRe ...

  9. iOS边练边学--触摸事件以及能够拖拽的UIView的练习

    一.用户在使用APP的过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: 二.响应者对象 在iOS中只有继承了了UIResponder的对象才能接受并处理事件,这样的对象称之为“响应者对象 ...

随机推荐

  1. iOS - UIPasteboard

    前言 NS_CLASS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED @interface UIPasteboard : NSOb ...

  2. lsnrctl: error while loading shared libraries: /opt/app/oracle/product/11.2/db_1/lib/libclntsh.so.11

    错误描述: 安装好数据库后,在oralce用户下敲入 查看监听状态命令,返回错误提示 [oracle@centos3 ~]$ lsnrctl statuslsnrctl: error while lo ...

  3. linux ---性能监控(工具)

    linux服务器性能监控-nmon Nmon 是一个分析aix和linux性能的免费工具,出自IBM,其采集的数据通过nmon_analyser生成报表 一.下载 官网下载地址 百度网盘 二.运行和使 ...

  4. PSQL_标准API和Interface基本的用法和比较(概念)

    2014-01-05 Created By BaoXinjian

  5. OGG_GoldenGate日常维护(案例)

    2014-03-12 Created By BaoXinjian

  6. epoll 系列函数简介、与select、poll 的区别

    一.epoll 系列函数简介 #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...

  7. HDU 3849 By Recognizing These Guys, We Find Social Networks Useful(双连通)

    HDU 3849 By Recognizing These Guys, We Find Social Networks Useful pid=3849" target="_blan ...

  8. pandas 按照某一列进行排序

    pandas排序的方法有很多,sort_values表示根据某一列排序 pd.sort_values("xxx",inplace=True) 表示pd按照xxx这个字段排序,inp ...

  9. 练习 Dijkstra 最短路径算法。

    练习 Dijkstra 最短路径算法. #coding: utf-8 # Author: woodfox, Oct 14, 2014 # http://en.wikipedia.org/wiki/Di ...

  10. TP关联模型

    <?php class ExpertiseModel extends RelationModel { protected $_link=array( 'Role'=> array( 'ma ...