我很难在Spotify的播放器中创建这样的UICollectionView:

一只忙碌的cat

对我来说,问题有两个方面.

1) 我如何使细胞居中,这样你就可以看到中间的细胞,以及左边和右边的细胞.

  • 如果我创建正方形的单元,并在每个单元之间添加间距,则单元会正确显示,但不会居中.

2) PaginEnabled=YES时,collectionview可以正确地从一个页面滑动到另一个页面.但是,如果单元格不居中,它只是将集合视图移动到屏幕宽度的页面上.所以问题是如何让页面移动,从而获得上述效果.

3) 如何在细胞移动时设置其大小的动画

  • 我不想太担心这个.如果我能做到这一点,那就太好了,但更难的问题是1和2.

我目前的代码是一个简单的UICollectionView,带有普通的委托设置和自定义的UICollectionView单元格,这些单元格是正方形的.也许我需要子类UICollectionViewFlowLayout?或者我需要将PaginEnabled设置为NO,然后使用自定义滑动事件?愿意帮忙!

推荐答案

正如您在 comments 中所说,在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"中的完整工作演示

由于它很老,很流行,你可以找到一些相关的教程,它也会比自定义代码实现稳定得多

Objective-c相关问答推荐

如何限制 UITextField 中的特殊字符(点和下划线除外)?

iOS9 - 这个应用程序正在从后台线程修改自动布局引擎 - 在哪里?

如何使 UIView 动画序列重复和自动反转

在 iOS 中实现闪屏

dispatch_get_global_queue 和 dispatch_queue_create 有什么区别?

为什么当我使其处于非活动状态时,弱 IBOutlet NSLayoutConstraint 变为 nil?

自动布局和按下时隐藏底栏

自动属性合成 (@property) 和继承

在 macOS 10.9+ 上删除 plist 文件不会重置应用程序

启动 Finder 窗口并 Select 特定文件

在基于视图的 NSTableView 上更改 Select colored颜色

适用于 ios 的 360° 全景库

iOS 在设备旋转时更改自动布局约束

如何计算多边形的圆角?

进入编辑模式时动画自定义绘制的 UITableViewCell

Electron邮件镇静 iOS 8

更改 UISearchBar 的字体大小

Objective-C 中的属性和实例变量

如何减小使用 UIImagePickerController 创建的视频的文件大小?

在 iOS UILabel 上设置 BOLD 字体