UITableView 视觉差效果

前几天研究了一下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];

以上就是我对视觉差效果的理解,希望对大家有所帮助,如果有不对的地方或者更好的方法希望大家多多指教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值