iOS TableView头视图根据偏移量下拉缩放的效果实现

在做项目时,一些TableView的地方会使用到下拉TableView让HeardView头视图随其偏移量的变化而变化,之前做过一次但没记录下来。现在做的时候总是遇到一些问题,比如下拉时是以原点向左右两边放大,这个只是效果问题。还有就是看到的视图确实变大了,但是会盖到下面的TableViewCell。开始以为是加在里面的视图变大,而TableViewHeardView没变大,我NSLog打印了一下两个视图的高度,发现是一样的,找不到问题了。

在网上找了一些相关的例子,各有各的做法,有的是根据偏移量来设置TableViewHeardView的缩放,有的是设置里面被我们设置为TableViewHeardView的那个视图的大小,都试了一下感觉还是不行,感觉缩放效果是有了的,但是拉大一点就会把下面的cell盖住。

之后自己摸索,调整后,我是用偏移量来控制里面的imageView的大小,imageView是我设置的HeardView的子视图,而heardView和TableViewheard的大小不会根据imageView变化,只是坐标变了,其实这是一种错觉,真实的效果是imageView坐标向左上方偏移,大小也变化,而heardView和TableViewheard没变大,但imageView变大的部分盖住他们的上方,让我们觉得heardView和TableViewheard也变大了,可以参考文章下面的逻辑图

先看一看效果吧,一张是没下拉时的效果,一张是下拉时的效果。


        


可以看到imageView向四周变大了,而头像和昵称只是垂直方向位置变了,而大小没变。

这里用到的控件入下图




#define ScreenWidth [UIScreen mainScreen].bounds.size.width

在viewDidLoad里创建一下,

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 iOS UITableView 的代码实现示例: 1. 首先,在你的视图控制器中添加 UITableView 属性: ``` @interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView; @end ``` 2. 在 viewDidLoad 方法中初始化 UITableView: ``` - (void)viewDidLoad { [super viewDidLoad]; // 初始化 UITableView self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; } ``` 3. 实现 UITableViewDataSource 协议中的方法: ``` // 返回 UITableView 中的 section 数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // 返回 UITableView 中某个 section 中的 row 数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } // 返回 UITableView 中某个 indexPath 的 cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MyCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = [NSString stringWithFormat:@"Cell %ld", (long)indexPath.row]; return cell; } ``` 4. 实现 UITableViewDelegate 协议中的方法,比如设置 cell 的高度: ``` - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44.0f; } ``` 以上就是一个简单的 UITableView 的代码实现示例。需要注意的是,UITableView 必须指定 delegate 和 dataSource,而且需要实现 UITableViewDataSource 和 UITableViewDelegate 协议中的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值