Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在滚动时淡入表格视图顶部单元格的内容?_Iphone_Uitableview - Fatal编程技术网

Iphone 如何在滚动时淡入表格视图顶部单元格的内容?

Iphone 如何在滚动时淡入表格视图顶部单元格的内容?,iphone,uitableview,Iphone,Uitableview,大家好,我需要实现此功能。我需要在单元格内容消失时淡出单元格内容,我的意思是当它到达表视图顶部时。我不知道如何做到这一点。请帮助我。我尝试使用scrollview的方法,但不知道如何做到这一点 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 您可以创建渐变图像,并将其放置在滚动视图顶部上方。如果您希望“淡入淡出”仅在滚动期间可见,您可以在scrollViewBegin/DiEndScrollingAnimation上设置淡入淡出图

大家好,我需要实现此功能。我需要在单元格内容消失时淡出单元格内容,我的意思是当它到达表视图顶部时。我不知道如何做到这一点。请帮助我。我尝试使用scrollview的方法,但不知道如何做到这一点

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 

您可以创建渐变图像,并将其放置在滚动视图顶部上方。如果您希望“淡入淡出”仅在滚动期间可见,您可以在scrollViewBegin/DiEndScrollingAnimation上设置淡入淡出图像的动画,或将其与scrollViewDidScroll结合使用,您可以决定何时淡出(即当您滚动到顶部时).

我希望我能正确理解您的问题:您需要根据单元格在屏幕上的显示量来淡出单元格的内容吗

我不知道表中有哪些单元格,但如果它们的高度相同,可以这样做:

// Let's say you have a variable called cellHeight
int cellHeight = 80;

// Get the location of the top of the table
int topPosition = (int)tableView.cotentOffset.y;

// Get the index of the cell
int cellIndex = topPosition  / cellHeight;  

// Determine how much the cell is outside the view
float opacity = 1.0f - ((topPosition  % cellHeight) / cellHeight);
现在可以使用
不透明度
控制透明度级别。0.0f表示完全在视图外,1.0f表示完全在视图内

要获取需要更改的实际UITableViewCell,请使用

- (NSArray *)indexPathsForVisibleRows

然后搜索包含
cellIndex

的单元格。对于任何想要它的人,这里有一些可随时复制/粘贴的代码,可以完全工作:

最好的部分:这段代码只依赖于一个外部属性:
self.tableView
,所以只需确保已经设置好,您就可以开始了

只需将此方法粘贴到视图控制器的某个位置:

#pragma mark - Scroll View Delegate Methods

- (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 = 2.5;     /* 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.contentView.alpha = topCellOpacity;
        }
        if (bottomCell) {
            bottomCell.contentView.alpha = bottomCellOpacity;
        }
    }
}
别忘了将
添加到类的.h'文件中

您可能还希望在
viewdiload
方法中的某个地方调用此方法,如下所示:
[self-scrollviewdilscroll:self.tableView]这样底部单元格将开始褪色(因为它通常被表格视图边缘切断)

提示:将表视图分隔符样式设置为“无”(
self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
),或者创建自己的分隔符作为图像,并将其作为子视图添加到单元格中,这样就不会出现分隔符消失而不褪色的不良效果


适用于小于全屏的表格视图。

以下是Fateh Khalsa在Swift中的解决方案

func scrollViewDidScroll(scrollView: UIScrollView)
{
    let visibleCells = tableView.visibleCells

    if visibleCells.count == 0 {
        return
    }

    guard let bottomCell = visibleCells.last else {
        return
    }

    guard let topCell = visibleCells.first else {
        return
    }

    for cell in visibleCells {
        cell.contentView.alpha = 1.0
    }

    let cellHeight = topCell.frame.size.height - 1
    let tableViewTopPosition = tableView.frame.origin.y
    let tableViewBottomPosition = tableView.frame.origin.y + tableView.frame.size.height

    let topCellPositionInTableView = tableView.rectForRowAtIndexPath(tableView.indexPathForCell(topCell)!)
    let bottomCellPositionInTableView = tableView.rectForRowAtIndexPath(tableView.indexPathForCell(bottomCell)!)
    let topCellPosition = tableView.convertRect(topCellPositionInTableView, toView: tableView.superview).origin.y
    let bottomCellPosition = tableView.convertRect(bottomCellPositionInTableView, toView: tableView.superview).origin.y + cellHeight

    let modifier: CGFloat = 2.5
    let topCellOpacity = 1.0 - ((tableViewTopPosition - topCellPosition) / cellHeight) * modifier
    let bottomCellOpacity = 1.0 - ((bottomCellPosition - tableViewBottomPosition) / cellHeight) * modifier

    topCell.contentView.alpha = topCellOpacity
    bottomCell.contentView.alpha = bottomCellOpacity
}

@WrightsCs可能重复,而不是重复,首先正确阅读我的问题,我不需要只淡出视图。我知道怎么做,但我想在不可见时淡出单元格内容,如果你知道的话,那么请帮助。干杯这不是我的问题,我需要淡化单元格的内容,该内容将在表格视图的顶部消失。我将在此处放置一个图像,只要看到…..我需要找出哪个单元格位于顶部如何知道….这样我就可以将其淡化。@Andrei您几乎是对的,但我需要使用哪种方法,你能帮我更多的忙吗?我是这样用的,int cellHeight=50;//获取表顶部的位置int-topPosition=(int)translationTableView.contentOffset.y;//获取单元格的索引int cellIndex=topPosition/cellHeight;//确定单元格在视图浮动不透明度之外的程度=1.0f-((topPosition%cellHeight)/cellHeight);NSArray*索引=[translationTableView indexPathsForVisibleRows];对于(索引中的indexpath){if(cellIndex){cell.contentView.alpha=1.0;}}}但是它会褪色很多行,请帮助我了解如何做到这一点…..我想你可以把它放在
-(void)scrollViewDidScroll:(UIScrollView*)scrollView
@Andrei我是用同样的方法做的,但它会使许多单元格褪色,而不是所需的单元格。请帮助我这样做,请看图片。@Andrei看到了我所给出的逻辑,您告诉我这是正确的,还是需要做一些更改…这确实有效…+1…但我以前已经找到了答案…谢谢您的评论,因为它也可以帮助其他人…效果很好!我将保存这个小片段。是否也要淡出表格单元格分隔线?是的。将分隔线作为子视图添加到单元格的contentView中。这段代码的工作原理是淡出contentView的所有子视图,因此不属于该子视图的任何内容(即默认的tableView分隔符)都不会淡出。解决此问题的一种简单方法是删除默认分隔符(self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;),然后在GIMP或Photoshop之类的程序中创建一个图像,即您希望看到的分隔线的确切宽度和高度,并使用UIImageView将该图像添加为单元格内容视图的子视图。