逆时针iOS CircleLayout

时间:2014-04-23 18:45:04

标签: ios layout uicollectionview

我在WWDC上使用了稍微修改过的Apple的CircleLayout版本:https://github.com/mpospese/CircleLayout

我当前的代码在顶部绘制第一个元素,然后以顺时针方式绘制其余元素。如何使用此代码,以便布局从顶部的第一个元素开始,沿路径逆时针绘制下一个元素?我的三角学有点生锈。

我认为需要更改的代码部分是:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
{
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
    attributes.size = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
    attributes.center = CGPointMake(_center.x + _radius * cosf(2 * path.item * M_PI / _cellCount - M_PI / 2),
                                    _center.y + _radius * sinf(2 * path.item * M_PI / _cellCount - M_PI / 2));
    return attributes;
}

电流:

enter image description here

所需:

enter image description here

2 个答案:

答案 0 :(得分:1)

当需要相反方向时,必须使用负值。

答案 1 :(得分:0)

用负值替换角度会改变方向。在你的情况下

attributes.center = CGPointMake(_center.x + _radius * cosf(-2 * path.item * M_PI / _cellCount - M_PI / 2),
                                _center.y + _radius * sinf(-2 * path.item * M_PI / _cellCount - M_PI / 2));

应该诀窍。

相关问题