iOS-仿QQ下拉效果

2017-07-25  本文已影响96人  charleswang

现在在很多的App中都可以看见类似这样的效果:

g.gif

分析:

下面我们来实现:

 BPCustomNavBar *nav = [[BPCustomNavBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    nav.title = @"风景如画";
    nav.titleColor = [UIColor whiteColor];
    nav.leftImage = @"back_icon";
    nav.rightImage = @"share_icon";
    nav.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.f];
    [self.view addSubview:nav];
    self.navBar = nav;
//创建背景图片
    self.bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2"]];
    self.bgView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.8);
    self.originalFrame = self.bgView.frame;
    [self.view addSubview:self.bgView];
//新建UItableView
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    table.backgroundColor = [UIColor clearColor];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    self.tableView = table;
    
    //添加头部
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, headHeight)];
    headerView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = headerView;

 CGFloat yoffset = scrollView.contentOffset.y;
    NSLog(@"===========%f",yoffset);
    //当其偏移量在headView的范围内的时候,navBar颜色的改变
    if (yoffset < headHeight) {
        self.navBar.titleColor = [UIColor whiteColor];
        self.navBar.leftImage = @"back_icon";
        self.navBar.rightImage = @"share_icon";
        self.navBar.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:yoffset/headHeight];
    }else {
        self.navBar.titleColor = [UIColor whiteColor];
        self.navBar.backgroundColor = [UIColor redColor];
        self.navBar.leftImage = @"back_icon";
        self.navBar.rightImage = @"share_icon";
    }
    
    //上下滑动的时候的背景图片的放大
    if (yoffset > 0) {//往上拖动
        self.bgView.frame = ({
            CGRect frame = self.bgView.frame;
            frame.origin.y = self.originalFrame.origin.y -yoffset;
            frame;
        });
        
        
    }else {//往下拖动
        
        self.bgView.frame = ({//高度变化的比较快
            CGRect frame = self.bgView.frame;
            frame.size.height = self.originalFrame.size.height -yoffset;
            frame.size.width = self.originalFrame.size.width * frame.size.height /self.originalFrame.size.height;
            frame.origin.x = -(frame.size.width - self.originalFrame.size.width)/2;
            frame.origin.y = 0;
            frame;
        });
        
//        self.bgView.frame = ({//高度变化的快
//            CGRect frame = self.bgView.frame;
//            frame.size.width = self.originalFrame.size.width -yoffset;
//            frame.size.height = self.originalFrame.size.height * frame.size.width /self.originalFrame.size.width;
//            frame.origin.x = -(frame.size.width - self.originalFrame.size.width)/2;
//      frame.origin.y = 0;
//            frame;
//        });

    }

demo地址:https://github.com/charles1216/iOS-_f_QQ_xiala.git

上一篇 下一篇

猜你喜欢

热点阅读