像iOS7天气应用程序中的单元格背景视差 [英] Cells background parallax like in iOS7 weather app
问题描述
如果您查看iPhone上的iOS 7天气应用程序,您会看到在滚动时,UITableViewCells的背景也会滚动(每个都独立于其余部分)。我想弄清楚它是如何完成的。有什么想法吗?
If you take a look at the iOS 7 Weather app on iPhone you will see that as you scroll, the background of the UITableViewCells scroll too(each independently of the rest). I'm trying to figure out how it's done. Any ideas?
答案是
-(void)scrollTable:(UIScrollView *)scrollView
{
float offset = _tableViewNew.contentOffset.y / _tableViewNew.frame.size.height;
for (int i = 0; i <[cellTitle count]; i++) {
UITableViewCell *cell = [_tableViewNew cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
CGRect frame = CGRectMake(cell.backgroundView.frame.origin.x, offset * 50, cell.backgroundView.frame.size.width, cell.backgroundView.frame.size.height);
cell.backgroundView.frame = frame;
}
}
推荐答案
我不会称之为视差效应。它所做的就是设置所有单元格背景相对于滚动位置的起始位置。假设您的单元格背景图像高150个单位,总可滚动高度为400个单位。
I wouldn't call that a parallax effect. All it does is to set the starting position of all the cell backgrounds relative to the scroll position. Say your cell background images are 150 units high and your total scrollable height is 400 units.
滚动的百分比为:
relative scroll offset = tableView.contentOffset.Y / 400.
只要表视图滚动(通过实现 UIScrollViewDelegate的
scrollViewDidScroll:)
,就可以调整垂直位置您的单元格背景:
Whenever the table view scrolls (you get that by implementing UIScrollViewDelegate's
scrollViewDidScroll:)
, adjust the vertical position of your cell backgrounds:
cell background offset = relative scroll offset * 150
为了实现 scrollViewDidScroll:
,你需要设置 UITableView的
属性委托。作为 UITableView
子类 UIScrollView
,此属性需要 UIScrollViewDelegate
实例。在您的控制器中,实现 scrollViewDidScroll:
并将delegate属性设置为您的控制器。
In order to implement scrollViewDidScroll:
you need to set the UITableView's
property delegate. As UITableView
subclasses UIScrollView
, this property expected a UIScrollViewDelegate
instance. In your controller, implement scrollViewDidScroll:
and set the delegate property to your controller.
这篇关于像iOS7天气应用程序中的单元格背景视差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!