2023年7月14日发(作者:)
【IOS学习笔记】为UICollectionView设置⾃适应屏幕宽度以及点击效果1、设置代理@property (weak, nonatomic) IBOutlet UICollectionView *gridview;_urce=self;_te=self;2、实现⽅法笔者使⽤了⼀⾏3个,所以在计算宽度时除了3;间距是2所以3个⼦试图⼀共2个间距减去了4。*注意:由于sizeForItemAtIndexPath是UICollectionViewDelegateFlowLayout的实现⽅法,但是UICollectionViewDelegateFlowLayout协议的⽗级协议是UICollectionViewDelegate 所以可以直接实现sizeForItemAtIndexPath⽅法//设置⼤⼩
float width=(-4)/3; return CGSizeMake(width, width);}//设置⾏间距 return 2.0f;}//触发点击事件-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{}//设置允许⾼亮- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ return YES;}//点击结束- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell=[colView cellForItemAtIndexPath:indexPath]; oundColor=[UIColor whiteColor];}//点击中- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath{ UICollectionViewCell *cell=[colView cellForItemAtIndexPath:indexPath];// oundColor=RGB(226, 226, 226); [cell setBackgroundColor:RGB(226, 226, 226)];}-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexP- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSIn3、RGB为宏定义#define RGB(r, g, b) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689265220a226457.html
评论列表(0条)