OGeek|极客世界-中国程序员成长平台

标题: ios - 如何实现像 Feedly 这样的动画,在 iOS Objective-c 中对新闻应用进行分类 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:10
标题: ios - 如何实现像 Feedly 这样的动画,在 iOS Objective-c 中对新闻应用进行分类

我想实现一个类似 insorts 和 feedly 新闻应用的动画。 我找到了快速版本 是通过uicollectionview布局定制实现的。

这是链接

Depth Page transform on iOS 我将 swift 转换为 objective-c ,但没有达到相同的效果。

这是我的代码

导入“DepthLayout.h”

@interface DepthLayout() {
    CGFloat contentWidth;
    CGFloat contentHeight;
    CGFloat yOffset;
    CGFloat maxAlpha;
    CGFloat minAlpha;
    CGFloat widthOffset;
    CGFloat heightOffset;
    NSMutableArray * cache;
}
@end

@implementation DepthLayout
#pragma mark - Lifecycle

- (id)init
{
    self = [super init];
    if (self) {
        [self setup];
    }

    return self;
}

- (id)initWithCoderNSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        [self setup];
    }

    return self;
}

- (void)setup
{

    yOffset=0;
    maxAlpha=1;
    minAlpha=0;
    widthOffset=35;
    heightOffset=35;
    cache=[[NSMutableArray alloc]init];

}
-(CGFloat)itemWidth{
    return CGRectGetWidth(self.collectionView.bounds);

}
-(CGFloat)itemHeight{
    return CGRectGetHeight(self.collectionView.bounds);

}
-(CGFloat)collectionViewHeight{
    return CGRectGetHeight(self.collectionView.bounds);

}
-(NSInteger)numberOfItems{
    return [self.collectionView numberOfItemsInSection:0];

}
-(CGFloat)dragOffset{
    return CGRectGetHeight(self.collectionView.bounds);

}
-(NSInteger)currentItemIndex{
    return MAX(0,(NSInteger)(self.collectionView.contentOffset.y/[self collectionViewHeight]));

}
-(CGFloat)nextItemBecomeCurrentPercentage{
    return (self.collectionView.contentOffset.y/[self collectionViewHeight])-(CGFloat)[self currentItemIndex];

}
#pragma mark - Layout

- (void)prepareLayout{
    [cache removeAllObjects];
    yOffset=0;
    for(NSInteger item=0;item<[self numberOfItems];item++){
       NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        UICollectionViewLayoutAttributes *attribute =
        [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        if ((indexPath.item == [self currentItemIndex ]+1) && (indexPath.item < [self numberOfItems])){
            attribute.alpha = minAlpha + MAX((maxAlpha-minAlpha) * [self nextItemBecomeCurrentPercentage], 0);
            CGFloat width = [self itemWidth] - widthOffset + (widthOffset * [self nextItemBecomeCurrentPercentage]);
            CGFloat height = [self itemWidth] - heightOffset + (heightOffset * [self nextItemBecomeCurrentPercentage]);
            CGFloat deltaWidth =  width/[self itemWidth];
            CGFloat deltaHeight = height/[self itemHeight ];
            attribute.frame = CGRectMake(0, yOffset, [self itemWidth],[self itemHeight]);
            attribute.transform = CGAffineTransformMakeScale(deltaWidth, deltaHeight);
            CGPoint center=self.collectionView.center;
            center.y=self.collectionView.center.y+self.collectionView.contentOffset.y;
            center.x=self.collectionView.center.x+self.collectionView.contentOffset.x;
            attribute.center = center;
            yOffset += [self collectionViewHeight];

        }else{
            attribute.frame = CGRectMake(0, yOffset, [self itemWidth],[self itemHeight]);
            CGPoint center=self.collectionView.center;
            center.y=self.collectionView.center.y+yOffset;
            center.x=self.collectionView.center.x;
            yOffset += [self collectionViewHeight];
        }
        [cache addObject:attribute];
    }

}

-(CGSize)collectionViewContentSize{
    contentWidth = CGRectGetWidth(self.collectionView.bounds);
    contentHeight = (CGFloat)[self numberOfItems] * CGRectGetHeight(self.collectionView.bounds);
    return CGSizeMake(contentWidth, contentHeight);
}
-(NSMutableArray *)layoutAttributesForElementsInRectCGRect)rect{


    NSMutableArray* layoutAttributes = [[NSMutableArray alloc]init];
    for (UICollectionViewLayoutAttributes * attribute in cache){
        if(CGRectIntersectsRect(attribute.frame, rect)){
            [layoutAttributes addObject:attribute];
        }
    }
    return layoutAttributes;
}
-(BOOL)shouldInvalidateLayoutForBoundsChangeCGRect)newBounds{
    return YES;
}
-(CGPoint)targetContentOffsetForProposedContentOffsetCGPoint)proposedContentOffset withScrollingVelocityCGPoint)velocity{
    NSInteger itemIndex = round(proposedContentOffset.y / ([self dragOffset]));
    CGFloat yOffsetTemp = itemIndex * CGRectGetHeight(self.collectionView.bounds);
    return CGPointMake( 0,  yOffsetTemp);

}
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPathNSIndexPath*)indexPath {

    UICollectionViewLayoutAttributes *attr=[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    return attr;
}
@end

What is the problem ?



Best Answer-推荐答案


无需将 swift 转换为 objective-c 。

我在我的 Objective C 项目中直接使用 Swift 文件 满足我的要求。

我从这里使用了 UltravisualLayout:Depth Page transform on iOS

感谢 swift 和 Objective C 之间的桥接;

关于ios - 如何实现像 Feedly 这样的动画,在 iOS Objective-c 中对新闻应用进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40842600/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4