UIImageView不遵循origin.x的位置

时间:2011-12-28 15:20:07

标签: iphone ios uiscrollview uiimageview

我正在尝试在UIScrollView上添加一组图像视图。我的问题是即使我增加x值,图像视图也相互重叠。还有一点很奇怪,这个问题仅适用于配备ios 4.2.1的设备,但适用于模拟器4.2和其他设备ios。

这是我的代码:

- (void)addScrollView {

      [self setImages];

      scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
      scrollView.delegate = self;

      [scrollView setBackgroundColor:[UIColor blackColor]];
      [scrollView setCanCancelContentTouches:NO];

      scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
      scrollView.clipsToBounds = YES;
      scrollView.scrollEnabled = YES;
      scrollView.pagingEnabled = YES;

      [self addSubview:scrollView];

      NSUInteger nimages;
      CGFloat cx = 0;

      for (nimages = 1; nimages < [imagesArray count] ; nimages++) {
        UIImage *image = [UIImage imageNamed:[imagesArray objectAtIndex:nimages]];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];


        CGRect rect = imageView.frame;
        rect.origin.x = cx;
        rect.origin.y = 0;
        rect.size.width = 320;
        rect.size.height = 480;

        imageView.frame = rect;

        [scrollView addSubview:imageView];

        [imageView release];

        cx += rect.size.width;

      }

      [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
 }

从这里打来:

- (id)initWithFrame:(CGRect)frame {

     self = [super initWithFrame:frame];

     if (self) {

            [self addScrollView];

            UIImage *handImg;

            UIImageView *longhand = [[UIImageView alloc] init];
            minuteHand = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 320, 480)];

            handImg = [UIImage imageNamed:@"clock_long_hand.png"];
            longhand.image = handImg;

                    /************** TRY COMMENT OUT HERE **************/

            longhand.frame = CGRectMake(155 - (handImg.size.width / 2), 240 - handImg.size.height, handImg.size.width, handImg.size.height);
                [minuteHand addSubview:longhand];
            [self addSubview:minuteHand];

                    /************** END COMMENT OUT HERE **************/

            [longhand release];
   }
   return self;
}   

更新:

此代码位于UIView子类中,这就是我使用[self ...]而不是[self.view ...]的原因。

我试过试验,发现当我注释掉指定的部分时,scrollview工作正常。但是当它被包含时,这是图像视图重叠的时候。

我知道scrollview的内容大小是正确的,因为它的指示器继续滚动,只有一个纯黑色的背景。

我真的很困惑为什么这适用于某些ios而不适用于其他ios。希望你能帮忙。

1 个答案:

答案 0 :(得分:1)

这可能与自动调整遮罩有关。宽度实际图像,我的意思是.png文件大于320,对吗?