iOS UICCollection Viewは横スライドを実現します。

5664 ワード

本論文の例では、iOS UICCollection Viewの横方向スライドを実現する具体的なコードを共有します。
UICCollection Viewの横スクロールは、入力ボックスの入力履歴を表示するために使用されています。

//
// SCVisitorInputAccessoryView.m
//         --         InputAccessory

#import "SCInputAccessoryView.h"
#import "SCInputAccessoryCell.h"

#define SCHorizontalMargin 15.0f
#define SCVerticalMargin 10.0f

@interface SCInputAccessoryView () <UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

///        
@property (nonatomic, strong) NSMutableArray *nameArray;

@end


@implementation SCInputAccessoryView

+ (instancetype)loadNibView {
 return [[[NSBundle mainBundle] loadNibNamed:[SCInputAccessoryView className] owner:self options:nil] objectAtIndex:0];
}

- (void)awakeFromNib {
 [super awakeFromNib];
 self.clipsToBounds = YES;
 self.collectionView.delegate = self;
 self.collectionView.dataSource = self;
 [self setupView];
}

- (void)setupView {

 ///       yes           
 self.collectionView.alwaysBounceHorizontal = YES;
 self.collectionView.showsHorizontalScrollIndicator = NO;
 // 1.      
 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
 layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
 self.collectionView.collectionViewLayout = layout;
 [self registerNibWithTableView];
}

#pragma mark -      Delegate Methods
//     

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
 return 1;
}

//           
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
 return self.nameArray.count;
}

//   cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

 SCInputAccessoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SCInputAccessoryCell class]) forIndexPath:indexPath];
 [cell refreshCellWithTitle:self.nameArray[indexPath.row]];
 return cell;
}

//   cell   itemSize:      cell       
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
 CGFloat height = 35.0f;
 CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height];
 return CGSizeMake(width, height);
}


//   UIcollectionView      (  item     )
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
 //        
 return UIEdgeInsetsMake(SCVerticalMargin, SCHorizontalMargin, SCVerticalMargin, SCHorizontalMargin);
}

//   minimumLineSpacing:cell         
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
 return SCHorizontalMargin;
}

//   minimumInteritemSpacing:cell         
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
 return SCHorizontalMargin;
}

//   cell   
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 if (self.selectRowBlock) {
 self.selectRowBlock(indexPath.row, self.nameArray[indexPath.row]);
 }
}

#pragma mark -      Public Methods
/// array                 
- (void)refreshUIWithNameArray:(NSArray<NSString *> *)array {
 [self.nameArray removeAllObjects];
 [self.nameArray addObjectsFromArray:array];
 [self.collectionView reloadData];
}


#pragma mark -      Private Methods
//   cell
- (void)registerNibWithTableView {
 [self.collectionView registerNib:[UINib nibWithNibName:[SCInputAccessoryCell className] bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([SCInputAccessoryCell class])];
}

- (CGFloat)gainStringWidthWithString:(NSString *)string font:(CGFloat)font height:(CGFloat)height {

 if (string.length == 0) {
 return 0.0f;
 }

 CGSize maxSize = CGSizeMake(MAXFLOAT, height);
 CGSize realSize = [string boundingRectWithSize:maxSize
   options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
   attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
   context:nil].size;
 ///    16
 return (realSize.width + 2 * (SCHorizontalMargin + 1.f));
}

#pragma mark -   /     Action Methods

#pragma mark -     Lazy Load

- (NSMutableArray *)nameArray {
 if (!_nameArray) {
 _nameArray = [NSMutableArray arrayWithCapacity:0];
 }
 return _nameArray;
}

@end
効果図:

Demo住所:XGDevelopDemo
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。