gpt4 book ai didi

以编程方式快速进行 IOS 图像查看器

转载 作者:可可西里 更新时间:2023-11-01 01:43:52 26 4
gpt4 key购买 nike

我正在尝试使用这个教程制作一个 imageViewer this toturial

我想使用“PagedScrollViewController”示例并使用 swift 实现它,我能够在屏幕上显示图像,但滚动有问题。

滚动不会在页面/照片之间停止,它会自由滚动,因此 loadVisiblePages 总是调用我滚动的每个像素。

可能我遗漏了什么,你能帮我解决这个问题吗?这是我正在做的:

谢谢

import UIKit

class PagedScrollViewController:UIViewController,UIScrollViewDelegate {


var pageImages:[UIImage] = [UIImage]()
var pageViews:[UIView?] = [UIView]()

var scrollView:UIScrollView = UIScrollView()
var pageControl:UIPageControl = UIPageControl()


func loadVisiblePages() {
// First, determine which page is currently visible
var pageWidth:CGFloat = self.scrollView.frame.size.width;

var page = Int(floor((self.scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)));

// Update the page control
self.pageControl.currentPage = page;

// Work out which pages we want to load
var firstPage = page - 1;
var lastPage = page + 1;

// Purge anything before the first page
for (var i=0; i<firstPage; i++) {
println("1) purge index \(i)")
self.purgePage(i)
}
for (var i=firstPage; i<=lastPage; i++) {
println("2) load index \(i)")
self.loadPage(i)
}
for (var i = lastPage+1 ; i < self.pageImages.count ; i++) {
println("3) purge index \(i)")
self.purgePage(i)
}
}
//
func loadPage(page:Int) {
if page < 0 || page >= self.pageImages.count {
// If it's outside the range of what we have to display, then do nothing
return;
}

// Load an individual page, first seeing if we've already loaded it
var pageView:UIView? = self.pageViews[page];
if pageView == nil {
var frame:CGRect = self.scrollView.bounds;
//frame.origin.x = frame.size.width * CGFloat(page)
frame.origin.x = 320.0 * CGFloat(page)
frame.origin.y = 0.0

var newPageView:UIImageView = UIImageView(image: self.pageImages[page])
newPageView.contentMode = UIViewContentMode.ScaleAspectFit;
newPageView.frame = frame;
self.scrollView.addSubview(newPageView)
self.pageViews[page] = newPageView
}
}

func purgePage(page:Int) {
if page < 0 || page >= self.pageImages.count {
// If it's outside the range of what we have to display, then do nothing
return;
}

// Remove a page from the scroll view and reset the container array
var pageView:UIView? = self.pageViews[page];
if pageView != nil {
pageView?.removeFromSuperview()
self.pageViews[page] = UIView()//nil
}
}




override func viewDidLoad() {
super.viewDidLoad()


self.title = "Paged";

self.view.backgroundColor = UIColor.blueColor()

// Set up the image we want to scroll & zoom and add it to the scroll view
self.pageImages.append(UIImage(named: "first.png"))
self.pageImages.append(UIImage(named: "second.png"))
self.pageImages.append(UIImage(named: "first.png"))
self.pageImages.append(UIImage(named: "second.png"))
self.pageImages.append(UIImage(named: "first.png"))

var pageCount = self.pageImages.count

self.scrollView.delegate = self
//self.tableView.showsHorizontalScrollIndicator = false
//self.tableView.showsVerticalScrollIndicator = false

// Set up the page control
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;

self.pageControl.setTranslatesAutoresizingMaskIntoConstraints(false)
self.scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)

self.view.addSubview(self.pageControl)
self.view.addSubview(self.scrollView)


//Set layout
var viewsDict = Dictionary <String, UIView>()
viewsDict["control"] = self.pageControl;
viewsDict["scrollView"] = self.scrollView;

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[control]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView(400)]-[control]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))


// Set up the array to hold the views for each page
for (var i = 0; i < pageCount; ++i) {
self.pageViews.append(nil)

}
}


override func viewDidAppear(animated:Bool) {
super.viewDidAppear(animated)

// Set up the content size of the scroll view
var pagesScrollViewSize:CGSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(self.pageImages.count), pagesScrollViewSize.height);

// Load the initial set of pages that are on screen
self.loadVisiblePages()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

// self.scrollView = nil
// self.pageControl = nil
// self.pageImages = nil
// self.pageViews = nil
}

func scrollViewDidScroll(scrollView:UIScrollView ) {
// Load the pages which are now on screen
self.loadVisiblePages()
println("scrollViewDidScroll")
}



}

最佳答案

问题是我没有将“self.scrollView.pagingEnabled”设置为 true。我现在仍然在使用 UIKit。无论如何我做了一些性能改进,现在只在需要时调用布局,希望有人会发现这个 usfull :)感谢http://www.raywenderlich.com

import UIKit

class PagedScrollViewController:UIViewController,UIScrollViewDelegate {

var pageImages:[UIImage] = [UIImage]()
var pageViews:[UIView?] = [UIView]()

var scrollView:UIScrollView = UIScrollView()
var pageControl:UIPageControl = UIPageControl()

var viewingPage = -1

func loadVisiblePages() {
// First, determine which page is currently visible
var pageWidth:CGFloat = self.scrollView.frame.size.width;
var page = Int(floor((self.scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)));

/*
Check that page have changed,
in case that user drag left in first page, or drag right in last page
a 'scrollViewDidEndDecelerating' is fired
*/
if viewingPage != page {

// Update the page control
self.pageControl.currentPage = page;

// Work out which pages we want to load
var firstPage = page - 1;
var lastPage = page + 1;

// Purge anything before the first page
for (var i=0; i<firstPage; i++) {
self.purgePage(i)
}
for (var i=firstPage; i<=lastPage; i++) {
self.loadPage(i)
}
for (var i = lastPage+1 ; i < self.pageImages.count ; i++) {
self.purgePage(i)
}

viewingPage = page
}

}

func loadPage(page:Int) {
if page < 0 || page >= self.pageImages.count {
// If it's outside the range of what we have to display, then do nothing
return;
}

// Load an individual page, first seeing if we've already loaded it
var pageView:UIView? = self.pageViews[page];
if pageView == nil {
var frame:CGRect = self.scrollView.bounds;
//frame.origin.x = frame.size.width * CGFloat(page)
frame.origin.x = 320.0 * CGFloat(page)
frame.origin.y = 0.0

var newPageView:UIImageView = UIImageView(image: self.pageImages[page])
newPageView.contentMode = UIViewContentMode.ScaleAspectFit;
newPageView.frame = frame;
self.scrollView.addSubview(newPageView)
self.pageViews[page] = newPageView
}
}

func purgePage(page:Int) {
if page < 0 || page >= self.pageImages.count {
// If it's outside the range of what we have to display, then do nothing
return;
}

// Remove a page from the scroll view and reset the container array
var pageView:UIView? = self.pageViews[page];
if pageView != nil {
pageView?.removeFromSuperview()
self.pageViews[page] = nil
}
}

override func viewDidLoad() {
super.viewDidLoad()

self.title = "Image viewer";

self.view.backgroundColor = UIColor.blueColor()

// Set up the image we want to scroll & zoom and add it to the scroll view
self.pageImages.append(UIImage(named: "message_full.png"))
self.pageImages.append(UIImage(named: "heart_full"))
self.pageImages.append(UIImage(named: "star_full.png"))
self.pageImages.append(UIImage(named: "second.png"))
self.pageImages.append(UIImage(named: "first.png"))

var pageCount = self.pageImages.count

self.scrollView.pagingEnabled = true
self.scrollView.delegate = self
self.scrollView.showsHorizontalScrollIndicator = false
self.scrollView.showsVerticalScrollIndicator = false

// Set up the page control
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;

self.pageControl.setTranslatesAutoresizingMaskIntoConstraints(false)
self.scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)

self.view.addSubview(self.pageControl)
self.view.addSubview(self.scrollView)


//Set layout
var viewsDict = Dictionary <String, UIView>()
viewsDict["control"] = self.pageControl;
viewsDict["scrollView"] = self.scrollView;

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[control]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView(400)]-[control]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))

// Set up the array to hold the views for each page
for (var i = 0; i < pageCount; ++i) {
self.pageViews.append(nil)
}
}


override func viewDidAppear(animated:Bool) {
super.viewDidAppear(animated)

// Set up the content size of the scroll view
var pagesScrollViewSize:CGSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(self.pageImages.count), pagesScrollViewSize.height);

// Load the initial set of pages that are on screen
self.loadVisiblePages()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//TODO make cleaning
// self.scrollView = nil
// self.pageControl = nil
// self.pageImages = nil
// self.pageViews = nil
}


func scrollViewDidEndDecelerating(scrollView: UIScrollView!) {
self.loadVisiblePages()
}

}

关于以编程方式快速进行 IOS 图像查看器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25632330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com