Monday, 3 September 2012

For the paging and scrollview photo gallery in iPhone

For the paging and scrollview photo gallery in iPhone
- (void)setupDisplayFiltering{
   
    NSArray *subview = [[mainScrollView subviews] copy];
    for (UIView *subview1 in subview) {
        [subview1 removeFromSuperview];
    }
    [subview release];
    
     mainScrollView.delegate = self;
   
    [mainScrollView setBackgroundColor:[UIColor clearColor]];
    [mainScrollView setCanCancelContentTouches:NO];
   
    mainScrollView.clipsToBounds = YES;
    mainScrollView.scrollEnabled = YES;
    mainScrollView.pagingEnabled = YES;
   
    CGFloat cx = 20;
    CGFloat cy = 10;
    CGFloat width=110;
    CGFloat height=110;
    int cnt=1;
    
    int scWidth=0;
   
    int row=0;
    int column=0;
   
    for (int i=0;i<[mainCatImgArray count];i++) {
       
        UIImage *image = [mainCatImgArray objectAtIndex:i];
        UIImageView *tempImg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"viewBg.png"]];
        CGRect rect = tempImg.frame;
        rect.size.height =  height;
        rect.size.width = width;
        rect.origin.x = cx;
        rect.origin.y = cy;
        tempImg.frame = rect;
        [mainScrollView addSubview:tempImg];
        [tempImg release];
     
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        rect = imageView.frame;
        rect.size.height = height-30;
        rect.size.width = width-10;
        rect.origin.x = cx+5;
        rect.origin.y = cy+5;
        imageView.userInteractionEnabled=YES;
        imageView.frame = rect;
        imageView.tag=i+1;
       
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollImgTap:)];
        [tapRecognizer setNumberOfTapsRequired:1];
        [tapRecognizer setDelegate:self];
       
        [imageView addGestureRecognizer:tapRecognizer];
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
        longPressGesture.minimumPressDuration=0.2f;
        [imageView addGestureRecognizer:longPressGesture];
        [longPressGesture release];
       
        [mainScrollView addSubview:imageView];
       
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"close_x.png"] forState:UIControlStateNormal];
        rect.size.height = 40;
        rect.size.width = 40;
        rect.origin.x = cx;
        rect.origin.y = cy+5;
        btn.userInteractionEnabled=YES;
        btn.frame = rect;
        btn.tag=i+1;
        [btn addTarget:self action:@selector(removeImageView:) forControlEvents:UIControlEventTouchUpInside];
       //  btn.hidden=YES;
        [mainScrollView addSubview:btn];   
       
       
        UILabel *tempIbl=[[UILabel alloc] initWithFrame:CGRectMake(cx,height+cy-20,width,25)];
        tempIbl.text=[NSString stringWithFormat:@"%d",imageView.tag];
        tempIbl.backgroundColor=[UIColor whiteColor];   
       
        tempIbl.textColor = [UIColor blackColor];
        tempIbl.textAlignment = UITextAlignmentCenter;
        tempIbl.font = [UIFont fontWithName:@"Arial" size:20.0f];
        tempIbl.lineBreakMode=UILineBreakModeMiddleTruncation;
        [mainScrollView addSubview:tempIbl];
       
        [imageView release];
        [tempIbl release];
        [btn release];
       
        if (column<=5) {
            column+=1;
            cx += tempImg.frame.size.width+20;
            NSLog(@"column %d",column);
            if (column>=5){
                column=0;
                cx=scWidth+20;
                cy+=tempImg.frame.size.height+15;
                row+=1;
                NSLog(@"row %d",row);
                if (row>=5) {
                    scWidth=683*cnt;
                    cx=scWidth+20;
                    cy=10;
                    row=0;
                     cnt+=1;
                }
              
            }
        }
    }
   
    pageControl.currentPage = 0;
    pageControl.numberOfPages=cnt;
    [mainScrollView setContentSize:CGSizeMake(683*cnt, [mainScrollView bounds].size.height)];
}
- (IBAction)changePage {
    // update the scroll view to the appropriate page
    CGRect frame;
    frame.origin.x = mainScrollView.frame.size.width * pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = mainScrollView.frame.size;
    [mainScrollView scrollRectToVisible:frame animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
    // Update the page when more than 50% of the previous/next page is visible
    CGFloat pageWidth = mainScrollView.frame.size.width;
    int page = floor((mainScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
    currentPageLbl.text=[NSString stringWithFormat:@"%d",page];
}

No comments:

Post a Comment