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的对象才能接受并处理事件,这样的对象称之为“响应者对象 ...
随机推荐
- Google C++单元测试框架
一.概述 Google C++单元测试框架(简称Gtest),可在多个平台上使用(包括Linux, Mac OS X, Windows, Cygwin和Symbian),它提供了丰富的断言.致命和非致 ...
- Android用shareUserID实现多个Activity显示在同一界面
近来整理文档,发现两年前研究Android多个Activity叠加显示的方案.时光荏苒,一去不回. 虽然后来没有用上,但还是整理如下,Android版本还是2.2的: ActivityGroup描画方 ...
- 转 selenium 自动下载文件
#coding=utf-8from selenium import webdriver #实例化一个火狐配置文件fp = webdriver.FirefoxProfile() #设置各项参数,参数可以 ...
- python标准库介绍——19 mmap 模块详解
==mmap 模块== (2.0 新增) ``mmap`` 模块提供了操作系统内存映射函数的接口, 如 [Example 2-13 #eg-2-13] 所示. 映射区域的行为和字符串对象类似, 但数据 ...
- 帆软报表和jeecg的进一步整合--ajax给后台传递map类型的参数
下面是页面代码: <%@ page language="java" contentType="text/html; charset=UTF-8" page ...
- eclipse部署class默认在build文件夹
1.eclipse新建的Dynamic web project 默认是将类编译在build如果在eclipse中配置了tomcate(server项),用自带的发布功能,是能自动识别的.2.自已修改到 ...
- spring framework 4 源代码阅读(2)---从ClassPathXmlApplicationContext開始
Application初始化日志 15:23:12.790 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperti ...
- 简易C#动态加载dll(实现插件化)
可以通过该方法来实现程序插件化. 假设A,B两个类,A类为宿主,B类为插件需要加载到A类中: class Program { public interface IHellow { void Hello ...
- spring bean autowire自动装配
转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139 autowire="byName"会自动装配属性与Bea ...
- Linux经常使用命令(六) - mv
mv命令是move的缩写.能够用来移动文件或者将文件改名(move (rename) files),是Linux系统下经常使用的命令,经经常使用来备份文件或者文件夹. 1.命令格式: mv [选项] ...