Aslam
Aslam

Reputation: 231

Not able to scroll images in iOS

I have create scrollview inside that i am displaying tiles using this i can swap images.if i take 9 images it displaying in scrollview but If i give setUserInteractionEnabled:NO means i can tiles i can swap but If i give setUserInteractionEnabled:YES then its scroll is working not tiles i can not swap image and not even touch images.Can any one tell me what is the mistake in this below code..

-(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];

        } 
    }

}

Upvotes: 1

Views: 107

Answers (1)

maddy
maddy

Reputation: 4111

enter code herewhy are you adjusting content size BEFORE adding subviews to scroll view.Always add content size after adding all subviews to scroll view.The content size of scroll view must be larger than its superview.

what you do :

in 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

and in createTile method, when you are all done adding your subview then set content size:

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

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

it should work then.

thanks

Upvotes: 1

Related Questions