TableView下拉之視圖放大及圖片展示

下面是動(dòng)畫效果

動(dòng)畫.gif

話不多說,直接上代碼


視圖的創(chuàng)建

    //UITableView頂部的ImageView
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, V_Width, V_height/3)];
    self.imageView.image = [UIImage imageNamed:@"3"];
    //設(shè)置imageView的填充模式
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createAllImageView)];
    [self.imageView addGestureRecognizer:tap];
    
    //UITableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, V_Width, V_height) style:UITableViewStylePlain];
    //設(shè)置tableView的內(nèi)邊距
    self.tableView.contentInset = UIEdgeInsetsMake(V_height/3, 0, 0, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];

    [self.view addSubview:self.imageView];

拖動(dòng)TableView時(shí)的事件

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGPoint point = scrollView.contentOffset;
    //下拉圖片放大
    if (point.y < -V_height/3 && point.y >= -V_height/3-60) {
        CGRect rect = self.imageView.frame;
        rect.origin.y = 0;
        rect.size.height = -point.y;
        self.imageView.frame = rect;
    }
    //上拉圖片跟隨移動(dòng)
    if (point.y > -V_height/3) {
        CGRect rect = self.imageView.frame;
        rect.origin.y = -point.y - V_height/3;
        self.imageView.frame = rect;
    }
    //下拉到一定程度,創(chuàng)建新的View
    if (point.y <= -V_height/3-60) {
        [self createAllImageView];
    }
}

創(chuàng)建新的視圖

/**
 *  在Window上創(chuàng)建新的View
 */
- (void)createAllImageView{
    //V_Width 屏幕寬的宏定義
    //V_height 屏幕高的宏定義
    
    //判斷如果已經(jīng)創(chuàng)建_allView,則不在創(chuàng)建
    if (_allView) {
        return;
    }
    
    //Window上的View
    _allView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, V_Width, V_height/3)];
    _allView.backgroundColor = [UIColor blackColor];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeAllImageView:)];
    [_allView addGestureRecognizer:tap];
    
    //新視圖上的ImageView
    _likeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, V_Width, V_height/3)];
    _likeImageView.image = [UIImage imageNamed:@"3"];
    [_allView addSubview:_likeImageView];
    
    //在Window上添加視圖
    [[UIApplication sharedApplication].keyWindow addSubview:_allView];
    
    CGRect rect1 = _allView.frame;
    rect1.size.height = V_height;
    
    CGRect rect2 = _likeImageView.frame;
    rect2.origin.y = (V_height - V_height/3)/2;
    
    //動(dòng)畫顯示
    [UIView animateWithDuration:0.8 animations:^{
        _allView.frame = rect1;
        _likeImageView.frame = rect2;
    }];
}

Window上視圖銷毀

/**
 *  移除Window上的View
 *
*/
- (void)removeAllImageView:(UITapGestureRecognizer *)tap{
    
    CGRect rect1 = _allView.frame;
    rect1.size.height = V_height/3;
    
    CGRect rect2 = _likeImageView.frame;
    rect2.origin.y = 0;
    
    //動(dòng)畫顯示
    [UIView animateWithDuration:0.5 animations:^{
        _allView.frame = rect1;
        _likeImageView.frame = rect2;
        _allView.alpha = 0;
    } completion:^(BOOL finished) {
        [_likeImageView removeFromSuperview];
        [_allView removeFromSuperview];
        _allView = nil;
    }];
}

tableView的一些代理事件這里就不在詳細(xì)的寫了,歡迎大家指正

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 廢話不多說,直接上干貨 ---------------------------------------------...
    小小趙紙農(nóng)閱讀 3,655評(píng)論 0 15
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,315評(píng)論 4 61
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,036評(píng)論 25 709
  • 不知道大家記憶中的年是什么樣的?我的記憶就是年三十在爺爺奶奶家吃完年夜飯后自己回家看春晚,初一早上穿上新衣服再去爺...
    玉的心貝閱讀 252評(píng)論 0 1
  • 百度說,所謂夫妻相,就是兩個(gè)人在一起生活得久了,表情動(dòng)作彼此模仿,會(huì)越來越像。夫妻相是面容相像,心靈相傾,習(xí)慣趨同...
    天與海的愛戀閱讀 1,304評(píng)論 0 0

友情鏈接更多精彩內(nèi)容