【问题标题】:UIScrollview content display problemUIScrollview内容显示问题
【发布时间】:2011-08-03 07:20:09
【问题描述】:

我有一个 scollview,我想显示 10 张图像。 iPhone 的帧宽度为 320,iPad 的帧宽度为 768。 像这张图片的东西

中间图像将是我们当前的选择(根据此快照的山图像)。 现在我想向左或向右滚动以检查所有图像。 如果瀑布位于位置 1 的位置 2 的山和位置 3 的小屋,当我向右滚动时,我想显示位置 1 的山在位置 2 的小屋和位置 3 的新图像。

请告诉我怎么做

【问题讨论】:

    标签: iphone objective-c xcode ipad uiscrollview


    【解决方案1】:

    您必须根据图像大小调整和设置scrollViewcontentOffset。您还必须为scrollView 设置必要的contentSize,这将启用滚动。

    【讨论】:

    • 我知道 contentSize 的用法,但我不知道 contentOffset 的确切用法
    • 您必须调整点以使其与图像大小相匹配。通过将此点分配给滚动视图的 contentOffset,您可以控制滚动。
    • 好的,我刚刚尝试了 contenoffset 并且它也可以工作,但是我应该在哪里设置它。我试图在 scrollViewDidEndDeclarating 中设置它:(
    【解决方案2】:

    嘿,使用内容偏移量。

    并使用此代码你得到你想要的。
    这是在 iPhone 上一次看到两个图像的滚动视图,当图像在 iPhone 中间滚动时,标签设置为 midlle 图像标签。当您将宽度 320 更改为 320

    -(void)scrollViewDidScroll:(UIScrollView *)inscrollView{

    p = scrollView.contentOffset;
    
    NSLog(@"x = %f, y = %f", p.x, p.y);
    
    [self loadDescLbl]; 
    }  
    

    -(void)loadDescLbl {

    int point;
    point = p.x+160;
        int i;
        i = point / 160;
        i = i-1;
    if(i > fetArray.count-1){
            i = fetArray.count -1;
        }
    if(i == -1)
    {
        i = 0;
    }
    imageView[i].highlighted = YES;
        if([yourArray objectAtIndex:i] isEqualToString:@""]){
            priceLbl.text = @"";
        }
        else {
            priceLbl.text = [NSString stringWithFormat:@"%@", [yourArray objectAtIndex:i]];
        }   
        [scroll addSubview:priceLbl];
    

    // }

    }

    只需设定点 = p.x 和 i = 点;

    如果遇到问题,请告诉我...

    【讨论】:

    • 不,我不希望这样,实际上我在滚动视图中一次显示 3 个图像,所以如果我在位置 1 向左(从右到左)滚动图像,即瀑布图像将移出视图并且新图像将从右侧移入位置 3。
    • 通过 pagecontroller 方法使用滚动视图
    【解决方案3】:

    f 所有图像的大小都相同,您可以添加 2 个按钮(左右)...当您点击其中一个按钮时,将内容偏移的框架更改为一张图片的宽度... 如果您不想要按钮,您可以使用 UISwipeGestureRecognizer 获得相同的效果

    -(IBAction)changeVisibleRect:(UIButton *)btn {
    
    [UIView beginAnimation:@"animation name" context:nil];
    [UIView setAnimationDuration:0.3];  //or how much you want in seconds
    
    [scrollview.contentOffset setFrame:CGRectMake(pictureSize * i, 0, pictureSizeWidth, pictureSizeHeight)]; //i = the number of the picture 
    
    [UIView commitAnimations];
    
    }
    

    CGRectMake(pictureSize * i, 0, pictureSizeWidth, pictureSizeHeight)] ...而不是pictureSize * i ...您可以分配左或右按钮标签并...制作一个

    if(btn.tag == 1) {
    [UIView beginAnimation:@"animation name" context:nil];
    [UIView setAnimationDuration:0.3];  //or how much you want in seconds
    
    [scrollview.contentOffset setFrame:CGRectMake(scrollView.contentOffset.frame.origin.x - pictureSizeWidth, 0, pictureSizeWidth, pictureSizeHeight)]; //i = the number of the picture 
    
    [UIView commitAnimations];
    }
    

    假设你的左键设置了 tag = 1;

    对于 SwipeGesture,它将是相同的动画代码/想法

    【讨论】:

      猜你喜欢
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多