swift - 如何在多个页面中快速缩放图像
Posted
技术标签:
【中文标题】swift - 如何在多个页面中快速缩放图像【英文标题】:How to zoom images when they are in multiple pages in swift 【发布时间】:2016-07-30 05:07:10 【问题描述】:我有一个主滚动视图,在该主滚动视图中我有一个具有 UIImageView 的子滚动视图。我在正确缩放滚动视图时遇到问题。每当我尝试放大图像时,视图只是四处移动,但图像没有放大。我不知道我做错了什么?有人可以帮我看看有什么问题吗?这是我的代码:
import UIKit
class ImageViewerViewController: UIViewController, UIScrollViewDelegate
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!
var tap: UITapGestureRecognizer?
var seguedAlbumImagePathArray=[]
var seguedCurrentImageIndex: Int?
var imageView: UIView?
var childScrollView: UIScrollView?
var imagev: UIImageView?
var totalPages: Int = 0
override func shouldAutorotate() -> Bool
return false
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask
return UIInterfaceOrientationMask.Portrait
override func viewDidLoad()
super.viewDidLoad()
totalPages = seguedAlbumImagePathArray.count
view.bringSubviewToFront(pageControl)
tap = UITapGestureRecognizer(target: self, action: #selector(ImageViewerViewController.dismissImageViewer))
scrollView.addGestureRecognizer(tap!)
func dismissImageViewer()
UIView.animateWithDuration(0.2, animations: () -> Void in
self.scrollView.alpha=0
)
NSTimer.scheduledTimerWithTimeInterval(0.13, target: self, selector: #selector(self.dismissView), userInfo: nil, repeats: true)
func dismissView()
self.presentingViewController?.dismissViewControllerAnimated(false, completion:
let secondPresentingVC = self.presentingViewController?.presentingViewController;
secondPresentingVC?.dismissViewControllerAnimated(false, completion: )
)
override func viewDidAppear(animated: Bool)
super.viewDidAppear(animated)
configureScrollView()
configurePageControl()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
override func preferredStatusBarStyle() -> UIStatusBarStyle
return UIStatusBarStyle.LightContent
// MARK: Custom method implementation
func configureScrollView()
self.scrollView.backgroundColor = UIColor.blackColor()
// Enable paging.
scrollView.pagingEnabled = true
// Set the following flag values.
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.scrollsToTop = false
// Set the scrollview content size.
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(totalPages), scrollView.frame.size.height)
// Set self as the delegate of the scrollview.
scrollView.delegate = self
// Load the ImageView view from the ImageView.xib file and configure it properly.
for i in 0 ..< totalPages
// Load the ImageView view.
imageView = NSBundle.mainBundle().loadNibNamed("ImageView", owner: self, options: nil)[0] as! UIView
imageView!.backgroundColor = UIColor.blackColor()
// Set its frame and the background color.
imageView!.frame = CGRectMake(CGFloat(i) * scrollView.frame.size.width, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)
//imageView.backgroundColor = sampleBGColors[i]
// Set the proper message to the test view's label.
let label = imageView!.viewWithTag(1) as! UILabel
label.text = "\(i + 1)/\(totalPages)"
childScrollView = imageView!.viewWithTag(2) as? UIScrollView
childScrollView?.minimumZoomScale=1.0
childScrollView?.maximumZoomScale=6.0
childScrollView?.delegate = self
imagev = childScrollView!.viewWithTag(3) as? UIImageView
view.sendSubviewToBack(imagev!)
if let imageData = NSUserDefaults.standardUserDefaults().objectForKey(seguedAlbumImagePathArray[i] as! String),
let imageToDisplay = UIImage(data: imageData as! NSData)
imagev!.image = imageToDisplay
else
ImageLoader.sharedLoader.imageForUrl(seguedAlbumImagePathArray[i] as! String, completionHandler:(image: UIImage?, url: String) in
self.imagev!.image = image
)
var frame: CGRect = scrollView.frame
frame.origin.x = frame.size.width * CGFloat(seguedCurrentImageIndex!)
frame.origin.y = 0
scrollView.scrollRectToVisible(frame, animated: false)
scrollView.addSubview(imageView!)
//scrollView.setContentOffset(CGPoint, animated: true)
print(scrollView.contentOffset)
func configurePageControl()
// Set the total pages to the page control.
pageControl.numberOfPages = totalPages
// Set the initial page.
pageControl.currentPage = seguedCurrentImageIndex!
// MARK: UIScrollViewDelegate method implementation
func scrollViewDidScroll(scrollView: UIScrollView)
// Calculate the new page index depending on the content offset.
let currentPage = floor(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width);
//1.025 = 328/320
// Set the new page index to the page control.
pageControl.currentPage = Int(currentPage)
// MARK: IBAction method implementation
@IBAction func changePage(sender: AnyObject)
// Calculate the frame that should scroll to based on the page control current page.
var newFrame = scrollView.frame
newFrame.origin.x = newFrame.size.width * CGFloat(pageControl.currentPage)
scrollView.scrollRectToVisible(newFrame, animated: true)
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
return self.imagev
【问题讨论】:
【参考方案1】:当我将滚动视图放在 UIcontrolViewCONtroller 下时,我遇到了类似的问题。 删除这个 -
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
return self.imagev
并使用
func viewForZooming( in scrollView: UIScrollView) -> UIView?
return imgview
这对我有用。
【讨论】:
以上是关于swift - 如何在多个页面中快速缩放图像的主要内容,如果未能解决你的问题,请参考以下文章
如何在启用分页的 UIScrollView 中延迟加载 100 多个页面?
如何使在Swift中使用UIScrollView进行滚动和缩放