[英]UICollectionViewLayout (Converting from Swift to Objective-C)
我目前正在关注本教程( https://www.raywenderlich.com/99087/swift-expanding-cells-ios-collection-views ),该教程创建了自定义UICollectionViewLayout,问题是该教程是用Swift编写的,但是我正在尝试将其转换为我的项目的Objective-C。
将本教程复制到Objective-C的第一部分很好,我已经到了这个阶段。
但是,在第二部分中,我们假设要创建一个自定义UICollectionViewLayout,并且在情节提要中将CollectionView的Layout更改为Custom并设置自定义类后,将出现一个空白屏幕。
以下是我从Swift到Objective-C的教程中复制的代码:
@implementation TimetableCustomLayout{
NSMutableArray *cache;
}
-(NSInteger)featuredItemIndex{
CGFloat dragOffset = 180;
return MAX(0, self.collectionView.contentOffset.y - dragOffset);
}
-(CGFloat)nextItemPercentageOffset{
CGFloat dragOffset = 180;
return (self.collectionView.contentOffset.y / dragOffset) - [self featuredItemIndex];
}
-(CGFloat)width{
return CGRectGetWidth(self.collectionView.bounds);
}
-(CGFloat)height{
return CGRectGetHeight(self.collectionView.bounds);
}
-(NSInteger)numberOfItems{
//Will be replaced with dynamic value later
return 5;
}
-(CGSize)collectionViewContentSize{
CGFloat dragOffset = 180;
return CGSizeMake([self height], [self width]);
}
-(void)prepareLayout{
[super prepareLayout];
cache = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];
self.standardHeight = 100;
self.featuredHeight = 280;
CGRect frame = CGRectZero;
CGFloat y = 0;
for(int item = 0; item < 5; item ++){
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.zIndex = item;
CGFloat height = self.standardHeight;
if(indexPath.item == [self featuredItemIndex]){
CGFloat yOffset = self.standardHeight * [self nextItemPercentageOffset];
y = self.collectionView.contentOffset.y - yOffset;
height = self.featuredHeight;
}else if(indexPath.item == ([self featuredItemIndex] + 1) && indexPath.item != [self numberOfItems]){
CGFloat maxY = y + self.standardHeight;
height = self.standardHeight + MAX((self.featuredHeight - self.standardHeight) * [self nextItemPercentageOffset], 0);
y = maxY - height;
}
frame = CGRectMake(0, y, [self width], [self height]);
attributes.frame = frame;
[cache addObject:attributes];
y = CGRectGetMaxY(frame);
}
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
[super layoutAttributesForElementsInRect:rect];
NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];
NSLog(@"%@", cache);
for(UICollectionViewLayoutAttributes *attributes in cache){
if(CGRectIntersectsRect(attributes.frame, rect)){
[layoutAttributes addObject:attributes];
}
}
return layoutAttributes;
}
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
return true;
}
@end
我也遇到错误,由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ + [UICollectionViewLayoutAttributes框架]:无法识别的选择器。
我认为该错误可能是由于从Swift到Objective-C的翻译不正确,尤其是这一行,
迅速:
var cache = [UICollectionViewLayoutAttributes]()
Objective-C的:
NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];
这是我对StackOverflow的第一个问题,非常感谢您的任何反馈和帮助。
您将在初始化时UICollectionViewLayoutAttributes
类的引用添加到cache
和layoutAttributes
数组。 然后,将frame
属性调用到类引用(注意,错误消息中的+
表示类方法,实例方法使用-
)。
替换此行:
NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];
有了这个:
NSMutableArray *layoutAttributes = [[NSMutableArray alloc] init];
cache
变量初始化也是如此。
您还可以使用泛型在Objective-C中使用类型安全的数组:
NSMutableArray<UICollectionViewLayoutAttributes*> *layoutAttributes = [[NSMutableArray alloc] init];
更多信息在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.