使用UICollectionView实现首页的滚动效果

实现类似这样的效果,可以滚动大概有两种实现方案
1. 使用scrollview来实现
2. 使用UICollectionView来实现
第一种比较简单,而且相对于性能来说不太好,于是我们使用第二种方案
UICollectionView 的基础知识再次就不做说明了,在网上随便一搜都是一大把,我们就说说这个如何实现的吧,
其实很简单

就这么几个文件。
先看看控制器里边的代码
import UIKit
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
var myCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.backgroundColor = UIColor(red: /255.0, green: /255.0, blue: /255.0, alpha: 1.0)
setupCollectionView()
}
// set up collection view
private func setupCollectionView() {
let screenW = UIScreen.mainScreen().bounds.size.width;
var rect = view.bounds
rect.size.height = (screenW - * - * ) / + +
rect.origin.y =
myCollectionView = UICollectionView(frame: rect, collectionViewLayout: collectionLayout())
myCollectionView.backgroundColor = UIColor.whiteColor()
myCollectionView.pagingEnabled = true
myCollectionView.showsHorizontalScrollIndicator = true
myCollectionView.delegate = self
myCollectionView.dataSource = self
view.addSubview(myCollectionView)
myCollectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
}
// set up layout
private func collectionLayout() -> UICollectionViewLayout {
let layout = CustomLayout()
return layout;
}
//MARK:- collection view data source
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print(indexPath.row)
}
}
就是在控制器中见了一个UICollectionView 所有的设置跟我们平时使用的一模一样,唯一不同的地方就是我们自定义的布局

import UIKit let edgeMargin: CGFloat = // 边界的间距
let padding: CGFloat = // 内部每个cell的间距
let column: Int = // 每页有多少列
let row: Int = // 每页有多少行 class CustomLayout: UICollectionViewLayout { private var layoutAttr: [UICollectionViewLayoutAttributes] = [] var totalCount: Int { // 有多少cell
get {
return collectionView!.numberOfItemsInSection()
}
} var page: Int {
get {
let numOfPage: Int = column * row
return totalCount / numOfPage +
}
} // 重写此方法,自定义想要的布局
override func prepareLayout() {
super.prepareLayout() // 这个方法最主要的任务是计算出每个cell的位置
layoutAttr = []
var indexPath: NSIndexPath
for index in ..<totalCount {
indexPath = NSIndexPath(forRow: index, inSection: )
let attributes = layoutAttributesForItemAtIndexPath(indexPath)! layoutAttr.append(attributes)
}
} override func collectionViewContentSize() -> CGSize { // 返回滚动的Size,根据页数计算出来
return CGSizeMake(collectionView!.frame.size.width * CGFloat(page), collectionView!.frame.size.height)
} override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
} override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { // 这个方法用来计算每一个cell的位置
let att: UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath) let collectW: CGFloat = collectionView!.frame.size.width // collection view 宽度
let numOfPage: Int = column * row
let pageIndex: Int = indexPath.row / numOfPage // 当前cell处在哪一个页
let columnInPage: Int = indexPath.row % numOfPage % column // 当前cell 在当前页的哪一列,用来计算位置
let rowInPage: Int = indexPath.row % numOfPage / column // 当前cell 在当前页的哪一行,用来计算位置
// 计算宽度
let cellW: CGFloat = (collectW - edgeMargin * - CGFloat(column - ) * padding) / CGFloat(column)
// 高度
let cellH: CGFloat = cellW
// x
let cellX: CGFloat = collectW * CGFloat(pageIndex) + edgeMargin + (cellW + padding) * CGFloat(columnInPage)
// y
let cellY :CGFloat = edgeMargin + (cellH + padding) * CGFloat(rowInPage) att.frame = CGRectMake(cellX, cellY, cellW, cellH)
return att
} override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return layoutAttr
} }
prepareLayout 这个方法使我们自定义布局的方法
只要我们自定义了这几个方法就是实现了上边图片中的效果, 其实到这里,就跟使用scrollviewview 差不多了。效果图如下下载地址 https://github.com/agelessman/ScrollItemView
使用UICollectionView实现首页的滚动效果的更多相关文章
- css实现视差滚动效果
今天逛京东金融的时候发现他家网站首页的滚动效果看着很有意思,于是就做了一个,demo链接http://1.liwenyang.applinzi.com/index.html 大多数的视差滚动效果都是使 ...
- 【angularjs】使用angularjs模拟淘宝首页-淘宝头条滚动效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [iOS] UICollectionView实现图片水平滚动
最新更新: 简单封装了一下代码,参考新文章:UICollectionView实现图片水平滚动 先简单看一下效果: 新博客:http://wossoneri.github.io 准备数据 首先先加入一些 ...
- [ios]新手笔记-。-UIPickerView 关于伪造循环效果和延时滚动效果
查找了网上资料,循环效果绝大部分都是增加行数来制造循环的错觉,延时滚动就是利用NSTimer间隔出发滚动事件来制造滚动效果. 代码: #import <UIKit/UIKit.h>#imp ...
- 全屏滚动效果H5FullscreenPage.js
前提: 介于现在很多活动都使用了 类似全屏滚动效果 尤其在微信里面 我自己开发了一个快速构建 此类项目的控件 与市面上大部分控件不同的是此控件还支持元素的动画效果 并提供多种元素效果 基于zepto. ...
- HTML标签marquee实现滚动效果
html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制.使用marquee标记不仅可以移动文字,也可以移动图片,表格等.只需要在<ma ...
- Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片
Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片 自定义ADPager 自定义水平滚动的ScrollView效仿ViewPager 当遇到要在Vie ...
- marquee标签实现页面内容的滚动效果
页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...
- marquee 实现首尾相连循环滚动效果
<marquee></marquee>可以实现多种滚动效果,无需js控制.使用marquee标签不仅可以滚动文字,也可以滚动图片,表格等 marquee标签不是HTML3.2 ...
随机推荐
- CORS详解[译]
介绍 由于同源策略的缘故,以往我们跨域请求,会使用诸如JSON-P(不安全)或者代理(设置代理和维护繁琐)的方式.而跨源资源共享(Cross-Origin Resource Sharing)是一个W3 ...
- 怎么让网站在本地支持SSL?
打开vs,点击项目,查看属性,打开ssl 如果有什么危险提示,就允许 右击项目,选择属性 运行项目
- 学习笔记之MVC级联及Ajax操作
由于刚转型到MVC,MVC的架构模式很多不是很清楚,比如今天就想做个级联的操作,因为之前的ASP.NET的方式是通过:控件-->添加事件-->后台编写级联事件进行触发,但是这个MVC就不同 ...
- 【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~
一.写在前面 自使用android studio开始,就被它独特的依赖方式:compile 'com.android.support:appcompat-v7:25.0.1'所深深吸引,自从有了它,麻 ...
- php cryptr 加密函数
class CryptHelper { /** * 加密 * @param unknown $password * @param unknown $salt * @return string */ p ...
- bash字符串操作
参考 http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html 问题:bash怎么提取字符串的最后一位?例如python中strin ...
- (整理)MyBatis入门教程(一)
本文转载: http://www.cnblogs.com/hellokitty1/p/5216025.html#3591383 本人文笔不行,根据上面博客内容引导,自己整理了一些东西 首先给大家推荐几 ...
- 我的MYSQL学习心得(五) 运算符
我的MYSQL学习心得(五) 运算符 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...
- 利用HAProxy代理SQL Server的AlwaysOn辅助副本
利用HAProxy代理SQL Server的AlwaysOn辅助副本 公司最近数据库升级到SQL Server2014 ,并部署了alwayson高可用集群 机房内有三套程序需要读取数据库 第一套:主 ...
- 快速开发Grunt插件----压缩js模板
前言 Grunt是一款前端构建工具,帮助我们自动化搭建前端工程.它可以实现自动对js.css.html文件的合并.压缩等一些列操作.Grunt有很多插件,每一款插件实现某个功能,你可以通过npm命名去 ...
下载地址