【问题标题】:scrollview stacks images on top of each other -xcode滚动视图将图像堆叠在一起 -xcode
【发布时间】:2012-07-26 11:58:31
【问题描述】:

我有一个带有子视图作为图像的滚动视图。当我将图片中心设置为滚动视图时,它们都作为堆栈显示在彼此的顶部。我需要它们水平滚动。我该如何解决这个问题?这是我到目前为止的代码..

- (void)viewDidLoad
{

    [super viewDidLoad];

    for (int i = 1; i < 18; i++) {
        UIImageView *images = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat:@"%d.jpg", i]]];

       images.frame = CGRectMake((i-1)*320, 0, 320, 330);

        [scroller setContentOffset:CGPointMake(0, 0)];
        [scroller addSubview:images];
        [images setContentMode:UIViewContentModeScaleAspectFill];
        [images sizeToFit];

        //center image
        CGSize boundsSize = scroller.bounds.size;
        CGRect frameToCenter = images.frame;

        // center horizontally
        if (frameToCenter.size.width < boundsSize.width) {
            frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
            NSLog(@"%f", frameToCenter.origin.x);
        }
        else
            frameToCenter.origin.x = 0;

        // center vertically
        if (frameToCenter.size.height < boundsSize.height)
            frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
        else
            frameToCenter.origin.y = 0;

        images.frame = frameToCenter;

        scroller.pagingEnabled = NO;


    }


    [scroller setContentMode:UIViewContentModeScaleAspectFit];

    scroller.delegate =self;
    scroller.contentSize = CGSizeMake(320*17, 330);
    scroller.backgroundColor = [UIColor blackColor];
    pageControl.numberOfPages =17;
    pageControl.currentPage = 0;

【问题讨论】:

  • 为了吸引更多答案,您应该添加与您正在使用的语言或工具相对应的标签。您可以通过editing your question 并在编辑页面底部添加标签来做到这一点。
  • 您使用的是什么平台?你是如何将所说的子视图居中的?你能显示一些代码吗?

标签: xcode image scrollview


【解决方案1】:

要使图像水平居中,只需使用下面的代码

float scrollHeight = 0;    
    for (int col = 0; col < index; ++col)
        {
            CGRect selectFrame = CGRectMake(0,0,kOvrW,kOvrH);
            selectionIndicator = [[UIImageView alloc] initWithFrame:selectFrame];
            selectionIndicator.contentMode = UIViewContentModeBottomRight;
            [selectionIndicator setImage:[UIImage imageNamed:@"checked"]];
            CGRect frame = CGRectMake(5,kOvrPreY+col*(kOvrPreH+kOvrH),kOvrW,kOvrH);
            UIView *fr = [[UIView alloc] initWithFrame:frame];

            CGRect imgFrame = CGRectMake(5, 4, 68, 55);
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:imgFrame];
            imgView.image = [UIImage imageNamed:[[subCategories objectAtIndex:col] valueForKey:@"add"]];

            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            tapGesture.numberOfTapsRequired = 1;
            tapGesture.numberOfTouchesRequired = 1;
            [fr setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"small-thumb"]]];
            [fr addGestureRecognizer:tapGesture]; 
            [fr addSubview:imgView];
            fr.tag = col;
            [subCatScroll addSubview:fr];

            [subCatScroll bringSubviewToFront:fr];
            scrollHeight += kOvrPreH+kOvrH;
        }
    subCatScroll.contentSize = CGSizeMake(subCatScroll.frame.size.width, scrollHeight+kOvrPreH);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2021-01-12
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多