【问题标题】:How to properly remove an UIImageView from UIScrollView?如何从 UIScrollView 中正确删除 UIImageView?
【发布时间】:2012-12-10 22:03:33
【问题描述】:

我正在开发一个应用程序,我需要使用 Xcode 4.5.2 在视图中加载许多图像

所以,在顶部,我有一个UIView,在这个UIView 里面,我有一个UIScrollView 来显示许多图像,我在pagingEnabled 模式下使用这个UIScrollView。由于我有许多图像要加载,我知道不可能一次加载。因此,我决定根据需要添加或删除。

当应用程序启动时,我在UIScrollView 中加载了三个图像,当用户传递到图像 2 时, 我想从UIScrollView 中删除图像 0。当我尝试删除图像 0 时,我得到了完全白屏。有趣的是,如果我不尝试删除图像 0,当用户传递到图像 3 时,我想将新图像(图像 4)加载到 UIScrollView,并删除图像 1。到目前为止,我能够做到这一点。基本上,我的应用程序工作得很好,但是当我想删除第一个(at index 0)图像时,屏幕上出现了完全白屏。

你知道我为什么会出现这种行为吗?我既没有收到警告,也没有收到错误。我怎么可能删除所有其他图像但不能删除图像 0 ?

谢谢。

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

}

【问题讨论】:

  • 一些代码会有所帮助。您能否发布在 UISCrollView 中添加和删除 UIImageViews 的代码
  • @Bergasms,我编辑了我的问题
  • 首先,我认为 currentPage 不需要是静态的。其次,尝试让您的视图以 1 而不是 0 的标记开始,因为如果我没记错的话,0 是未标记视图的默认值,这可能会导致问题。
  • @Bergasms,currentPage 必须是静态的。但是,你说的其他内容,我会尝试。
  • @Bergasms,你是对的。它现在工作。添加您的评论作为答案,我会接受。

标签: iphone ios uiview uiscrollview uiimageview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以给三个延期Tag 中的每一个,然后由[[view viewWithtag:tag] removeFromSuperView] 重复使用,或者您可以尝试由nicklockwood 制作的名为iCarousel 的现成项目,这是一个很棒的项目。

    可以是horizontal和vertical

    【讨论】:

    • 你可以看看我的代码,我一直在使用tag属性和你刚才提到的所有方法。但是,无论如何,谢谢,顺便说一句,在我的情况。
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    相关资源
    最近更新 更多