我有一个UITableView从Web加载其数据,并且在加载数据时,几个单元格用于指示进程。每个单元格都有一个UIImageView,我想在其中不断旋转。
在UITableViewController中我得到了这个:
- (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有一个* .xib文件,带有UIImageView的相应参考出口,称为“circle”
- (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;
}
似乎旋转2次然后停止。在控制台中有6个“SPINNIN”输出,我想每个单元有两个。
我对iOS开发非常陌生,一直试着在整个晚上搞清楚这一点。顺便问一下,当这些单元格被加载数据的单元格取代时会发生什么?那我在哪里打电话给stopSpin?
答案 0 :(得分:0)
最终我最终得到了这段代码:
- (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];
}
}];
}