UIScrollView不起作用,只显示一个图像

时间:2014-02-15 04:45:10

标签: uiscrollview rubymotion

我正在尝试创建一个UIScrollView - 但它不起作用。我只在视图中看到一个图像--swipe1。滚动视图向前滚动,但它的空白,我没有看到其他2个图像。

def create_image_scroll_view_NICE
    @scroll_view = UIScrollView.alloc.initWithFrame self.view.bounds
    @scroll_view.delegate = self
    @scroll_view.scrollsToTop = false
    self.view.addSubview @scroll_view

    @scroll_view.pagingEnabled = true
    @scroll_view.showsHorizontalScrollIndicator = false
    @scroll_view.contentSize = CGSizeMake(NUMBER_OF_PAGES * self.view.frame.size.width, self.view.frame.size.height)

    @delivery_image = UIImageView.alloc.initWithImage(UIImage.imageNamed("swipe3"))
    @scroll_view.addSubview @delivery_image

    @notification_image = UIImageView.alloc.initWithImage(UIImage.imageNamed("swipe2"))
    @scroll_view.addSubview @notification_image

    @box_image = UIImageView.alloc.initWithImage(UIImage.imageNamed("swipe1"))
    @scroll_view.addSubview @box_image
  end

2 个答案:

答案 0 :(得分:1)

因为三个图像帧是相同的。 你需要像这样设置框架:

delivery_image.frame = CGRectMake(0,0,width,height);
notification_image.frame = CGRectMake(self.view.frame.size.width,0,width,height);
box_image.frame = CGRectMake(self.view.frame.size.width*2,0,width,height);

答案 1 :(得分:1)

你所有的imageView都会有[0, 0]的原点,所以它们都会重叠。

您需要做的是偏移每个图像的边框,使其显示在上一个图像的旁边

%w(swipe3 swipe2 swipe1).each_with_index do |image_name, index|
  delivery_image = UIImageView.alloc.initWithImage(UIImage.imageNamed(image_name))

  x_offset = CGRectGetWidth(self.view.frame) * index
  delivery_image.frame = [[ x_offset, 0], delivery_image.frame.size ]

  @scroll_view.addSubview delivery_image
end