菜鸟教程小白 发表于 2022-12-13 09:35:05

ios - UITableView 的自定义加载动画


                                            <p><p>我正在尝试创建一个 UITableViewController,其中 tableview 上的加载动画应该使项目从侧面滑入。单元格的高度高于默认单元格,大约大 5-7 倍。我想要一个像 Google+ 加载动画这样的动画,可以在 android 上看到。 (卡片动画)。</p>

<p>我想是继承 TableView 类,重写一个加载方法,当它们被引入显示器时显示新项目,然后使用 Spring 动画同时旋转单元格,同时它的 x 和 y 值逐渐改变.</p>

<p>我在 <a href="https://stackoverflow.com/questions/11379614/can-you-do-custom-animations-for-uitableview-cell-inserts" rel="noreferrer noopener nofollow">this answer</a> 中尝试了此代码但并没有让它发挥作用。对该答案的详细说明和解释也将非常有帮助。代码:</p>

<pre><code>- (void)animate
{
[ enumerateObjectsUsingBlock:^(UITableViewCell *cell, NSUInteger idx, BOOL *stop) {
    ;
    [UIView animateWithDuration:1 animations:^{
      ;
    }];
}];
}
</code></pre>

<p>任何帮助将不胜感激!
埃里克</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这是(我相信)您所描述的功能齐全的示例。我打开了一个空的单 View 项目,删除了 Storyboard 中的 View 控件,然后简单地创建了一个 UITableViewController 并将其类设置为我在下面创建的 UITableViewController 子类的类。 </p>

<p>头文件中没有添加任何内容,所以我将其排除在外</p>

<pre><code>#import &#34;weeeTables.h&#34;

@interface weeeTables ()


@property (nonatomic, strong) NSMutableSet *shownIndexes;
@property (nonatomic, assign) CATransform3D initialTransform;

@end
</code></pre>

<p> shownIndexes 将包含已显示 View 的索引,因此我们不会重复向上滚动的动画。 initialTransform 属性是单元格初始状态的转换(在它出现在屏幕上之前你想要它的位置)</p>

<pre><code>@implementation weeeTables

- (void)viewDidLoad {
;

CGFloat rotationAngleDegrees = -30;
CGFloat rotationAngleRadians = rotationAngleDegrees * (M_PI/180);
CGPoint offsetPositioning = CGPointMake(-20, -20.0);

CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DRotate(transform, rotationAngleRadians, -50.0, 0.0, 1.0);
transform = CATransform3DTranslate(transform, offsetPositioning.x, offsetPositioning.y, -50.0);
_initialTransform = transform;

_shownIndexes = ;
}
</code></pre>

<p>在 ViewDidLoad 中,我们设置了初始变换的值,我对这些值进行了一些调整,并鼓励您这样做以获得您正在寻找的效果,但单元格从左下角开始动画目前。</p>

<pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 385;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = ;


return cell;
}
</code></pre>

<p>上面 block 中的所有内容对于 UITableViewController 子类来说都是非常典型的,并且与添加动画没有特别的关系,我只是设置了一些静态数字来添加大量要滚动的单元格,这里唯一的唯一值是单元格标识符,如您所见,它有一个经过深思熟虑的名称。</p>

<pre><code>- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{

if (!) {

    ;
    UIView *weeeeCell = ;

    weeeeCell.layer.transform = self.initialTransform;
    weeeeCell.layer.opacity = 0.8;

    [UIView animateWithDuration:.65 delay:0.0 usingSpringWithDamping:.85 initialSpringVelocity:.8 options:0 animations:^{
      weeeeCell.layer.transform = CATransform3DIdentity;
      weeeeCell.layer.opacity = 1;
    } completion:^(BOOL finished) {}];
}
}
@end
</code></pre>

<p>这是真正将所有内容组合在一起的人,首先我们检查我们要显示的单元格是否已经在显示索引列表中,如果不是,我们添加它,然后创建一个局部变量当前单元格内容 View 。</p>

<p>然后将该 contentView 上的转换设置为我们的初始转换(我们在 ViewDidLoad 中设置)。这会在用户可见之前将单元格移到我们的起始位置,然后我们将该变换动画化回身份变换。我也有不透明的地方,只是为了它。</p>

<p>希望对你有帮助!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableView 的自定义加载动画,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/26410645/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/26410645/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableView 的自定义加载动画