Ios 获取每个UITableViewCell屏幕上的当前y值位置
我有一个带有自定义类的Ios 获取每个UITableViewCell屏幕上的当前y值位置,ios,objective-c,uitableview,transparent,alpha,Ios,Objective C,Uitableview,Transparent,Alpha,我有一个带有自定义类的UITableViewController,用于我的UITableViewCell。我在这个UITableViewCell上有一个UIImageView和一个UILabel 我希望UITableViewCell上的内容在屏幕上高于某个y值时变得更加半透明/透明,这样你就会得到一种“淡入淡出效果”,类似于Rdio应用程序: UIImageView和UILabel为1.0“> MyUIViewController.h @property (strong, nonatomic)
UITableViewController
,用于我的UITableViewCell
。我在这个UITableViewCell
上有一个UIImageView
和一个UILabel
我希望UITableViewCell
上的内容在屏幕上高于某个y值时变得更加半透明/透明,这样你就会得到一种“淡入淡出效果”,类似于Rdio应用程序:
UIImageView和UILabel为1.0“>
MyUIViewController.h
@property (strong, nonatomic) IBOutlet UITableView *tableview;
MyUIViewController.m
@synthesize tableview;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSArray *listOfVisibleCells = tableview.visibleCells;
for (int i=0; i<[listOfVisibleCells count]; i++) {
MTTableViewCell *cell = [listOfVisibleCells objectAtIndex:i];
/* Smooth fade out */
if (scrollView.contentOffset.y > (cell.frame.origin.y + 10)) {
[cell.companyImage setAlpha:0.7];
}
if (scrollView.contentOffset.y > (cell.frame.origin.y + 20)) {
[cell.companyImage setAlpha:0.5];
}
if (scrollView.contentOffset.y > (cell.frame.origin.y + 30)) {
[cell.companyImage setAlpha:0.3];
}
if (scrollView.contentOffset.y > (cell.frame.origin.y + 40)) {
[cell.companyImage setAlpha:0.1];
}
/* Fade in */
if (scrollView.contentOffset.y < (cell.frame.origin.y + 10)) {
[cell.companyImage setAlpha:1.0];
}
}
}
@synthesistableview;
-(无效)scrollViewDidScroll:(UIScrollView*)scrollView{
NSArray*listOfVisibleCells=tableview.visibleCells;
对于(int i=0;i(cell.frame.origin.y+10)){
[cell.companyImage setAlpha:0.7];
}
if(scrollView.contentOffset.y>(cell.frame.origin.y+20)){
[cell.companyImage setAlpha:0.5];
}
if(scrollView.contentOffset.y>(cell.frame.origin.y+30)){
[cell.companyImage setAlpha:0.3];
}
if(scrollView.contentOffset.y>(cell.frame.origin.y+40)){
[cell.companyImage setAlpha:0.1];
}
/*淡入*/
if(scrollView.contentOffset.y<(cell.frame.origin.y+10)){
[cell.companyImage setAlpha:1.0];
}
}
}
我想您可以使用UITableView委托方法来处理这些问题
UITableViewDelegate:
告诉代理表视图将要为特定行绘制单元格
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
UIScrollViewDelegate:
当用户在接收器内滚动内容视图时通知代理。您还可以知道内容偏移量
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
按照这些方法,可以检测当前UITableViewCell或偏移.y值
希望这能对您有所帮助。这一行:
if(scrollView.contentOffset.y>((i*90)+30)){
应该是:
如果(scrollView.contentOffset.y>(cell.frame.origin.y+30)){
即使您已收到所需的答案,我也会添加一种替代方法来完成此操作
在scrollViewDidScroll:
方法中:
if ([myTableView indexPathsForVisibleRows].count) {
NSIndexPath* firstCellIndexPath = [myTableView indexPathsForVisibleRows].firstObject;
CGRect cellRect = [myTableView rectForRowAtIndexPath:firstCellIndexPath];//frame of cell with respect to the table content
CGFloat yOriginForTopCell = [myTableView convertRect:cellRect toView:self.view].origin.y;//get y-origin after converting the co-ordinates to self.view
NSLog(@"%f", yOriginForTopCell);
}
在这里,无论滚动状态如何,您都可以获得表格中第一个可见单元格的y原点。一旦获得原点,您可以以任何方式使用它。优点是,您不需要进行任何计算。只需为顶部边缘选择一个值(淡入必须开始的位置)最好是您的顶栏/导航栏y原点+高度,这是一个常量值
您可以动态设置alpha,并通过以下方式逐渐淡入淡出:
customCell* yourCell = (customCell*)[myTableView cellForRowAtIndexPath:firstCellIndexPath];
yourCell.yourView.alpha = yOriginForTopCell/<CONSTANT_VALUE>;//yourCell = `MTTableViewCell` | yourView = `companyImage`
customCell*yourCell=(customCell*)[myTableView cellForRowAtIndexPath:firstCellIndexPath];
yourCell.yourView.alpha=yOriginForTopCell///yourCell=`MTTableViewCell`;yourView=`companyImage`
这里,您的常量_值最好为64px(如果使用本机UINavigationBar)或yourTopBar.frame.origin.y+yourTopBar.frame.size.height
(如果在顶部使用自定义视图)
尝试此解决方案一次。请在scrollViewDidScroll方法中发布您迄今为止尝试过的代码,使用tableView上的
visibleCells
方法获取屏幕上当前可见的所有单元格。这些单元格中的每一个都将通过cell.frame.origin.y
具有特定的y偏移量,您可以使用该偏移量进行淡入效果。