У меня есть основной вид прокрутки и в этом главном представлении прокрутки У меня есть детальный прокрутка, у которого есть UIImageView. У меня возникла проблема с масштабированием вида прокрутки. Всякий раз, когда я пытаюсь увеличить изображение, вид просто перемещается, но изображение не масштабируется. Я не знаю, что я делаю неправильно? Может кто-то, пожалуйста, помогите мне понять, что случилось? Вот мой код:Как увеличить изображение, когда они находятся на нескольких страницах в swift
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
}
}