0

無限に回転する UIImageView の形のカスタム アクティビティ インジケーターがあります。この驚くべきアクティビティ インジケータは、表のセル内に配置されています。

アニメーションは次の方法で実現されます。

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
animation.duration = 1.0f;
animation.repeatCount = INFINITY;

[self.layer addAnimation:animation forKey:@"SpinAnimation"];



最初は、それは美しく機能します...しかし、セルがテーブルの境界からスクロールされると、アニメーションが停止し、リセットする方法がわかりません... /:

助けてください...
よろしくお願いします... :)

4

2 に答える 2

1

このアニメーションは、対応するセルを再利用すると削除されるようです。少なくとも、animationDidStopデキューされたセルを から返したときにデリゲートのコールバックを取得しましたcellForItemAtIndexPath

私が思い付くことができる最も簡単な回避策:

テーブル ビューのデータ ソース:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if(cell && [cell conformsToProtocol:@protocol(CustomCellProtocol)])
    {
        [(id<CustomCellProtocol>)cell beforeDisplayed];
    }

    return cell;
}

あなたのセル:

static NSString* const kSpinAnimationKey = @"SpinAnimation";

@interface CustomCell () <CustomCellProtocol>
@end

@implementation CustomCell

-(void)startSpinAnimation
{
    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
    animation.duration = 1.0f;
    animation.repeatCount = INFINITY;

    [self.layer addAnimation:animation forKey:kSpinAnimationKey];
}

-(void)beforeDisplayed
{
    UIView* animatedView = self.contentView;
    if(![animatedView.layer animationForKey:kSpinAnimationKey])
    {
        [self startSpinAnimation];
    }
}
于 2014-04-29T19:11:23.307 に答える