IOS开发ios程序员

iOS下拉放大图片

2016-04-11  本文已影响853人  朱凯奇

闲来无事,模仿小恩爱写了个图片下拉放大的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;
    }

}


搞定,如有疑问,请留言

上一篇 下一篇

猜你喜欢

热点阅读