簡體   English   中英

自定義UITableView單元格動畫

[英]Custom UITableView Cell Animation

我正在嘗試制作自定義單元格highlighted動畫...但是它不起作用。 有什么想法可以使.alpa改變動畫嗎?

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"myID";
    TableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    cell.selectedImg.frame = cell.contentView.frame;
    cell.selectedImg.image = [ViewController imageFromColor:[UIColor colorWithRed:0.55 green:0.95 blue:0.68 alpha:0.0]];
    cell.selectedImg.hidden = NO;

    return cell;
}

TableViewCustomCell.m

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted) {
        [UIView animateWithDuration:0.2 animations:^(void) {
            self.selectedImg.alpha = 0.5;}];
    }
    else {
         [UIView animateWithDuration:0.2 animations:^(void) {
            self.selectedImg.alpha = 0.0;}];
    }
}

PS我將有很多UITableViews ,它們都將具有相同的單元格樣式,因此我是否應該將cell.selectedImg代碼放在TableViewCustomCell.m中? 如果是,正確的地方在哪里?

嘗試替換中的代碼

  • (void)setHighlighted:(BOOL)高亮動畫:(BOOL)動畫

TableViewCell.m中使用以下代碼:

[UIView animateWithDuration:0.2
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         if (highlighted) {
                           self.selectedImg.alpha = 0.5;
                         }
                         else {
                           self.selectedImg.alpha = 0;
                         }
                     }
                     completion:^(BOOL finished){
                         if (highlighted) {
                          self.selectedImg.alpha = 0;
                         }
                         else {
                          self.selectedImg.alpha = 0.5;
                         }
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM