【问题标题】:UIImageView doesn't follow origin.x positionUIImageView 不遵循 origin.x 位置
【发布时间】:2011-12-28 15:20:07
【问题描述】:

我正在尝试在 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...] 的原因。

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

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

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

【问题讨论】:

  • 我不确定这是否是导致问题的原因,但在我看来,您的视图控制器有一个名为 scrollView 的属性(因为您在某一时刻使用 self.scrollView )并带有支持iVar 也称为滚动视图。当您初始化 scrollView 时,您使用支持 iVar 而不是属性,这意味着它不会被保留。如果您尝试在拥有 scrollView 的任何地方使用 self.scrollView 会发生什么?
  • 听起来你正在尝试制作凝胶,为什么不使用existing galleries
  • @Darren 我尝试使用 self.scrollView,它似乎没有改变任何东西。我的图像仍然重叠。
  • @AdilSoomro 抱歉,我想试试那个照片查看器,但也许可以用于我的下一个应用程序。我想先实现我自己的,因为我还在这里添加了功能。不过非常感谢。
  • 您在哪里调用问题中的代码?它看起来像是在视图控制器的 viewDidLoad 方法中调用的东西,但你有 self.frame 和 [self addSubview:scrollView]。这实际上是您的代码中的 self.view.frame 和 [self.view addSubview:scrollView] 吗?此外,您是否尝试将 imageViews 的不透明度设置为小于 1 以验证它们实际上位于最后一个视图的下方?并且您的 scrollView.contentSize 是否始终正确,即。您可以滚动整个宽度,但是除了前 320 像素之外到处都看到黑色背景?

标签: iphone ios uiscrollview uiimageview


【解决方案1】:

这可能与自动调整掩码有关。实际图片的宽度,我的意思是.png文件大于320,对吧?

【讨论】:

  • 不,抱歉。对于视网膜显示,我的图像正好是 320x480 和 640x960。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-21
  • 2020-11-18
  • 1970-01-01
  • 2015-03-01
  • 2017-06-29
  • 1970-01-01
相关资源
最近更新 更多