微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

uiscrollview加载子视图而不是图像

如何解决uiscrollview加载子视图而不是图像

| 我从apple示例项目滚动中获得了以下代码
@synthesize scrollView1,scrollView2;

const CGFloat kScrollObjHeight  = 467.0;
const CGFloat kScrollObjWidth   = 320.0;
const NSUInteger kNumImages     = 6;


- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [scrollView1 subviews];


    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc,0);
            view.frame = frame;

            curXLoc += (kScrollObjWidth);
        }
    }

    // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth),[scrollView1 bounds].size.height)];
}
我试图修改它以加载子视图而不是图像,但是它不起作用。我想以某种方式替换视图而不是添加视图?我需要从单独的xib文件加载视图,并在xib \的左右滚动(它们每个都包含一个导致游戏中不同级别的按钮面板)。那我在做什么错?为什么不起作用?它只显示第二个面板,每当我按下按钮时,它就会因SIGABRT而崩溃。
#import \"LevelSelectButtons.h\"

@implementation LevelSelectButtons
@synthesize levelScroll;

const int kScrollObjHeight = 400;
const int kScrollObjWidth = 320;
const NSUInteger kNumCategories = 5;

- (void)layoutScrollScreens {
    UIImageView *view = nil;
    NSArray *subviews = [levelScroll subviews];

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    for (view in subviews) {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc,0);
            view.frame = frame;

            curXLoc += (kScrollObjWidth);
        }
    }

    // set the content size so it can be scrollable
    [levelScroll setContentSize:CGSizeMake((kNumCategories * kScrollObjWidth),[levelScroll bounds].size.height)];
}

- (void)viewDidLoad {
    [levelScroll setScrollEnabled:YES];
    CGFloat width = 640; //kScrollObjWidth*kNumCategories;
    CGFloat height = 400;
    [levelScroll setContentSize:CGSizeMake(width,height)];
    [self.view addSubview:levelScroll];
    levelScroll.pagingEnabled = YES;
    levelScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;

    levelScroll.pagingEnabled = YES;

    /// LEVELS 1 ///
    NSUInteger viewNum = 0;
    CGRect catViewFrame = CGRectMake(0,kScrollObjWidth,kScrollObjHeight);
    UIView *levels1view = [[UIView alloc] initWithFrame:catViewFrame];

    NSArray *array1 = [[NSBundle  mainBundle] loadNibNamed:@\"Levels1\" owner:self options:nil];
    levels1view = [array1 objectAtIndex:0];
    levels1view.tag = viewNum;

    [self.levelScroll addSubview:levels1view];


    /// LEVELS 2 ///
    viewNum = 1;
    catViewFrame = CGRectMake(320,kScrollObjHeight);
    UIView *levels2view = [[UIView alloc] initWithFrame:catViewFrame];
    NSArray * array2= [[NSBundle mainBundle] loadNibNamed:@\"Levels2\" owner:self options:nil];
    levels2view = [array2 objectAtIndex:0];
    levels2view.tag = viewNum;

    [self.levelScroll addSubview:levels2view];

    [self layoutScrollScreens];
    [super viewDidLoad];
}
这是SIGABRT出现的行:
int retVal = UIApplicationMain(argc,argv,nil,nil);
    

解决方法

        这是为了解释为什么只显示“ 3”。
/// LEVELS 2 ///
viewNum = 1;
catViewFrame = CGRectMake(320,kScrollObjWidth,kScrollObjHeight);
UIView *levels2view = [[UIView alloc] initWithFrame:catViewFrame];
如果您在此处查看,则说明您正在使用所需的框架创建新视图,您要在其中放置
levels2view
。但,
NSArray * array2= [[NSBundle mainBundle] loadNibNamed:@\"Levels2\" owner:self options:nil];
levels2view = [array2 objectAtIndex:0];
levels2view.tag = viewNum;

[self.levelScroll addSubview:levels2view];
在这里,您正在将变量重新分配给新视图,从而丢失了对您创建的视图的引用,从而导致内存泄漏。顺便说一句,这个视图没有框架。默认将其原点设为
CGPointZero
。而且,由于您对
levels1view
执行相同的操作,因此当按顺序将
levels2view
添加到滚动视图时,最终会将
levels2view
定位在
levels1view
上方。 为了解决这个问题,
viewNum = 1;
catViewFrame = CGRectMake(320,kScrollObjHeight);

NSArray * array2= [[NSBundle mainBundle] loadNibNamed:@\"Levels2\" owner:self options:nil];
UIView * levels2view = [array2 objectAtIndex:0];
levels2view.frame = catViewFrame;
levels2view.tag = viewNum;

[self.levelScroll addSubview:levels2view];
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。