• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 UITableView 中滚动时缩放图像

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:00:07 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试重现这种效果 -

在 UITableView 的 header 部分中有一个 UIImageView。当用户向下滚动时,图像的大小会减小并在导航栏下方滚动,而当用户向上滚动时,UIImageView 会展开并返回到其原始位置。

iOS 版 Twitter 会对你的个人资料产生这种影响。

到目前为止,这就是我所知道的 - 代码将位于 scrollViewDidScroll 方法下,我将不得不使用 2 张图像,一张大一张小。我需要帮助弄清楚如何在方法内的这两个图像之间进行转换。



Best Answer-推荐答案


如果你想实现 Twitter 滚动扩展 UIImageView,Yari D'areglia 的教程概述了如何做到这一点。你可能会发现它很有用。该链接包含一个演示所需效果的视频。

http://www.thinkandbuild.it/implementing-the-twitter-ios-app-ui/

Alternative Source Code

#define IMAGE_VIEW_TAG 100
#define IMAGE_SCROLL_VIEW_TAG 101

    #pragma mark - ScrollView Delegate
- (void)scrollViewDidScrollUIScrollView *)scrollView
{
        if(scrollView.tag == IMAGE_SCROLL_VIEW_TAG) return;

    UITableView * tv = (UITableView*) scrollView;
    UITableViewCell * cell = [tv cellForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
    UIScrollView * svImage = (UIScrollView*)[cell viewWithTag:IMAGE_SCROLL_VIEW_TAG];
    CGRect frame = svImage.frame;
    frame.size.height =  MAX((_imageHeaderHeight-tv.contentOffset.y),0);
    frame.origin.y = tv.contentOffset.y;
    svImage.frame = frame;
    svImage.zoomScale = 1 + (abs(MIN(tv.contentOffset.y,0))/320.0f);
}

- (UIView *)viewForZoomingInScrollViewUIScrollView *)scrollView
{
        return [scrollView viewWithTag:IMAGE_VIEW_TAG];
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
        UITableViewCell * cell;

        NSLog(@"Row %@", indexPath);
        if(indexPath.row == 0){
                static NSString * headerId = @"headerCell";
                cell = [tableView dequeueReusableCellWithIdentifier:headerId];
                if(!cell) {

// create cell as it doesn't exist
                            cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, _imageHeaderHeight)];
                            cell.backgroundColor = [UIColor clearColor];
                            cell.selectionStyle = UITableViewCellSelectionStyleNone;

                        // image view
                        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, cell.contentView.frame.size.width, _imageHeaderHeight)];
                        imageView.contentMode = UIViewContentModeScaleAspectFill;
                        imageView.tag = IMAGE_VIEW_TAG;
                        imageView.clipsToBounds = YES;
                        imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

                        UIScrollView * svImage = [[UIScrollView alloc]initWithFrame:imageView.frame];
                        svImage.delegate = self;
                        svImage.userInteractionEnabled = NO;

                        [svImage addSubview:imageView];

                        // image
                        svImage.tag = IMAGE_SCROLL_VIEW_TAG;
                        svImage.backgroundColor = [UIColor blackColor];
                        svImage.zoomScale = 1.0f;
                        svImage.minimumZoomScale = 1.0f;
                        svImage.maximumZoomScale = 2.0f;
                        [cell.contentView addSubview:svImage];
                        UIImageView * headerInfo = [[UIImageView alloc] initWithImage:[UIImage imageNamed"image.png"]];

                    [cell.contentView addSubview:headerInfo];

                    CGRect headerFrame = headerInfo.frame;
                    headerFrame.size.height = 149.0f;
                    headerFrame.origin.y = _imageHeaderHeight - 149.0f;
                    headerInfo.frame = headerFrame;
}
// cell exists - grab a reference to the image it displays
            UIImageView *imageView = (UIImageView*)[cell viewWithTag:IMAGE_VIEW_TAG];

// maybe change the imageView
return cell;
}

关于ios - 在 UITableView 中滚动时缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27530891/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap