您的位置:首页 > 产品设计 > UI/UE

UITableView 视觉差效果

2015-12-23 19:13 471 查看
前几天研究了一下iOS的视觉差效果,主要实现是通过UITableView实现效果,当然UICollectionView、UIScrollView都可以实现视觉差效果,废话少说直接上代码。。。

首先在头文件中定义

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
UITableView      *_tableView;
NSArray          *_imageArray;
}
@end


在.m文件中实现UITableView,这个大家都会,在这里不多做讲解。

在这里只要说一下我对视觉差效果的理解,当UITableView在滚动的时候,通过UITableView滚动的距离计算出每个cell中图片移动的距离。UITableView继承UIScrollView,在UIScrollViewDelegate中,通过方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView得到滚动位置的变化,具体方法如下:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//下面的方法是我在网上查询得到方法
for (TableViewCell *cell in [_tableView visibleCells]) {
[cell cellOnTableView:_tableView didScrollOnView:self.view];
}
*******************这两种实现方式都需要在自定义cell中实现相应的方法********************
//一下是我的计算方法,显示效果略有不同
for (TableViewCell *cell in [_tableView visibleCells]) {
CGFloat yOffset = ((scrollView.contentOffset.y - cell.frame.origin.y) / IMAGE_HEIGHT) * IMAGE_OFFSET_SPEED;
if (yOffset >= 0) {
yOffset = -yOffset;
}
[cell updateTableShowImageFrame:yOffset];
}
}


这是我在网上查询得到的方法,在自定义cell中实现

- (void)cellOnTableView:(UITableView )tableView didScrollOnView:(UIView )view

{

CGRect rectInSuperview = [tableView convertRect:self.frame toView:view];

float distanceFromCenter = CGRectGetHeight(view.frame)/2 - CGRectGetMinY(rectInSuperview);

float difference = CGRectGetHeight(_showImage.frame) - CGRectGetHeight(self.frame);

float move = (distanceFromCenter / CGRectGetHeight(view.frame)) * difference;

CGRect imageRect = _showImage.frame;
imageRect.origin.y = -(difference/2)+move;
_showImage.frame = imageRect;
}


一下是我自己的方法

-(void)updateTableShowImageFrame:(CGFloat)y
{
_showImage.frame = CGRectMake(0, y, 375, 520);
}


该方法在效果为了在首次滚动时避免闪屏需要在初始化UITableView后实现一下方法

#define IMAGE_HEIGHT 200

#define IMAGE_OFFSET_SPEED 25

[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];


以上就是我对视觉差效果的理解,希望对大家有所帮助,如果有不对的地方或者更好的方法希望大家多多指教。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  界面 ios uitableview