简体   繁体   English

旋转UITableViewCells

[英]Spinning UITableViewCells

I have a UITableView loading its data from the Web, and while the data is loading there are few cells, to indicate the process. 我有一个UITableView从Web加载其数据,并且在加载数据时,只有很少的单元格指示该过程。 Each cell got an UIImageView inside which i want to be spinning constantly. 每个单元格都有一个UIImageView,我想不断旋转它。

Inside the UITableViewController i got this: 在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 has a *.xib file with a corresponding reference outlet to the UIImageView, called "circle" 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;
}

It seems to spin 2 times and then stops. 它似乎旋转2次然后停止。 In the console there are 6 "SPINNIN'" outputs, two for each cell, i suppose. 在控制台中,我想有6个“ SPINNIN'”输出,每个单元两个。

I'm very new to iOS-development, been tryin' to figure this out the entire evening. 我对iOS开发非常陌生,一直在努力解决整个晚上的问题。 Btw, what will happen to these cell objects when they will get replaced by cells with loaded data? 顺便说一句,当这些单元对象被已加载数据的单元替换时,会发生什么? Where do i call stopSpin then? 那我在哪里叫stopSpin?

Eventually i ended up with this 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];
                        }
                 }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM