菜鸟教程小白 发表于 2022-12-11 18:10:32

ios - 如何实现像 Feedly 这样的动画,在 iOS Objective-c 中对新闻应用进行分类


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

<p>这是链接</p>

<p> <a href="https://stackoverflow.com/questions/35717774/depth-page-transform-on-ios" rel="noreferrer noopener nofollow">Depth Page transform on iOS</a>
我将 swift 转换为 objective-c ,但没有达到相同的效果。</p>

<p>这是我的代码</p>

<h1>导入“DepthLayout.h”</h1>

<pre><code>@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 = ;
    if (self) {
      ;
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = ;
    if (self) {
      ;
    }

    return self;
}

- (void)setup
{

    yOffset=0;
    maxAlpha=1;
    minAlpha=0;
    widthOffset=35;
    heightOffset=35;
    cache=[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 ;

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

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

}
-(CGFloat)nextItemBecomeCurrentPercentage{
    return (self.collectionView.contentOffset.y/)-(CGFloat);

}
#pragma mark - Layout

- (void)prepareLayout{
    ;
    yOffset=0;
    for(NSInteger item=0;item&lt;;item++){
       NSIndexPath *indexPath = ;
      UICollectionViewLayoutAttributes *attribute =
      ;
      if ((indexPath.item == +1) &amp;&amp; (indexPath.item &lt; )){
            attribute.alpha = minAlpha + MAX((maxAlpha-minAlpha) * , 0);
            CGFloat width = - widthOffset + (widthOffset * );
            CGFloat height = - heightOffset + (heightOffset * );
            CGFloat deltaWidth =width/;
            CGFloat deltaHeight = height/;
            attribute.frame = CGRectMake(0, yOffset, ,);
            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 += ;

      }else{
            attribute.frame = CGRectMake(0, yOffset, ,);
            CGPoint center=self.collectionView.center;
            center.y=self.collectionView.center.y+yOffset;
            center.x=self.collectionView.center.x;
            yOffset += ;
      }
      ;
    }

}

-(CGSize)collectionViewContentSize{
    contentWidth = CGRectGetWidth(self.collectionView.bounds);
    contentHeight = (CGFloat) * CGRectGetHeight(self.collectionView.bounds);
    return CGSizeMake(contentWidth, contentHeight);
}
-(NSMutableArray *)layoutAttributesForElementsInRect:(CGRect)rect{


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

}
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath*)indexPath {

    UICollectionViewLayoutAttributes *attr=;
    return attr;
}
@end
</code></pre>

<blockquote>
<p>What is the problem ?</p>
</blockquote></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>无需将 swift 转换为 objective-c 。</p>

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

<p>我从这里使用了 UltravisualLayout:<a href="https://stackoverflow.com/questions/35717774/depth-page-transform-on-ios" rel="noreferrer noopener nofollow">Depth Page transform on iOS </a> </p>

<p>感谢 swift 和 Objective C 之间的桥接;</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何实现像 Feedly 这样的动画,在 iOS Objective-c 中对新闻应用进行分类,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40842600/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40842600/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何实现像 Feedly 这样的动画,在 iOS Objective-c 中对新闻应用进行分类