奇怪的UIScrollView行为

时间:2011-05-04 18:58:03

标签: iphone uiscrollview

我对这个卷轴视图感到非常困惑。出于某种原因,在此循环的第三遍中,额外的两个UIImageViews被添加为子视图。我假设我正在解决记忆力不好或类似的问题,但我无法弄清楚它的来源是为了我的生活。

以下是代码:

scrollview=[[[UIScrollView alloc] initWithFrame:CGRectMake(x, y+40, 300, 225)] retain];

NSArray *chunks=[lesson.photoString componentsSeparatedByString:@"|"];
ALAssetsLibrary *assetLibrary;
__block CGRect workingFrame = scrollview.bounds;
__block UIImageView *imageview=nil;
workingFrame.origin.x = 0;
NSLog(@"Chunks: %d\n",[chunks count]);
for(NSString *url in chunks) {        
    //
    if(url && [url length])
    {
        NSURL *asseturl = [NSURL URLWithString:url];
        assetLibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetLibrary assetForURL:asseturl 
                      resultBlock:^(ALAsset *myasset)
         {
             UIView *sub=[[UIView alloc] initWithFrame:workingFrame];
             imageview=[[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:[[myasset defaultRepresentation] fullScreenImage]]];
             [imageview setContentMode:UIViewContentModeScaleAspectFit];
             imageview.frame = sub.bounds;
             [sub addSubview:imageview];
             [imageview release];
             UIButton *imgfull=[UIButton buttonWithType:UIButtonTypeCustom];
             [imgfull setFrame:sub.bounds];
             imgfull.backgroundColor=[UIColor clearColor];
             [imgfull addTarget:self action:@selector(imageViewToFullScreen) forControlEvents:UIControlEventTouchUpInside];
             [sub addSubview:imgfull];
             [imgfull release];
             [scrollview addSubview: sub];
             [sub release];
             NSLog(@"%@",[scrollview subviews]);
             workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
             [scrollview setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
             if(scrollview.contentSize.width>300)
                 [scrollview flashScrollIndicators];
         }
                     failureBlock:^(NSError *myerror){
                         NSLog(@"Failure - %@",[myerror localizedDescription]);
                     }
         ];
    }
}                 
[scrollview setPagingEnabled:YES];
[self.view addSubview:scrollview];

这是输出:

2011-05-04 11:53:00.331 AppName[24896:207] Chunks: 4
2011-05-04 11:53:00.369 AppName[24896:207] (
    "<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>"
)
2011-05-04 11:53:00.394 AppName[24896:207] (
    "<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>",
    "<UIView: 0x642db00; frame = (300 0; 300 225); layer = <CALayer: 0x6420c80>>"
)
2011-05-04 11:53:00.426 AppName[24896:207] (
    "<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>",
    "<UIView: 0x642db00; frame = (300 0; 300 225); layer = <CALayer: 0x6420c80>>",
    "<UIImageView: 0x666e3d0; frame = (292 1; 7 223); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x666e400>>",
    "<UIImageView: 0x6605490; frame = (1 217; 149 7); opaque = NO; autoresize = TM; userInteractionEnabled = NO; animations = { opacity=<CABasicAnimation: 0x6686ff0>; }; layer = <CALayer: 0x6664280>>",
    "<UIView: 0x6686c30; frame = (600 0; 300 225); layer = <CALayer: 0x6686c60>>"
)
2011-05-04 11:53:00.439 AppName[24896:207] (
    "<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>",
    "<UIView: 0x642db00; frame = (300 0; 300 225); layer = <CALayer: 0x6420c80>>",
    "<UIImageView: 0x666e3d0; frame = (292 1; 7 223); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x666e400>>",
    "<UIView: 0x6686c30; frame = (600 0; 300 225); layer = <CALayer: 0x6686c60>>",
    "<UIImageView: 0x6605490; frame = (1 217; 99 7); opaque = NO; autoresize = TM; userInteractionEnabled = NO; animations = { opacity=<CABasicAnimation: 0x6648d60>; }; layer = <CALayer: 0x6664280>>",
    "<UIView: 0x6673f90; frame = (900 0; 300 225); layer = <CALayer: 0x6673fc0>>"
)

编辑:我还应该提一下,我已经NSLog了sub,它会产生我期望的结果(1个图像视图,每次循环1个按钮) 。我还要提一下,据我所知,我的应用程序中的任何地方都没有滚动视图记录(99x7或7x223)显示的尺寸图像。

编辑2:更多信息:我将添加行切换为[scrollview addSubview:nil];,我仍然看到在循环的第三遍中添加了两个图像视图。此外,不会从发布的代码部分之外的任何其他位置访问scrollview。

编辑3:想通了。我太早地闪烁了滚动条,这与滚动视图中的子视图的顺序相混淆。

3 个答案:

答案 0 :(得分:2)

如果您的滚动视图中没有实际显示异常图像,您可以假设这些子视图由UIScrollView在内部管理,您应该忽略它们。也许是滚动指标。

答案 1 :(得分:2)

这些滚动视图指示灯是否闪烁?

答案 2 :(得分:0)

实际上这些额外的两个图像是uiscrollindicator图像,它们是垂直滚动指示器和水平滚动指示器,标签为0,由UIScrollview自动添加,如果你将UIIMageViews添加到ScrollView,请确保指定大于零的标签,或使用一些其他技术,以便您可以通过大于零的标记检索UIIMageViews。