2015-03-18 4 views
0

J'ai un UITableView chargement de ses données à partir du Web, et pendant le chargement des données il y a quelques cellules, pour indiquer le processus. Chaque cellule a un UIImageView à l'intérieur duquel je veux tourner constamment.Spinning UITableViewCells

A l'intérieur du UITableViewController j'ai obtenu ceci:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section 
{ 
    //there are three "default" cells 
    return self.datasource.count > 0 ? self.datasource.count : 3; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(self.datasource.count) 
    { 
    //return a cell with loaded data 
    } 
    else 
    { 
     LoadingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoadingIndicationCell"]; 

     return cell; 
    } 
} 

LoadingTableViewCell a un fichier * .xib avec une sortie de référence correspondant à la UIImageView, appelé "cercle"

- (void)awakeFromNib { 
    self.circle.alpha = .3f; 
    self.spinning = NO; 
    [self startSpin]; 
} 
- (void) spinWithOptions: (UIViewAnimationOptions) options { 
    NSLog(@"SPINNIN'"); 
    // this spin completes 360 degrees every 2 seconds 
    [UIView animateWithDuration: 0.5f 
          delay: 0.0f 
         options: options 
        animations: ^{ 
         _circle.transform =  CGAffineTransformRotate(_circle.transform, M_PI/2); 
        } 
        completion: ^(BOOL finished) { 
         if (finished) { 
          if (_spinning) { 
          [self spinWithOptions: UIViewAnimationOptionCurveLinear]; 
         } else if (options != UIViewAnimationOptionCurveEaseOut) { 
          [self spinWithOptions: UIViewAnimationOptionCurveEaseOut]; 
         } 
        } 
       }]; 
} 

- (void) startSpin { 
    if (!self.spinning) { 
     self.spinning = YES; 
     [self spinWithOptions: UIViewAnimationOptionCurveEaseIn]; 
    } 
} 

- (void) stopSpin { 
    self.spinning = NO; 
} 

Il semble tourner 2 fois et s'arrête. Dans la console il y a 6 sorties "SPINNIN", deux pour chaque cellule, je suppose.

Je suis très novice dans le développement iOS, je l'ai essayé pendant toute la soirée. Btw, qu'adviendra-t-il de ces objets cellulaires lorsqu'ils seront remplacés par des cellules avec des données chargées? Où puis-je appeler stopSpin alors?

+0

il semble que votre code ne possédent pas le problème. vous pouvez ajouter plus de journal et de point d'arrêt au bloc d'achèvement pour déboguer. Notez aussi parfois que le bloc d'achèvement appellera avec fini = NON. par exemple, quand la cellule est remplacée, elle sera supprimée en premier, cela arrêtera l'animation, et la fin du rappel sera terminée. NO – SolaWing

+0

@SolaWing merci pour l'indication sur le drapeau "fini". J'ai légèrement modifié la méthode spinWithOptions et cela fonctionne désormais. – Alexander

Répondre

0

Finalement, j'ai fini avec ce code:

- (void) spinWithOptions: (UIViewAnimationOptions) options { 
    if([self isHidden]) [self stopSpin]; //stop spinning when the cell got replaced 
    // this spin completes 360 degrees every 2 seconds 
    [UIView animateWithDuration: 1.0f 
         delay: 0.0f 
        options: options 
       animations: ^{ 
        _circle.transform = CGAffineTransformRotate(_circle.transform, M_PI/2); 
       } 
       completion: ^(BOOL finished) { 
         if (_spinning) { 
          [self spinWithOptions: UIViewAnimationOptionCurveLinear]; 
         } 
       }]; 
} 
+0

Qu'est-ce qui est différent, et pourquoi est-ce différent? –

+0

@MatthiasBauch J'ai cessé de compter sur le drapeau «fini» au bloc d'achèvement, le rendant ainsi infini. Mais quand une cellule est retirée de la tableview, sa méthode isHidden va retourner YES, et quand cela arrive, j'arrête la rotation. – Alexander