我有一个使用2个视图控制器(视图控制器和pageItemController)创建的滑动视图页面和1个pageViewController,如屏幕截图所示。
我的滑动页面中有4个图片及其数组。所以我的滑动页面包含4页 我想要的是当我在滑动视图中点击每个图像时转到新页面。我在pageItemController的imageView中添加了UIgestureRecognizer,并在pageItemController中使用了这段代码:
import UIKit
class PageItemController: UIViewController {
var itemIndex = Int!()
var imageName: String = "" {
didSet {
if let imageView = contentImageView {
imageView.image = UIImage(named: imageName)
}
}
}
@IBOutlet var contentImageView: UIImageView?
@IBOutlet weak var gameLevel: UIImageView!
func imageTapped(gestureRecognizer: UITapGestureRecognizer)
{
let targetImageIndex = itemIndex! as Int
let gameLevel = self.storyboard?.instantiateViewControllerWithIdentifier("GameLevelPack") as! GameLevel
let gameLevelPack2 = self.storyboard?.instantiateViewControllerWithIdentifier("GameLevelPack2") as! GameLevelPack2
let gameLevelPack3 = self.storyboard?.instantiateViewControllerWithIdentifier("GameLevelPack3") as! GameLevelPack3
let gamelevelPack4 = self.storyboard?.instantiateViewControllerWithIdentifier("GameLevelPack4") as! GameLevelPack4
switch (targetImageIndex) {
case 0:
self.navigationController?.pushViewController(gameLevel, animated: true)
break;
case 1:
self.navigationController?.pushViewController(gameLevelPack2, animated: true)
break;
case 2:
self.navigationController?.pushViewController(gameLevelPack3, animated: true)
break;
case 3:
self.navigationController?.pushViewController(gamelevelPack4, animated: true)
break;
default:
break;
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor (red: 100, green: 100, blue: 100, alpha: 0)
let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
contentImageView!.userInteractionEnabled = true
contentImageView!.addGestureRecognizer(tapGestureRecognizer)
contentImageView!.image = UIImage(named: imageName)
}
}
我在viewController中使用了这段代码:
import UIKit
class ViewController: UIViewController, UIPageViewControllerDataSource {
private var pageViewController: UIPageViewController?
private let contentImages = ["pack1.png",
"pack2.png",
"pack3.png",
"pack4.png"];
override func viewDidLoad() {
super.viewDidLoad()
createPageViewController()
setupPageControl()
}
private func createPageViewController() {
let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
pageController.dataSource = self
if contentImages.count > 0 {
let firstController = getItemController(0)!
let startingViewControllers: NSArray = [firstController]
pageController.setViewControllers(startingViewControllers as? [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
pageViewController = pageController
addChildViewController(pageViewController!)
self.view.addSubview(pageViewController!.view)
pageViewController!.didMoveToParentViewController(self)
}
private func setupPageControl() {
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor (red: 100, green: 100, blue: 100, alpha: 0)
appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
appearance.backgroundColor = UIColor (red: 100, green: 100, blue: 100, alpha: 0)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let itemController = viewController as! PageItemController
if itemController.itemIndex > 0 {
return getItemController(itemController.itemIndex-1)
}
return nil
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let itemController = viewController as! PageItemController
if itemController.itemIndex+1 < contentImages.count {
return getItemController(itemController.itemIndex+1)
}
return nil
}
private func getItemController(itemIndex: Int) -> PageItemController? {
if itemIndex < contentImages.count {
let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as! PageItemController
pageItemController.itemIndex = itemIndex
pageItemController.imageName = contentImages[itemIndex]
return pageItemController
}
return nil
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return contentImages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
使用这些代码,当我点击滑动图像时,我没有动作。 请告诉我如何解决?