Monday, 3 June 2013

How to create vertical scroll view programmatically: cocos2d

To create a vertical scroll view with items, create an UIVIew and add ScrollView to it and add items to that ScrollView:

// in .h File //

        UIView *profileView;
        UIScrollView *verticalScroll;   

   

// in .m File //

    -(void)show_{

        // Create a UI View //
        profileView = [[UIView alloc] init];
        profileView.frame = CGRectMake(0, 0, 320, 480);
        profileView.backgroundColor = [UIColor blueColor];
        [[[CCDirector sharedDirector] openGLView] addSubview:profileView];
   
        // Create a UIScroll View //
        verticalScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 300, 420)];
        verticalScroll.backgroundColor = [UIColor yellowColor];
        verticalScroll.showsVerticalScrollIndicator = NO;
        verticalScroll.contentSize = CGSizeMake(300, 760);
        verticalScroll.center = CGPointMake(160, 250);
        verticalScroll.pagingEnabled = FALSE;
        [profileView addSubview:verticalScroll];
   
        // Create and add an UIImageView to the ScrollView //
        UIImageView *baseImg1 = [[UIImageView alloc] initWithFrame:CGRectMake(5, 20, 325, 65)];
        baseImg1.image = [UIImage imageNamed:@"imageName.png"];
        baseImg1.center =CGPointMake(150, 340);
        [verticalScroll addSubview:baseImg1];
        [baseImg1 release];
   
    }

No comments:

Post a Comment