私はUIPageViewControllerを使用して画像をスワイプしています。ビューコントローラーには小さなイメージセットがあり、大きなイメージのみを表示する別のビューコントローラーにセグメントします。私はスクロールビューでページビューコントローラを作成し、それは正常に動作しますが、常に最初の画像から開始します。最初のコントローラーに5つの小さな画像があるとしましょう。ユーザーが3番目の画像をタップして大きい画像を見ると、2番目のコントローラーが大きな画像を表示するセグが成功し、現在のページも3に設定されているとしましょう画像は最初の画像です。私はこの2番目のコントローラにsegueingするとき、私はイメージのURLを含む配列を渡し、最初のビューでクリックされたインデックスに基づいて現在のページが設定されていますが、イメージは配列の最初のイメージです。SwiftでUIPageViewControllerを使用して特定のページに直接ジャンプ
どのように私はこの問題を解決することができますか?以下のコードされています
import UIKit
class ImageViewerViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!
var seguedAlbumImagePathArray=[]
var seguedCurrentImageIndex: Int?
var totalPages: Int = 0
//let sampleBGColors: Array<UIColor> = [UIColor.redColor(), UIColor.yellowColor(), UIColor.greenColor(), UIColor.magentaColor(), UIColor.orangeColor()]
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
totalPages = seguedAlbumImagePathArray.count
view.bringSubviewToFront(pageControl)
}
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() {
// 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.
let 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)"
// Set the proper message to the test view's label.
let imagev = imageView.viewWithTag(2) 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
view.sendSubviewToBack(imagev)
}
else {
ImageLoader.sharedLoader.imageForUrl(seguedAlbumImagePathArray[i] as! String, completionHandler:{(image: UIImage?, url: String) in
imagev.image = image
})
}
// Add the test view as a subview to the scrollview.
scrollView.addSubview(imageView)
}
}
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);
// 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)
}
}