iOS边练边学--UIScrollView和xib文件实现简单分页+定时器初使用
一、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文件实现简单分页+定时器初使用的更多相关文章
- iOS边练边学--UIScrollView的属性简单使用,代理的简单介绍以及内容缩放
一.什么是UIScrollView *移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也是相当有限 *当展示的内容较多,超出一个屏幕时,用户可通过滚动收拾来查看屏幕以外的内容 *普通的UI ...
- iOS边练边学--Http网络再学习,简单介绍
一.URL 什么是URL URL中常见的协议 二.Http Http的基本通信过程 发送Http请求的方法 GET 和 POST 对比 GET 和 POST 的选择 三.iOS中的Http学习 iOS ...
- iOS边练边学--多线程介绍、NSThread的简单实用、线程安全以及线程之间的通信
一.iOS中的多线程 多线程的原理(之前多线程这块没好好学,之前对多线程的理解也是错误的,这里更正,好好学习这块) iOS中多线程的实现方案有以下几种 二.NSThread线程类的简单实用(直接上代码 ...
- iOS边练边学--xib文件初使用
一.Xib和storyboard对比 *共同点: 1>都用来描述软件界面 2>都用Interface Builder工具来编辑 3>本质都是转换成代码去创建控件 *不同点 1> ...
- iOS边练边学--自定义非等高的cell
一.使用xib或者storyboard自定义非等高的cell实现方式差不多,这里简单介绍一下通过xib文件实现的方法 <1.1>创建一个继承自UITableViewCell的子类,比如Ch ...
- iOS边练边学--自定义等高的cell
一.storyboard自定义cell <1>创建一个继承自UITableViewCell的子类,比如ChaosDealCell <2>在storyboard中 <2.1 ...
- iOS边练边学--CALayer,非根层隐式动画,钟表练习
一.CALayer UIView之所以能显示在屏幕上,完全是因为他内部的一个图层 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView的layer属性 ...
- iOS边练边学--UIGestureRecognizer手势识别器简单介绍
iOS 3.2之后,苹果退出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度. 一.UIGestureRecognizer UIGestureRe ...
- iOS边练边学--触摸事件以及能够拖拽的UIView的练习
一.用户在使用APP的过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: 二.响应者对象 在iOS中只有继承了了UIResponder的对象才能接受并处理事件,这样的对象称之为“响应者对象 ...
随机推荐
- Android开发学习之反编译APK文件
反编译的目的在于学习一些优秀的Android应用程序代码. 在进行反编译之前,需要准备好下面的软件工具(这些文件都放在同一文件下): 这些工具的下载地址:http://down.51cto.com/d ...
- lua学习笔记13:协程具体解释和举例
一.coroutine.create创建协程 參数是协程的主函数,返回一个thread对象 co = coroutine.create(function() print("coroutine ...
- 定时器quartz工具类
一.gradle配置 // https://mvnrepository.com/artifact/org.quartz-scheduler/quartz compile group: 'org.qua ...
- 分享Memcached shell启动停止脚本
注意:要使用这个shell,必须先成功建立memcache环境 1>建立memcached文件和权限 [root@luozhonghua ~]# touch /etc/init.d/memcac ...
- es6 解构赋值 新认知/新习惯
es6 的解构赋值其实很早就学习了,但一直纠结于习惯和可读性问题,所以没有大规模使用.最近被 react调教一番之后.已经完全融入认知和习惯中去了.总结一下三个常用的技巧: 对象取值 取值并重命名 剩 ...
- linux进程 kipmi0
top 发现负载很低,没有连接的时候,一个进程经常跳到最前面,用户是root, 命令是 kipmi0 , 后来查询了一下,很可能 是外部设备要使用到的 IPMI , 智能型平台管理接口(Intell ...
- android framework-安装samba
用于在windows下使用souceinsight访问linux中的android源代码. □ apt-get install samba samba-common #安装samba ...
- java.net.Socket/java.net.ServerSocket-TCP Socket编程
TCP 的 Java 支持 协议相当于相互通信的程序间达成的一种约定,它规定了分组报文的结构.交换方式.包含的意义以及怎样对报文所包含的信息进行解析,TCP/IP 协议族有 IP 协议.TCP 协议和 ...
- Flink articles
http://ictlabs-summer-school.sics.se/2015/slides/flink-advanced.pdf http://henning.kropponline.de/20 ...
- 每日英语:In Digital Era, What Does 'Watching TV' Even Mean?
We spend a full five hours and 16 minutes a day in front of a screen, and that's without even turnin ...