閑來無事曹傀,模仿小恩愛寫了個圖片下拉放大的demo。
效果如下:
具體實現特別簡單,主要實現代碼如下:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(kWidth/3.75, kHeight/6.67);
layout.sectionInset = UIEdgeInsetsMake(20, 20, 0.33*kWidth, 20);
self.menuCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
self.menuCollectionView.backgroundColor = [UIColor whiteColor];
self.menuCollectionView.delegate = self;
self.menuCollectionView.dataSource = self;
self.menuCollectionView.contentInset = UIEdgeInsetsMake(250, 0, 0, 0);
self.menuCollectionView.alwaysBounceVertical = YES;
[self.view addSubview:self.menuCollectionView];
self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -250, kWidth, 250)];
self.backImageView.image = [UIImage imageNamed:@"1.jpg"];
self.backImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.menuCollectionView addSubview:self.backImageView];
下拉時實現放大方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat contentY = scrollView.contentOffset.y;
if (contentY < -250) {
CGRect frame = self.backImageView.frame;
frame.origin.y = contentY;
frame.size.height = -contentY;
self.backImageView.frame = frame;
}
}
搞定,如有疑問,請留言