Powered By Blogger

Tuesday, October 16, 2012

How to make paging in iPhone?

Hi,
Here is the example of horizontal paging using UIScrollView and its property pagingEnabled.

Look at the below code :

- (void)viewDidLoad
{
    [super viewDidLoad];
    pageCount = 0;
    scrollView.pagingEnabled = YES;
}

#pragma mark ---------------------
#pragma mark User-Defined Methods

-(void)GenerateVideoThumbnail
{
    for (UIView *videoView in self.scrollView.subviews)
    {
        [videoView removeFromSuperview];
        videoView = nil;
    }

    CGFloat X = 12.5,Y = 5.0; // Button X and Y position
    CGFloat Width = 90.0,Height = 90.0;
    CGFloat XDiff = 12.5,YDiff = 5.0;
    NSInteger MaxIconPerRow = 3;  
    NSInteger MaxIconPerPage = 12;

    for (int i=0; i<[arrVideos count]; i++)
    {
        GetVideos *obj_video = [arrVideos objectAtIndex:i];
        UIButton *btnVideo = [UIButton buttonWithType:UIButtonTypeCustom];
        btnVideo.tag = i;
        NSLog(@"frame X: %f, Y: %f",btnVideo.frame.origin.x,btnVideo.frame.origin.y);
        [btnVideo setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:obj_video.Thumnailpath]]] forState:UIControlStateNormal];

        btnVideo.frame = CGRectMake(X, Y, Width, Height);
        [self.scrollView addSubview:btnVideo];
        [btnVideo startLoading];
        btnVideo = nil;
       
        X = X + Width + XDiff;
        if((i+1)%MaxIconPerRow ==0)
        {
            X = (pageCount*320.0) + XDiff;
            Y = Y + Height + YDiff;
        }
        if((i+1)%MaxIconPerPage ==0)
        {
            ++pageCount;
            X = (pageCount*320.0) + XDiff;
            Y = 5.0;
        }
    }
    self.scrollView.contentSize = CGSizeMake((pageCount*self.scrollView.frame.size.width), self.scrollView.frame.size.height);
}

No comments:

Post a Comment