尝试像这样在Spotify的Player中创建UICollectionView时,我遇到很多困难:
我的问题有两个。
1)如何将单元格居中,以便您可以看到中间单元格以及左右单元格之一。
2)使用pageingEnabled = YES,collectionview可以正确地从一页滑动到另一页。但是,如果不将单元格居中放置,它只会将集合视图移动到整个页面(即屏幕的宽度)上。因此,问题是如何使页面移动,从而获得上述效果。
3)当细胞移动时,如何对它们的大小进行动画处理
我当前拥有的代码是一个简单的UICollectionView,具有常规的委托设置和自定义的UICollectionview单元格(正方形)。也许我需要将UICollectionViewFlowLayout子类化吗?还是我需要将pageEnabled设置为NO,然后使用自定义滑动事件?希望有帮助!
正如您在评论中所说的那样,您希望在Objective-c代码中有一个非常著名的库,名为iCarousel,可以帮助您满足需求。链接:https://github.com/nicklockwood/iCarousel
您可以使用'Rotary'或'Linear'或其他样式,只需很少或不做任何修改即可实现自定义视图
要实现它,您只实现了它的一些委托方法,并且它适用于ex:
//specify the type you want to use in viewDidLoad
_carousel.type = iCarouselTypeRotary;
//Set the following delegate methods
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
return [_items count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling
if (view == nil)
{
//don't do anything specific to the index within
//this `if (view == nil) {...}` statement because the view will be
//recycled and used with other index values later
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
((UIImageView *)view).image = [UIImage imageNamed:@"page.png"];
view.contentMode = UIViewContentModeCenter;
label = [[UILabel alloc] initWithFrame:view.bounds];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [label.font fontWithSize:50];
label.tag = 1;
[view addSubview:label];
}
else
{
//get a reference to the label in the recycled view
label = (UILabel *)[view viewWithTag:1];
}
//set item label
label.text = [_items[index] stringValue];
return view;
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
if (option == iCarouselOptionSpacing)
{
return value * 1.1;
}
return value;
}
您可以从Github存储库链接中随附的“ Examples / Basic iOS Example ”中查看完整的工作演示。
由于它已经很老了并且很流行,所以您可以找到一些相关的教程,并且它比自定义代码实现要稳定得多。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句