首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIView -让UITableViewCells滚动到IOS上(就像shazam应用程序)

UIView -让UITableViewCells滚动到IOS上(就像shazam应用程序)
EN

Stack Overflow用户
提问于 2015-05-28 09:23:32
回答 4查看 268关注 0票数 1

我的UITableView上面有一个UIView。当用户向下滚动时,我希望UIView保持在屏幕顶部的位置,并让单元格滚动到它上面。Shazam应用程序的第一个页面做了我想要的事情。

代码语言:javascript
复制
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.tableView.contentOffset.y > 0) {
        CGRect newframe = self.publicTopView.frame;
        newframe.origin.y = -self.tableView.contentOffset.y;
        self.publicTopView.frame = newframe;
}

}

这就是我到目前为止尝试过的方法,但它什么也做不了。谢谢

EN

回答 4

Stack Overflow用户

发布于 2015-05-28 10:02:42

我会将tableView.backgroundView设置为您的视图,然后将您的单元格设置为清晰(或您喜欢的任何透明度)。这将允许单元格在视图上移动。

票数 1
EN

Stack Overflow用户

发布于 2015-05-28 09:33:08

你需要有一个透明背景的scrollView。然后添加一个不透明的子视图,并向下偏移一点。这里有一个过度杀伤力的例子。TextureView只是为了让你可以看到背景没有移动。

代码语言:javascript
复制
#import "ViewController.h"
@interface TextureView : UIView
@end
@implementation TextureView

-(void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
    CGContextMoveToPoint(context, 0, rect.size.height);
    CGContextAddLineToPoint(context, rect.size.width, 0);
    CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor]CGColor]);
    CGContextSetLineWidth(context, 8);
    CGContextStrokePath(context);
}

@end

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CGRect scrollViewFrame = self.view.frame;
    CGFloat sectionHeight = 300;
    int numberSections = 3;
    CGFloat clearSpaceAtTop = 300;

    CGRect sectionsViewFrame = CGRectMake(0, clearSpaceAtTop, scrollViewFrame.size.width, sectionHeight * numberSections);
    CGSize contentSize = CGSizeMake(scrollViewFrame.size.width, CGRectGetMaxY(sectionsViewFrame));



    TextureView *backGroundView = [[TextureView alloc]initWithFrame:scrollViewFrame];
    backGroundView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:backGroundView];

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:scrollViewFrame];
    scrollView.contentSize = contentSize;
    scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scrollView];

    UIView *sectionViewsParent = [[UIView alloc]initWithFrame:sectionsViewFrame];
    sectionViewsParent.backgroundColor = [UIColor lightGrayColor];
    [scrollView addSubview:sectionViewsParent];

    NSArray *colors = @[[UIColor redColor],[UIColor greenColor],[UIColor purpleColor]];
    CGFloat spacer = 4;
    CGRect colorViewFrame = CGRectMake(0, 0, sectionsViewFrame.size.width, sectionHeight - spacer);
    for (UIColor *color in colors){
        UIView *sectionView = [[UIView alloc]initWithFrame:colorViewFrame];
        sectionView.backgroundColor = color;
        [sectionViewsParent addSubview:sectionView];
        colorViewFrame.origin.y += sectionHeight;
    }

}

@end
票数 0
EN

Stack Overflow用户

发布于 2015-05-28 10:25:56

如果UIView实例称为iconView,则UITableView实例称为tableView,如果您的视图控制器为UIViewController。

代码语言:javascript
复制
[self.view addSubview:iconView];
[self.view addSubview:tableView];
//and you need set tableView.background to transparent.

如果您的视图控制器是UITableViewController,请阅读以下问题:

UITableViewController Background Image

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30495711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档