iOS 轮播图--特效
基于collectionView的轮播图特效
demo下载地址Git下载地址
1、自定义collection
//
// CircleLayout.h
// LDTXDEMO
//
// Created by 罗东 on 2018/1/16.
// Copyright © 2018年 LuoDong. All rights reserved.
// gitHub:https://github.com/TonyDongDong/CollectionView.git
//
#import "CircleLayout.h"
@implementation CircleLayout
-(CGSize)collectionViewContentSize
{
float width = self.collectionView.frame.size.width *([self.collectionView numberOfItemsInSection:0 ]+2);
float height= self.collectionView.frame.size.height;
CGSize size = CGSizeMake(width, height);
return size;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
#pragma mark - UICollectionViewLayout
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
//3D代码
UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
UICollectionView *collection = self.collectionView;
float width = collection.frame.size.width;
float x = collection.contentOffset.x;
CGFloat arc = M_PI * 2.0f;
NSInteger numberOfVisibleItems = [self.collectionView numberOfItemsInSection:0 ];
attributes.center = CGPointMake(x+[UIScreen mainScreen].bounds.size.width/2,125.0f);
attributes.size = CGSizeMake([UIScreen mainScreen].bounds.size.width-200, 250);
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1.0f/700.0f;
CGFloat radius = attributes.size.width/2/ tanf(arc/2.0f/numberOfVisibleItems);
CGFloat angle = (indexPath.row-x/width+1)/ numberOfVisibleItems * arc;
transform = CATransform3DRotate(transform, angle, 0.0f, 1.0f, 0.0f);
transform = CATransform3DTranslate(transform, 0.f, 0.0f, radius);
attributes.transform3D = transform ;
return attributes;
}
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *arr = [super layoutAttributesForElementsInRect:rect];
if ([arr count] >0) {
return arr;
}
NSMutableArray* attributes = [NSMutableArray array];
for (NSInteger i=0 ; i < [self.collectionView numberOfItemsInSection:0 ]; i++) {
NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
}
return attributes;
}
//
// ShowImageCell.m
// LDTXDEMO
//
// Created by 罗东 on 2018/1/16.
// Copyright © 2018年 LuoDong. All rights reserved.
// gitHub:https://github.com/TonyDongDong/CollectionView.git
//
#import "ShowImageCell.h"
@implementation ShowImageCell
@synthesize imageView;
@synthesize titleLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
imageView = [[UIImageView alloc] init];
[self.contentView addSubview:imageView];
titleLabel = [[UILabel alloc] init];
titleLabel.textColor = [UIColor whiteColor];
[self.contentView addSubview:titleLabel];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
imageView.frame = self.contentView.bounds;
titleLabel.frame = CGRectMake(0.0f,0.0f , self.contentView.bounds.size.width, 44.0f);
}
@end
3、自定义的homeView
//
// HomeHeaderView.m
// LDTXDEMO
//
// Created by 罗东 on 2018/1/16.
// Copyright © 2018年 LuoDong. All rights reserved.
// gitHub:https://github.com/TonyDongDong/CollectionView.git
//
#import "HomeHeaderView.h"
#import "ShowImageCell.h"
#import "CircleLayout.h"
#import "UIImageView+WebCache.h"
@interface HomeHeaderView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UICollectionView *collectionView;
@end
@implementation HomeHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createUI];
}
return self;
}
-(void)createUI{
float width = self.frame.size.width;
float height = self.frame.size.height;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width, height) collectionViewLayout:[[CircleLayout alloc] init]];
[self.collectionView registerClass:[ShowImageCell class] forCellWithReuseIdentifier:@"identifier"];
self.collectionView.backgroundColor = [UIColor grayColor];
self.collectionView.delegate = self;
[self.collectionView setContentOffset:CGPointMake(width, 0.0F)];
self.collectionView.dataSource = self;
[self addSubview:self.collectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ShowImageCell *cell = (ShowImageCell *)[cView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
if (!cell) return nil;
if (self.imgArray.count) {
NSString *imageName = self.imgArray[indexPath.row];
if ([imageName hasPrefix:@"http"]) {
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:@""]];
}else{
cell.imageView.image = [UIImage imageNamed:imageName];
}
}
// NSString *imageName = [NSString stringWithFormat:@"%ld.jpg",(long)indexPath.row+1];
// cell.imageView.image = [UIImage imageNamed:imageName];
// cell.titleLabel.text = imageName;
return cell;
}
//给图片赋值
-(void)setImgArray:(NSArray *)imgArray{
_imgArray = imgArray;
[self.collectionView reloadData];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
float targetX = scrollView.contentOffset.x;
scrollView.pagingEnabled = YES;
NSInteger numCount = [self.collectionView numberOfItemsInSection:0];
float ITEM_WIDTH = scrollView.frame.size.width;
if (numCount>=3)
{
if (targetX < ITEM_WIDTH/2) {
[scrollView setContentOffset:CGPointMake(targetX+ITEM_WIDTH *numCount, 0)];
}
else if (targetX >ITEM_WIDTH/2+ITEM_WIDTH *numCount)
{
[scrollView setContentOffset:CGPointMake(targetX-ITEM_WIDTH *numCount, 0)];
}
}
}
//
// HomeHeaderView.m
// LDTXDEMO
//
// Created by 罗东 on 2018/1/16.
// Copyright © 2018年 LuoDong. All rights reserved.
// gitHub:https://github.com/TonyDongDong/CollectionView.git
//
demo下载地址Git下载地址