iOS开发自定义流水布局
//集成UICollectionViewFlowLayout 自己写的布局
// SJBFlowLayout.m
// 自定义流水布局
//
// Created by zyyt on 16/7/20.
// Copyright © 2016年 sjb. All rights reserved.
//
#import "SJBFlowLayout.h"
/*******分割线*******分割线********分割线*******分割线 *********分割线*********分割线***********/
@implementation SJBFlowLayout
/**
//告诉布局对象去更像布局
Tells the layout object to update the current layout.
Layout updates occur the first time the collection view presents its content and whenever the layout is invalidated explicitly or implicitly because of a change to the view.
//在布局每一次更新的时候,collection都会唤醒这个方法
During each layout update, the collection view calls this method first to give your layout object a chance to prepare for the upcoming layout operation.
//这个方法默认什么也没实现 子类可以重写这个方法和用它去组建数据结构或实现任一个
The default implementation of this method does nothing. Subclasses can override it and use it to set up data structures or perform any initial computations needed to perform the layout later.
**/
- (void)prepareLayout
{
[super prepareLayout];
CGFloat inset = (self.collectionView.frame.size.width - self.itemSize.width) * 0.5;
/*
The distance that the content view is inset from the enclosing scroll view.
Use this property to add to the scrolling area around the content. The unit of size is points. The default value is UIEdgeInsetsZero.
*/
self.collectionView.contentInset = UIEdgeInsetsMake(0, inset, 0, inset);
}
/*Asks the layout object if the new bounds require a layout update.
The new bounds of the collection view.
Parameters
newBounds
The new bounds of the collection view.
Returns YES if the collection view requires a layout update or NO if the layout does not need to change.
*/
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
/*
Returns the layout attributes for all of the cells and views in the specified rectangle.
The rectangle (specified in the collection view’s coordinate system) containing the target views.
Parameters
rect
The rectangle (specified in the collection view’s coordinate system) containing the target views.
Returns An array of UICollectionViewLayoutAttributes objects representing the layout information for the cells and views.
*/
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray * attArr = [super layoutAttributesForElementsInRect:rect];
/*
The collection view object currently using this layout object. (read-only)
The collection view object sets the value of this property when a new layout object is assigned to it.
*/
CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;
for (int i=0; i<attArr.count; i++) {
UICollectionViewLayoutAttributes * att = attArr[i];
CGFloat delta = ABS(att.center.x - centerX);
CGFloat scale = 1 - delta / self.collectionView.bounds.size.width;
att.transform = CGAffineTransformMakeScale(scale, scale);
}
return attArr;
}
/*
Returns the point at which to stop scrolling.
The proposed point (in the collection view’s content view) at which to stop scrolling. This is the value at which scrolling would naturally stop if no adjustments were made. The point reflects the upper-left corner of the visible content.
Parameters
proposedContentOffset
The proposed point (in the collection view’s content view) at which to stop scrolling. This is the value at which scrolling would naturally stop if no adjustments were made. The point reflects the upper-left corner of the visible content.
velocity
The current scrolling velocity along both the horizontal and vertical axes. This value is measured in points per second.
Returns The content offset that you want to use instead.
*/
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGRect rect;
rect.origin.y = 0;
rect.origin.x = proposedContentOffset.x;
rect.size.width = self.collectionView.bounds.size.width;
rect.size.height = self.collectionView.bounds.size.height;
NSArray * attArr = [super layoutAttributesForElementsInRect:rect];
//The point at which the origin of the content view is offset from the origin of the scroll view.
CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;
CGFloat mindel = MAXFLOAT;
for (int i= 0; i<attArr.count; i++) {
UICollectionViewLayoutAttributes * attributes = attArr[i];
if ( ABS(mindel) > ABS(attributes.center.x - centerX)) {
mindel = attributes.center.x - centerX;
}
}
CGPoint point = CGPointMake(proposedContentOffset.x + mindel, 0);
return point ;
}
@end
/*******分割线*******分割线********分割线*******分割线 *********分割线*********分割线***********/
// ViewController.m
// 自定义流水布局
//
// Created by zyyt on 16/7/20.
// Copyright © 2016年 sjb. All rights reserved.
//
#import "ViewController.h"
#import "SJBFlowLayout.h"
#import "SJBCollectionCell.h"
#import "AFNetworking.h"
@interface ViewController ()<UICollectionViewDataSource>
@property (nonatomic,strong)NSMutableArray * dataSouce;
@property (nonatomic,strong)UICollectionView * collectionView;
@end
/*******分割线*******分割线********分割线*******分割线 *********分割线*********分割线***********/
static NSString * const cellID = @"cell";
@implementation ViewController
- (NSMutableArray *)dataSouce
{
if (_dataSouce == nil) {
_dataSouce = [NSMutableArray array];
}
return _dataSouce;
}
- (void)viewDidLoad {
[super viewDidLoad];
SJBFlowLayout * layout = [[SJBFlowLayout alloc] init];
layout.itemSize = CGSizeMake(100, 100);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width, 200) collectionViewLayout:layout];
self.collectionView.dataSource = self;
[self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SJBCollectionCell class]) bundle:nil] forCellWithReuseIdentifier:cellID];
[self.view addSubview:self.collectionView];
[self requestData];
}
- (void)requestData
{
NSMutableDictionary * params = [NSMutableDictionary dictionary];
params[@"a"] =@"list";
params[@"c"] = @"data";
params[@"type"] = @(10);
params[@"page"] = @(0);
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
[manager GET:@"http://api.budejie.com/api/api_open.php" parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSLog(@"%@",responseObject);
for (int i=0; i<[responseObject[@"list"] count]; i++) {
[ self.dataSouce addObject:responseObject[@"list"][i]];
}
[self.collectionView reloadData];
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
}];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataSouce.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
SJBCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
cell.modelDic = [self.dataSouce objectAtIndex:indexPath.item];
cell.contentView.backgroundColor = [UIColor whiteColor];
return cell;
}
@end
/*******分割线*******分割线********分割线*******分割线 *********分割线*********分割线***********/
#import "SJBCollectionCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Circal.h"
@interface SJBCollectionCell ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation SJBCollectionCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setModelDic:(NSDictionary *)modelDic
{
_modelDic = modelDic;
NSLog(@"%@",modelDic);
[self.imageView sd_setImageWithURL:[NSURL URLWithString:[modelDic objectForKey:@"image1"]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
self.imageView.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
} ];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
}
@end
iOS开发自定义流水布局的更多相关文章
- IOS开发之绝对布局和相对布局(屏幕适配)
之前如果做过Web前端页面的小伙伴们,看到绝对定位和相对定位并不陌生,并且使用起来也挺方便.在IOS的UI设计中也有绝对定位和相对定位,和我们的web前端的绝对定位和相对定位有所不同但又有相似之处.下 ...
- swift - uicollectionView自定义流水布局
TYWaterFallLayout 不规则流水布局 - swift3.0 配图 使用方法 //创建layout let layout = TYWaterFallLayout() layout.sect ...
- iOS开发自定义字体之静态字体
最后更新 2017-04-25 在iOS开发中经常会用到字体, 一般字体文件比较小的,单一的,几十k, 可以通过内置进去;如果字体文件比较多或者字体文件比较大,通常通过动态加载方式. 静态加载方式 将 ...
- iOS开发之蜂窝布局—Swift
前言 最近项目中用到了类似蜂窝的六边形布局,在这里分享出来抛砖引玉,供大家参考学习.本文提供了2种思路实现效果,第一种方式使用UICollectionView实现,第二种方式使用UIScrollVie ...
- iOS 开发自定义一个提示框
在开发的时候,会碰到很多需要提示的地方,提示的方法也有很多种,ios 8 以前的版本有alertview还是以后用的alertController,都是这种作用, 但是不够灵活,而且用的多了,用户体验 ...
- [IOS 开发] 自定义(重写) UITableViewCell的高亮背景色
IOS的sdk中,对UITableViewCell的高亮背景色只支持两种颜色,分别为UITableViewCellSelectionStyleBlue和UITableViewCellSelection ...
- iOS开发~UI布局(三)深入理解autolayout
一.概要 通过对iOS8界面布局的学习和总结,发现autolayout才是主角,autolayout是iOS6引入的新特性,当时还粗浅的学习了下,可是没有真正应用到项目中.随着iOS设备尺寸逐渐碎片化 ...
- iOS开发~UI布局(二)storyboard中autolayout和size class的使用详解
一.概要:前一篇初步的描述了size class的概念,那么实际中如何使用呢,下面两个问题是我们一定会遇到的: 1.Xcode6中增加了size class,在storyboard中如何使用? 2.a ...
- iOS开发~UI布局(一)初探Size Class
随着iOS8系统的发布,一个全新的页面UI布局概念出现,这个新特性将颠覆包括iOS7及之前版本的UI布局方式,这个新特性就是Size Class.Size Class配合Auto Layout可以解决 ...
随机推荐
- list、dict、tuple的一些小操作总结
一.list 1.赋值(append) list.append(data) 2.去重 list(set(list)) list_gpcode = list(set(list(dfQuery.index ...
- mac nodejs安装
很久没有配置开发环境了,刚换了新电脑,正好借机会重新配置一下node相关的开发环境 安装 nvm :Node Version Manager 由于nodejs版本更新迭代较快,而不同版本间的差异又很大 ...
- D3.js:饼状图的制作
假设有如下数据需要可视化: var dataset = [ 30 , 10 , 43 , 55 , 13 ]; 这样的值是不能直接绘图的.例如绘制饼状图的一个部分,需要知道一段弧的起始角度和终止角度, ...
- GridView”的控件 必须放在具有 runat=server 的窗体标记内 “错误提示”
在做导出数据到EXCEL程序中,出现了错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记 解决办法 重写 VerifyRendering ...
- java加解密
换工作中,把以前学的知识,整理整理.能否得到一份好的薪资且满意的工作,然后赢取白富美,走向人生的巅峰,就靠它了.哈哈. 对称加密:DES, AES DES (数据加密算法) : 明文按64位进行分组, ...
- 关于C#继承运用的总结
整体代码部分: 解决方案: 父类Person类: using System; using System.Collections.Generic; using System.Linq; using Sy ...
- 图像处理_imgproc笔记(1)
图像处理_滤波器 (1)图像的平滑处理 图像的平滑也称模糊,平滑处理需要一个滤波器,最常用的滤波器就是线性滤波器,线性滤波器的输出像素值是g(x,y),是输入像素值是 f(x,y)的加权和: ...
- js的特殊运算符
1)三元条件运算符: c是一个布尔值,当c为true的时候,取冒号左边a的值,否取冒号右边的b的值: 2)逗号运算符: 值从左到右依次计算,取最右边的,例如例子里的val,会取最右边的值3: 特殊运算 ...
- maven中在本地maven仓库添加jar包
Maven 手动添加 JAR 包到本地仓库 Maven 确确实实是个好东西,用来管理项目显得很方便,但是如果是通过 Maven 来远程下载 JAR 包的话,我宿舍的带宽是4兆的,4个人共用,有时候用 ...
- MySQL安装之zip格式
背景: 今天本来想学点JDBC的,没想到在MySQL的安装上卡了很久,特此写下此文,希望大家遇到类似问题可以早些跳出坑. 一.寻找资源 今天,为了学习JDBC,准备在公司的电脑上装MySQL,于是 ...