iOS UICCollection Viewはカード効果を実現する。
今はカードの効果を使うアプリが多く、前の会社がカードの効果を実現させるために、カードの実現に関する文章を書きました。文章の最後にデモがついています。
実際にUICCollection Viewを使うことを選びました。UICCollection View Flow Layoutでスタイルをカスタマイズします。具体的な実現を見てみます。
具体的に実現する
1、UICCollection Viewを作成する
LHLeft CollocationView.m実現
モデル
ダウンロード
ギthub デモ
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。
実際にUICCollection Viewを使うことを選びました。UICCollection View Flow Layoutでスタイルをカスタマイズします。具体的な実現を見てみます。
具体的に実現する
1、UICCollection Viewを作成する
- (void)createCollectionView {
CGFloat pading = 0 * SCREEN_WIDTH/375;
LHLeftCollocationView * layout = [[LHLeftCollocationView alloc]init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = pading;
layout.minimumInteritemSpacing = pading;
// UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView3 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, imageHeight * SCREEN_RATE) collectionViewLayout:layout];
_collectionView3.tag = 33;
_collectionView3.dataSource = self;
_collectionView3.delegate = self;
_collectionView3.bounces = NO;
_collectionView3.alwaysBounceHorizontal = NO;
_collectionView3.alwaysBounceVertical = NO;
_collectionView3.backgroundColor = [UIColor grayColor];
_collectionView3.showsHorizontalScrollIndicator = NO;
_collectionView3.showsVerticalScrollIndicator = NO;
[self.view addSubview:_collectionView3];
[_collectionView3 registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:collectionViewCell];
}
2、具体的な代理方法UCollection View Delegateを実現し、UICCollection View Data Source
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.modelArray.count;
}
- (NSMutableArray *)modelArray {
if (!_modelArray) {
_modelArray = [NSMutableArray array];
}
return _modelArray;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollModel *infoModel = self.modelArray[indexPath.row];
NSLog(@"section:%ld --- row:%ld -----%@",indexPath.section,indexPath.row,infoModel.title);
CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCell forIndexPath:indexPath];
cell.itemModel = infoModel;
return cell;
}
// item
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat CWidth = 80 * SCREEN_RATE;
CGFloat CHeight = 80 * SCREEN_RATE;
return CGSizeMake(CWidth, CHeight);
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
CollModel *infoModel = self.modelArray[indexPath.row];
NSLog(@"infoModelArray----%@",infoModel.title);
}
3、カスタムUICCollection View Flow LayoutLHLeft CollocationView.m実現
#import "LHLeftCollocationView.h"
@implementation LHLeftCollocationView
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
CGRect targectRect = CGRectMake(proposedContentOffset.x, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
NSArray * attriArray = [super layoutAttributesForElementsInRect:targectRect];
CGFloat horizontalCenterX = proposedContentOffset.x + ([UIScreen mainScreen].bounds.size.width);
CGFloat offsetAdjustment = CGFLOAT_MAX;
for (UICollectionViewLayoutAttributes * layoutAttributes in attriArray) {
CGFloat itemHorizontalCenterX = layoutAttributes.center.x;
if (fabs(itemHorizontalCenterX-horizontalCenterX) < fabs(offsetAdjustment)) {
offsetAdjustment = itemHorizontalCenterX - horizontalCenterX;
}
}
return CGPointMake(proposedContentOffset.x , proposedContentOffset.y);
}
CGFloat ActiveDistance = 400; //
CGFloat ScaleFactor = 0.50; //
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray * array = [super layoutAttributesForElementsInRect:rect];
CGRect visibleRect = CGRectZero;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;
for (UICollectionViewLayoutAttributes *attributes in array) {
CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;
CGFloat normalizedDistance = fabs(distance / ActiveDistance);
CGFloat zoom = 1 - ScaleFactor * normalizedDistance;
NSLog(@"zoom----%f",zoom);
attributes.transform3D = CATransform3DMakeScale(1.0, zoom, 1.0);
//
attributes.frame = CGRectMake(attributes.frame.origin.x, attributes.frame.origin.y + zoom, attributes.size.width, attributes.size.height);
//
// CGFloat scrollDirectionItemHeight = self.itemSize.height;
// CGFloat sideItemFixedOffset = 0;
// sideItemFixedOffset = (scrollDirectionItemHeight - scrollDirectionItemHeight * 0.7) / 2;
// attributes.center = CGPointMake(attributes.center.x, attributes.center.y + zoom);
}
return array;
}
////
//-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
//{
// NSArray *arr = [self getCopyOfAttributes:[super layoutAttributesForElementsInRect:rect]];
// //
// CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.bounds.size.width/2.0f;
// // cell
// for (UICollectionViewLayoutAttributes *attributes in arr) {
// CGFloat distance = fabs(attributes.center.x - centerX);
// //
// CGFloat apartScale = distance/self.collectionView.bounds.size.width;
// // -π/4 +π/4
// CGFloat scale = fabs(cos(apartScale * M_PI/4));
// // cell 1
// attributes.transform = CGAffineTransformMakeScale(1.0, scale);
// }
// return arr;
//}
// attributes
- (NSArray *)getCopyOfAttributes:(NSArray *)attributes
{
NSMutableArray *copyArr = [NSMutableArray new];
for (UICollectionViewLayoutAttributes *attribute in attributes) {
[copyArr addObject:[attribute copy]];
}
return copyArr;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return true;
}
@end
4、カスタムセルとモデルモデル
#import <Foundation/Foundation.h>
@interface CollModel : NSObject
@property (nonatomic,strong)NSString *imgUrl;
@property (nonatomic,strong)NSString *title;
@property (nonatomic,strong)NSString *url;
@end
カスタムセル
#import <UIKit/UIKit.h>
#import "CollModel.h"
@interface CollectionViewCell : UICollectionViewCell
@property (nonatomic, strong) CollModel * itemModel;
@end
#import "CollectionViewCell.h"
#define SCREEN_RATE ([UIScreen mainScreen].bounds.size.width/375.0)
@interface CollectionViewCell()
/**
*
*/
@property (nonatomic, strong) UIImageView *itemIcon;
@property (nonatomic, strong) UILabel *itemLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@end
@implementation CollectionViewCell
@synthesize itemModel = _itemModel;
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.contentView.backgroundColor = [UIColor clearColor];
[self initView];
}
return self;
}
- (void)initView {
_itemIcon = [[UIImageView alloc] init];
[self.contentView addSubview:_itemIcon];
_itemIcon.backgroundColor = [UIColor clearColor];
// CGFloat iconWidth = ([UIScreen mainScreen].bounds.size.width / 5.0) * SCREEN_RATE;
_itemIcon.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
_itemIcon.center = self.contentView.center;
}
- (CollModel *)itemModel
{
return _itemModel;
}
- (void)setItemModel:(CollModel *)itemModel
{
if (!itemModel) {
return;
}
_itemModel = itemModel;
[self setCellWithModel:_itemModel];
}
- (void)setCellWithModel:(CollModel *)itemModel
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
_itemIcon.image = [UIImage imageNamed:itemModel.url];
}];
}
@end
実行効果ダウンロード
ギthub デモ
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。