2014-03-29 6 views
5

Ich verblasse Zellen meines uitableview, während sie aus der Sicht hinausrollen, oder verblassen sie, während sie in Sicht scrollen. Das Problem, mit dem ich konfrontiert bin, ist, dass, wenn ich sehr schnell scrolle, die Zellen, die vollständig sichtbar sind, abgeblendet bleiben. Hier ist mein Code unten:ios uitableview blendet untere Zelle und obere Zelle aus, während Sie blättern

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    // Fades out top and bottom cells in table view as they leave the screen 
    NSArray *visibleCells = [self.tableView visibleCells]; 

    if (visibleCells != nil && [visibleCells count] != 0) {  // Don't do anything for empty table view 

     /* Get top and bottom cells */ 
     UITableViewCell *topCell = [visibleCells objectAtIndex:0]; 
     UITableViewCell *bottomCell = [visibleCells lastObject]; 

     /* Make sure other cells stay opaque */ 
     // Avoids issues with skipped method calls during rapid scrolling 
     for (UITableViewCell *cell in visibleCells) { 
      cell.contentView.alpha = 1.0; 
     } 

     /* Set necessary constants */ 
     NSInteger cellHeight = topCell.frame.size.height - 1; // -1 To allow for typical separator line height 
     NSInteger tableViewTopPosition = self.tableView.frame.origin.y; 
     NSInteger tableViewBottomPosition = self.tableView.frame.origin.y + self.tableView.frame.size.height; 

     /* Get content offset to set opacity */ 
     CGRect topCellPositionInTableView = [self.tableView rectForRowAtIndexPath:[self.tableView indexPathForCell:topCell]]; 
     CGRect bottomCellPositionInTableView = [self.tableView rectForRowAtIndexPath:[self.tableView indexPathForCell:bottomCell]]; 
     CGFloat topCellPosition = [self.tableView convertRect:topCellPositionInTableView toView:[self.tableView superview]].origin.y; 
     CGFloat bottomCellPosition = ([self.tableView convertRect:bottomCellPositionInTableView toView:[self.tableView superview]].origin.y + cellHeight); 

     /* Set opacity based on amount of cell that is outside of view */ 
     CGFloat modifier = 1.2;  /* Increases the speed of fading (1.0 for fully transparent when the cell is entirely off the screen, 
           2.0 for fully transparent when the cell is half off the screen, etc) */ 
     CGFloat topCellOpacity = (1.0f - ((tableViewTopPosition - topCellPosition)/cellHeight) * modifier); 
     CGFloat bottomCellOpacity = (1.0f - ((bottomCellPosition - tableViewBottomPosition)/cellHeight) * modifier); 

     /* Set cell opacity */ 
     if (topCell) { 
      topCell.alpha = topCellOpacity; 
     } 
     if (bottomCell) { 
      bottomCell.alpha = bottomCellOpacity; 
     } 
    } 
} 

Jede Idee, warum, wenn ich scrollen wirklich schnell die Zellen im Auge manchmal verdunkelt bleiben?

Antwort

14

Das Problem ist, dass das Scroll-Ereignis nicht immer genug häufig vorkommt, dass eine Zelle, die in Sicht scrollt, von dem verblassten Alpha zu einem Alpha von 1.0 wechselt, während es von Oben/Unten zu der Mitte scrollt. Aber wenn Sie sehr schnell scrollen, wird die scrollViewDidScroll nicht häufig genug aufgerufen, um sicherzustellen, dass dies der Fall ist. Und Ihre Schleife, die anscheinend versucht, das alpha zurückzusetzen, aktualisiert das alpha der falschen Ansicht (contentView eher als die Zelle selbst).

Sie können hier Abhilfe schaffen, indem Sie Ihre bestehende for Schleife ersetzt:

for (UITableViewCell *cell in visibleCells) { 
    cell.contentView.alpha = 1.0; 
} 

Mit

for (UITableViewCell *cell in visibleCells) { 
    cell.alpha = 1.0; 
} 

Oder beseitigen alternativ, dass die Schleife von dort und dann die Linien ersetzen, die die Alpha des Satzes obere und untere Zellen:

if (topCell) { 
    topCell.alpha = topCellOpacity; 
} 
if (bottomCell) { 
    bottomCell.alpha = bottomCellOpacity; 
} 

Mit:

for (UITableViewCell *cell in self.tableView.visibleCells) { 
    if (cell == topCell) { 
     cell.alpha = topCellOpacity; 
    } else if (cell == bottomCell) { 
     cell.alpha = bottomCellOpacity; 
    } else { 
     cell.alpha = 1.0; 
    } 
} 

By the way, eine andere Art und Weise einen ähnlichen Effekt zu erreichen, ist eine Verlaufsmaske auf das gesamte Tableview anzuwenden und das scrollViewDidScroll Verfahren zurückzuziehen. Der einzige Trick dabei ist, dass Sie die Farbverlaufsmaske nicht auf die Tabellenansicht selbst anwenden können (sonst wird der Farbverlauf mit der Tabellenansicht verschoben), sondern platzieren Sie die Tabellenansicht in einen Container UIView und wenden Sie dann die Maske darauf an:

- (void)viewDidAppear:(BOOL)animated 
{ 
    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = self.containerView.bounds; 
    gradient.colors = @[(id)[UIColor clearColor].CGColor, 
         (id)[UIColor whiteColor].CGColor, 
         (id)[UIColor whiteColor].CGColor, 
         (id)[UIColor clearColor].CGColor]; 
    gradient.locations = @[@0.0, @0.1, @0.9, @1.0]; 
    self.containerView.layer.mask = gradient; 
} 

Dies ist zwar ein etwas anderer Effekt, aber manchmal ist es wünschenswert. Es hängt nur davon ab, was Sie anstreben.

+0

das scheint das Problem nicht mehr zu verursachen ... aber ich bin verwirrt, wie die for-Schleife in meinem Code dies nicht behandelt. Wenn Sie schauen, habe ich diese for-Schleife in meinem Code oben: 'für (UITableViewCell * Zelle in visibleCells) { cell.contentView.alpha = 1.0; } ' –

+1

@AtulBhatia Oh, ich war mir nicht sicher, was Sie dort zu tun versuchten. Das liegt daran, dass Sie 'cell.contentView.alpha' und nicht' cell.alpha' einstellen. Wenn du abdunkelst, änderst du 'cell.alpha', also musst du das selbe machen, wenn du es dimmst. (Und Sie wollen definitiv 'cell.alpha' für den Fall verwenden, dass Sie jemals Enthüllungsindikatoren, Zellenschattierungen usw. verwenden). Ich habe meine Antwort aktualisiert. – Rob

+0

ah ja, dummer Fehler meinerseits. Danke, dass du mir geholfen hast! –

Verwandte Themen