美文网首页
下拉放大

下拉放大

作者: 天亮説晚安 | 来源:发表于2016-01-23 22:29 被阅读177次

最近学了一个下拉放大的功能。

#import "ViewController.h"
#define TOPVIEWHEIGH 300
#define SCREENWIDTH self.view.frame.size.width 
#define SCREENHEIGHT self.view.frame.size.height

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UIImageView * topView;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
   //创建tableView
    UITableView * myTableView = [[UITableView alloc]init];
    myTableView.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];
   
    //核心代码1,核心代码1,核心代码1(重要的话说三遍)
    myTableView.contentInset = UIEdgeInsetsMake(TOPVIEWHEIGH, 0, 0, 0);
    //tableView在当前屏幕上的初始位置
    myTableView.contentOffset = CGPointMake(0, -TOPVIEWHEIGH);
    UIImageView * topView = [[UIImageView alloc]init];
    topView.clipsToBounds =  YES;
    [topView setImage:[UIImage imageNamed:@"biaoqingdi.png"]];
    topView.frame = CGRectMake(0, -TOPVIEWHEIGH, SCREENWIDTH, TOPVIEWHEIGH);
    //图等比例变化,防止图变形(很关键)
    topView.contentMode = UIViewContentModeScaleAspectFill;
    [myTableView addSubview:topView];
    self.topView = topView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//核心代码2,核心代码2,核心代码2(重要的话说三遍)
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    //topView的初始位置是(0,- TOPVIEWHEIGH)
    //当y < - TOPVIEWHEIGH时,表示topView开始下拉
    if(y < - TOPVIEWHEIGH)
    {
        CGRect myFrame = self.topView.frame;
        myFrame.origin.y = y;
        myFrame.size.height = -y;
        self.topView.frame = myFrame;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * string = @"str";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        
    }
    cell.textLabel.text = [NSString stringWithFormat:@"我是帅哥%ld",(long)indexPath.row];
    return cell;
}

@end

相关文章

网友评论

      本文标题:下拉放大

      本文链接:https://www.haomeiwen.com/subject/mqurkttx.html