iOS界面动画特效

1、TableView的headView背景图片拉伸

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _bgImage =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CScreenWidth, CScreenHeight)];
    _bgImage.image = [UIImage imageNamed:@"pic4"];
    orginYframe = _bgImage.frame;
    [self.view addSubview:_bgImage];
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CScreenWidth, 64)];
    _headView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    _titleLBel = [[UILabel alloc] init];
    _titleLBel.frame =CGRectMake(0, 20, CScreenWidth, 44);
    _titleLBel.text = @"测试界面";
    _titleLBel.textColor = [UIColor greenColor];
    _titleLBel.textAlignment = NSTextAlignmentCenter;
    [_headView addSubview:_titleLBel];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(addDataClick) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"back" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 20, 120, 44);
    [_headView addSubview:btn];
    [self.view addSubview:_headView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, CScreenWidth, CScreenHeight-64)];
    UIView *headViewT = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CScreenWidth, 164)];
    headViewT.backgroundColor = [UIColor clearColor];
    _tableView.tableHeaderView = headViewT;
    _tableView.delegate =self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.textLabel.text =@"测试一下";
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offSetY = scrollView.contentOffset.y;
    NSLog(@"===%f",offSetY);
    if (offSetY < 164)
    {
        _titleLBel.textColor = [UIColor blueColor];
        _headView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:offSetY/164];
    }
    else
    {
        _titleLBel.textColor = [UIColor redColor];
        _headView.backgroundColor = [UIColor whiteColor];
    }
    if (offSetY > 0)
    {
        _bgImage.frame = ({
            CGRect frame = _bgImage.frame;
            frame.origin.y = orginYframe.origin.y - offSetY;
            frame;
        });
    }
    else
    {
        _bgImage.frame = ({
            CGRect frame = _bgImage.frame;
            frame.size.width =orginYframe.size.width -offSetY;
            frame.size.height = orginYframe.size.height *frame.size.width/ orginYframe.size.width;
            frame.origin.x = -(frame.size.width - orginYframe.size.width)/2;
            NSLog(@"----%@",NSStringFromCGRect(frame));
            frame;
        });
    }
    if (offSetY ==0)
    {
        _titleLBel.textColor = [UIColor redColor];
    }
}
原文地址:https://www.cnblogs.com/xianfeng-zhang/p/8608671.html