Liang's Blog

Dont worry,be happy.

CircleLayout

| Comments

今天继续看了下CircleLayout布局。

1
2
3
4
5
6
7
8
- (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),
                                    _center.y + _radius * sinf(2 * path.item * M_PI / _cellCount));
    return attributes;
}

用三角函数计算求得当前item在圆环上得位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
    attributes.alpha = 0.0;
    attributes.center = CGPointMake(_center.x, _center.y);
    return attributes;
}

- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
    attributes.alpha = 0.0;
    attributes.center = CGPointMake(_center.x, _center.y);
    attributes.transform3D = CATransform3DMakeScale(0.1, 0.1, 1.0);
    return attributes;
}
1
2
3
4
5
6
7
8
- (void)performBatchUpdates:(void (^)(void))updates completion:(void (^)(BOOL finished))completion;该方法调用的时候会触发上边两个动画方法

注意,现在这两个动画方法已经改掉了api名称,由下边两个替代

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath{
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath{
}

原文链接 http://www.onevcat.com/2012/08/advanced-collection-view/

NS_DEPRECATED_IOS

| Comments

每一个新版本iOS出现之后,开发者会发现有一些方法会被NS_DEPRECATED_IOS(xx,xx).这个宏定义是什么意思呢?

1
前面一个xx表明了这个方法被引入时的iOS版本,后面一个表明它被废弃时的iOS版本。被废弃并不是指这个方法就不存在了,只是意味着我们应当开始考虑将相关代码迁移到新的API上去了。

所以可以继续使用。

原文链接 http://codingobjc.com/blog/2014/02/11/ni-xu-yao-zhi-dao-de-suo-you-guan-yu-ioshe-os-xyi-qi-yong-de-apide-shi-er/

CollectionView Details

| Comments

今天,真热啊。

看了看苹果官方工程LineLayout,记录下这段代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

    -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray* array = [super layoutAttributesForElementsInRect:rect];
    CGRect visibleRect;
    visibleRect.origin = self.collectionView.contentOffset;
    visibleRect.size = self.collectionView.bounds.size;
    
    for (UICollectionViewLayoutAttributes* attributes in array) {
        if (CGRectIntersectsRect(attributes.frame, rect)) {
            CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;
            CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
            if (ABS(distance) < ACTIVE_DISTANCE) {
                CGFloat zoom = 1 + ZOOM_FACTOR*(1 - ABS(normalizedDistance));
                attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);
                attributes.zIndex = 1;
            }
        }
    }
    return array;
}

对方法的看法

1
方法调用,通过判断当前item和collectionview可视区域两个的中心间距同item的宽度来进行对比,以决定是否进行transform3D变换。变幻的时候添加了一个比例因子ZOOM_FACTOR,使缩放不会太夸张。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (CGPoint)targetContentOffsetForProposedContentOffset: (CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
    
    //proposedContentOffset是没有对齐到网格时本来应该停下的位置
    CGFloat offsetAdjustment = MAXFLOAT;
    CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
    CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    NSArray* array = [super layoutAttributesForElementsInRect:targetRect];
    
    //对当前屏幕中的UICollectionViewLayoutAttributes逐个与屏幕中心进行比较,找出最接近中心的一个
    for (UICollectionViewLayoutAttributes* layoutAttributes in array) {
        CGFloat itemHorizontalCenter = layoutAttributes.center.x;
        if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
            offsetAdjustment = itemHorizontalCenter - horizontalCenter;
        }
    }
    return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}

对方法的看法

1
2
与上个方法一样,需要求得可是区域的cgrect,通过该参数求出所有的UICollectionViewLayoutAttributes,然后用每个参数里的center和中心点求差比较,比较的算法就是如果当前差数比上一个差的绝对值小就记录下来。
最后返回的应该停下来的点,y不动,x加的当前记录的差值。

对UICollectionViewLayoutAttributes的看法

1
UICollectionViewLayoutAttributes记录了一个元素所有与布局相关联的参数,仅此而已。

原文链接:http://www.onevcat.com/2012/08/advanced-collection-view/

Xcode5安装老版本模拟器

| Comments

最近刚刚更新老了Xcode5.1.1,发现只有系统自带的iOS7.1模拟器,对于调试以前的版本,还需要安装老版本的模拟器。系统提供了一种从preference下载的方式,但是模拟器一般都会很大,非常的费劲。于是上网搜到了一种比较快捷的方式。一共3步

1.从老版本Applications/Xcode.app/Contents/Developer/Platforms /iPhoneOS.platform/Developer/SDKs 拷贝Base Sdk到新版本Xcode对应目录下。

2./Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs 对这个路径下进行如上的一次拷贝。

3.关掉Xcode。重新打开系统会自动提示有新的包加入。按照提示做即可。

原文链接:http://blog.csdn.net/kenera/article/details/12513287

About This Blog

| Comments

关于博客 以前就有写博客的,博客地址是http://yupeiliang110.blog.163.com 欢迎大家前来学习交流。现在发现了很多人都用这个octopress 写技术博客,觉得挺好玩的,想试试,经过一天半时间,终于可以写东西了,窃喜,接下来继续改进。

520

| Comments

Dear Cherry,

its 520's day, so be happy. thanks for your supports. as you look this ,it seems that i have finish the blog. still i have to do some other works. anyhow , be happy.