scrollview将图像堆叠在一起--xcode

时间:2012-07-26 11:58:32

标签: xcode image scrollview

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

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

1 个答案:

答案 0 :(得分:0)

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

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);