scrollView图像不居中

时间:2014-09-25 13:33:09

标签: ios iphone swift uiscrollview

我试图用UIPageControl创建一个scrollView。这工作正常,但问题是图像不会在scrollView的每个页面中居中。它与右边对齐。我怎样才能使图像居中?

图片插图:

http://i.stack.imgur.com/In6t7.png

代码:

viewDidLoad中

self.pageControl = UIPageControl()
self.pageControl?.frame = CGRectZero
self.pageControl?.currentPage = 0
self.pageControl?.numberOfPages = self.imageArray!.count
self.pageControl?.tintColor = UIColor.whiteColor()
self.pageControl?.userInteractionEnabled = false
self.navigationItem.titleView = self.pageControl?

for var i = 0; i<self.imageArray?.count; i++ {
    self.pageViews.append(nil)
}

let pagesScrollViewSize = self.scrollView.frame.size
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(self.imageArray!.count), pagesScrollViewSize.height)


self.loadVisiblePages()

其余方法

func loadPage(page: Int) {

    if page < 0 || page >= self.imageArray!.count {
        // If it's outside the range of what you have to display, then do nothing
        return
    }

    // 1
    if let pageView = pageViews[page] {
        // Do nothing. The view is already loaded.
    } else {
        // 2
        let newPageView = UIImageView(image: self.imageArray![page] as UIImage)
        var frame = scrollView.bounds
        frame.origin.x = (frame.size.width * CGFloat(page))
        frame.origin.y = 0.0

        // 3

        newPageView.contentMode = .ScaleAspectFit
        newPageView.frame = frame
        scrollView.addSubview(newPageView)
        newPageView.backgroundColor = UIColor.blackColor()

        // 4
        pageViews[page] = newPageView
    }
}

func purgePage(page: Int) {

    if page < 0 || page >= self.imageArray!.count {
        // If it's outside the range of what you have to display, then do nothing
        return
    }

    // Remove a page from the scroll view and reset the container array
    if let pageView = pageViews[page] {
        pageView.removeFromSuperview()
        pageViews[page] = nil
    }

}

func loadVisiblePages() {

    // First, determine which page is currently visible
    let pageWidth = scrollView.frame.size.width
    let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))

    // Update the page control
    pageControl?.currentPage = page

    // Work out which pages you want to load
    let firstPage = page - 1
    let lastPage = page + 1


    // Purge anything before the first page
    for var index = 0; index < firstPage; ++index {
        purgePage(index)
    }

    // Load pages in our range
    for var index = firstPage; index <= lastPage; ++index {
        loadPage(index)
    }

    // Purge anything after the last page
    for var index = lastPage+1; index < self.imageArray!.count; ++index {
        purgePage(index)
    }
}


func scrollViewDidScroll(scrollView: UIScrollView!) {
    // Load the pages that are now on screen
    loadVisiblePages()
}

2 个答案:

答案 0 :(得分:0)

我在此处发现您使用let来声明常量UIImageView,我认为您应该使用var

let newPageView = UIImageView(image: self.imageArray![page] as UIImage)

实际上,除了这个之外我还能找到任何问题,我认为你不能将图像设置到中心的原因是因为这个声明之后的所有操作都不起作用,所以你不能设置正确的框架,正确的内容模式为UIImageView

答案 1 :(得分:0)

在定位scrollView的图片视图时,如果使用center.x而不是origin.x,可能会更容易。

我将在这种情况下使用的公式:

centerX = pageNumber * 320 + 160

类似于:

myImageView.center = CGPointMake(pageNumber * 320 + 160, 0);

通过我的公式,我得到了这些结果:

page 0: 0 * 320 + 160 = 160
page 1: 1 * 320 + 160 = 480
page 2: 2 * 320 + 160 = 800
page 3: 3 * 320 + 160 = 1120

您可以看到每个页面的偏移量为320(假设此处的iPhone屏幕尺寸)。你显然会改用它:

self.view.bounds.size.width 

这里的320用于说明目的。

您的方法

好的,让我们来看看你的代码:

var frame = scrollView.bounds
frame.origin.x = (frame.size.width * CGFloat(page))
frame.origin.y = 0.0
  1. 如果页面从0开始,则origin.x将为0,scrollView的宽度保持为320。
  2. 第二次,origin.x将是320 * 1(这里是页面== 1),scrollView的宽度将变为640,因为你有2个我认为是320宽的imageViews。
  3. 第三次,origin.x将是640 * 2 = 1280,scrollView的宽度现在是1600(origin.x = 1280 + 320 imageView宽度)
  4. 你看到数字如何变得更大而不是常数320?