[英]Spinning UITableViewCells
我有一個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的相應引用出口,稱為“圓圈”
- (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?
最終,我最終得到了以下代碼:
- (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];
}
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.