简体   繁体   中英

How to properly remove an UIImageView from UIScrollView?

I'm developing an app that i need to load many images in a view using Xcode 4.5.2

So, At the top, I have a UIView , inside of this UIView , i have UIScrollView to display many images,i'm using this UIScrollView in pagingEnabled mode. As i have many images to load, i know that it cannot be possible to load at once. So, i decided to add or remove if need be.

When the app starts, i've loaded three images in UIScrollView , when user passes to image 2, i want to remove image 0 from UIScrollView .When i try to remove the image 0,i got totally white screen.The funny part is that if i dont try to remove image 0,when user passes to image 3, i want to load a new image(image 4) to UIScrollView , and remove image 1.So far, I'm able to do this. Basically,my apps works just fine, but when i want to remove the first( at index 0 )image,I got totally white screen on the screen.

Do you have any idea why i'm getting this behaviour ? I got neither warning,nor error. How is it possible that i'm able to remove all the other images but not the image 0 ?

Thank you.

     -(void)scrollViewDidScroll:(UIScrollView *)scrollView
{ 
static NSInteger currentPage = 0 ;
CGFloat pageWidth = self.scrollView.frame.size.width ;
float fractionalPage = self.scrollView.contentOffset.x / pageWidth ;
NSLog(@"%f",fractionalPage);
if(fractionalPage == currentPage+1)
{
    NSLog(@"Swipe Left");
    currentPage++;
    NSLog(@"Current page :%d",currentPage);

    [self setLayoutImages:kLeft] ;
}
else if(fractionalPage+1 ==currentPage)
{
    NSLog(@"Swipe Right");
    currentPage--;
    NSLog(@"Current page :%d",currentPage);
    [self setLayoutImages:kRight];
}

}

-(void)setLayoutImages:(Direction )direction
{
int currentPage = self.scrollView.contentOffset.x / scrollViewWidth ;
if(direction == kLeft)
{
    self.scrollView.contentSize = CGSizeMake(((currentPage+2) * scrollViewWidth), 350.0f);
    if(![self.scrollView viewWithTag:currentPage+1])
    {
        NSString *imageName = @"iPhone.png" ;
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image ] ;
        imageView.tag  = currentPage+1 ;
        CGRect frame = imageView.frame ;
        frame.size.height = imageHeight;
        frame.size.width = scrollViewWidth ;
        frame.origin.x = (currentPage+1) * scrollViewWidth ;
        frame.origin.y = 0 ;
        imageView.frame = frame ;
        if (currentPage>2) { //To be able to remove the first image,i need to add equal here,but i got white screen.
            [[self.scrollView viewWithTag:(currentPage -2)]removeFromSuperview ] ;            }
        [self.scrollView addSubview:imageView ] ;
    }
}
else if(direction == kRight)
{
    if(![self.scrollView viewWithTag:currentPage-1] && currentPage >0)
    {
        NSString *imageName = @"gs.jpeg";
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image ];
        imageView.tag = currentPage-1 ;
        CGRect frame = imageView.frame ;
        frame.size.height = imageHeight ;
        frame.size.width = scrollViewWidth ;
        frame.origin.x = (currentPage-1)*scrollViewWidth ;
        frame.origin.y = 0 ;
        imageView.frame = frame ;
        [[self.scrollView viewWithTag:currentPage+2]removeFromSuperview ] ;
        [self.scrollView addSubview:imageView ];
    }
}

}

You can Give each one of three deferant Tag and reuse it by [[view viewWithtag:tag] removeFromSuperView] or you can try ready project called iCarousel made by nicklockwood it is an wonderful project.

it can be horizontal and vertical

在此处输入图片说明

尝试使您的视图以标签1而不是0开头,因为如果我没有记错的话,0是未标记视图的默认值,这可能会导致问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM