无法在iOS中滚动图像

时间:2013-12-19 05:47:20

标签: ios iphone objective-c

我在里面创建了scrollview,我正在使用这个我可以交换images.if我需要9个图像在scrollview中显示但如果我给setUserInteractionEnabled:NO意味着我可以切换我可以交换但是如果我给setUserInteractionEnabled:是然后它的滚动工作不是瓷砖我不能交换图像,甚至不能触摸图像。任何人都可以告诉我这下面的代码有什么错误..

-(void)ViewDidLoad
{
thumbnailscrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
    thumbnailscrollview.delegate = self;

    thumbnailscrollview.backgroundColor = [UIColor whiteColor];
    [thumbnailscrollview setUserInteractionEnabled:YES];
    [thumbnailscrollview setContentSize:CGSizeMake(320.0,1600.0)];
       thumbnailscrollview.showsVerticalScrollIndicator = YES;
    [self.view addSubview:thumbnailscrollview];

[self createTiles];
}



- (void)createTiles {

    theviewlabel=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
    // [self.view addSubview:thumbnailscrollview];
    [thumbnailscrollview addSubview:theviewlabel];
    tileindex=0;

    UIColor *tileColors[] = {
        [UIColor whiteColor],
        [UIColor whiteColor],
        [UIColor whiteColor],
        [UIColor whiteColor],
        [UIColor whiteColor],
        [UIColor whiteColor],
        [UIColor whiteColor],
    };
    int tileColorCount = sizeof(tileColors) / sizeof(tileColors[0]);

    int rows=([_imageCollection1 count]/3)+1;
    CGRect frame;
    int count=0;
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < TILE_COLUMNS; col++) {

            if ([_imageCollection1 count] <= count) {
                return;
            }


            int index = (row * TILE_COLUMNS) + col;

             frame = CGRectMake(TILE_MARGIN + col * (TILE_MARGIN + TILE_WIDTH),
                                      TILE_MARGIN + row * (TILE_MARGIN + TILE_HEIGHT),
                                      TILE_WIDTH, TILE_HEIGHT);

            CGRect frame2 = CGRectMake(TILE_MARGIN + col * (TILE_MARGIN + TILE_WIDTH)-12,
                                       TILE_MARGIN + row * (TILE_MARGIN + TILE_HEIGHT)-3,
                                       12, 12);

            tileFrame[index] = frame;

            Tile *tile = [[Tile alloc] init];
            tile.tileIndex = index;
            tile.tag=[[_buttontagcollections1 objectAtIndex:index]intValue];
            tile.mRearrangeView=[_imageCollection1 objectAtIndex:count];
                       count++;
            tileForFrame[index] = tile;
            tile.frame = frame;
            tile.backgroundColor = tileColors[index % tileColorCount].CGColor;

            //tile.cornerRadius = 8;
            tile.delegate = self;
            thelabel=[[UILabel alloc]initWithFrame:frame2];
            thelabel.backgroundColor=[UIColor whiteColor];
            thelabel.textColor=[UIColor redColor];
            thelabel.layer.cornerRadius=7;
            //button.titleLabel.textAlignment = NSTextAlignmentCenter;
            thelabel.textAlignment=NSTextAlignmentCenter;
            thelabel.text=[NSString stringWithFormat:@"%d",tileindex
                           ];
            thelabel.font=[UIFont systemFontOfSize:13];

                       //[self.view.layer addSublayer:tile];
            [theviewlabel addSubview:thelabel];

            homeView = [[UIView alloc]initWithFrame:CGRectMake(248, -480, 72, 480)];
            homeView.backgroundColor = [UIColor clearColor];
            [thumbnailscrollview addSubview:homeView];

                     [tile setNeedsDisplay];
            tileindex++;
            homeView = [[UIView alloc]initWithFrame:CGRectMake(248, -480, 72, 480)];
            homeView.backgroundColor = [UIColor clearColor];
            [thumbnailscrollview addSubview:homeView];

            [thumbnailscrollview.layer addSublayer:tile];

        } 
    }

}

1 个答案:

答案 0 :(得分:1)

在此处输入代码为什么要在添加子视图以滚动视图之前调整内容大小。在将所有子视图添加到滚动视图后,始终添加内容大小。滚动视图的内容大小必须大于其超级视图。

你做了什么:

viewDIDLoad中的

thumbnailscrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,
                                            self.view.frame.origin.y,
                                            self.view.frame.size.width,
                                            self.viewframe.size.height)];

//[thumbnailscrollview setContentSize:CGSizeMake(320.0,1600.0)]; comment this line

并在createTile方法中,当您完成添加子视图后,请设置内容大小:

CGRect contentRect = CGRectZero;
for (UIView *view in self.thumbnailscrollview.subviews)
{

    contentRect = CGRectUnion(contentRect, view.frame);
}
self.thumbnailscrollview.contentSize = contentRect.size;

它应该适用。

感谢