禁用和启用页面滚动 (Swift)
Posted
技术标签:
【中文标题】禁用和启用页面滚动 (Swift)【英文标题】:Disabling and enabling Page Scrolling (Swift) 【发布时间】:2016-08-16 16:44:03 【问题描述】:我正在使用以下代码来构建 PageViewController。 我的问题是如何在“前”和“后”视图控制器中禁用或启用页面滚动?
class PageController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate
var pages = [UIViewController]()
override func viewDidLoad()
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let page1: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("Front")
let page2: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("Back")
pages.append(page1)
pages.append(page2)
setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
let currentIndex = pages.indexOf(viewController)!
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
let currentIndex = pages.indexOf(viewController)!
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
return pages.count
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
return 0
【问题讨论】:
【参考方案1】:试试这个代码:
ViewController.swift
import UIKit
class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate
var pages = [UIViewController]()
var pageContollerEnabled = true
override func viewDidLoad()
super.viewDidLoad()
self.delegate = self
self.dataSource = self
if let viewController = createViewController("Front")
pages.append(viewController)
viewController.tag = pages.count
setViewControllers([viewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
if let viewController = createViewController("Back")
pages.append(viewController)
viewController.tag = pages.count
private func createViewController (identifier:String) -> ViewController?
let viewController = storyboard?.instantiateViewControllerWithIdentifier(identifier)
if let viewController = viewController as? ViewController
viewController.pageViewController = self
return viewController
return nil
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
if (pageContollerEnabled)
let currentIndex = pages.indexOf(viewController)!
let previousIndex = abs((currentIndex - 1) % pages.count)
if let previousViewController = pages[previousIndex] as? ViewController
if previousViewController.tag <= currentIndex
return pages[previousIndex]
return nil
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
if (pageContollerEnabled)
let currentIndex = pages.indexOf(viewController)!
let nextIndex = abs((currentIndex + 1) % pages.count)
if let nextViewController = pages[nextIndex] as? ViewController
if nextViewController.tag > currentIndex
return pages[nextIndex]
return nil
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
return pages.count
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
return 0
ViewController.swift
import UIKit
class ViewController: UIViewController
var pageViewController: PageViewController?
@IBAction func valueChanged(sender: UISwitch)
NSLog("Swith \(sender.on)")
if let pageViewController = self.pageViewController
pageViewController.pageContollerEnabled = sender.on
Main.storyboard
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="ios.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="uMa-bV-FKZ">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--Page View Controller-->
<scene sceneID="v41-UK-OxZ">
<objects>
<pageViewController autoresizesArchivedViewToFullSize="NO" transitionStyle="pageCurl" navigationOrientation="horizontal" spineLocation="min" id="uMa-bV-FKZ" customClass="PageViewController" customModule="***_38980301" customModuleProvider="target" sceneMemberID="viewController"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="iKI-ej-UeC" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="424" y="356"/>
</scene>
<!--View Controller-->
<scene sceneID="rJU-DG-wgp">
<objects>
<viewController storyboardIdentifier="Front" useStoryboardIdentifierAsRestorationIdentifier="YES" id="xxX-RS-ZVZ" customClass="ViewController" customModule="***_38980301" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="c8s-ud-821"/>
<viewControllerLayoutGuide type="bottom" id="JbG-NZ-eP5"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="rpm-D4-Tqn">
<rect key="frame" x="0.0" y="0.0" />
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Front" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="MhC-PV-UBQ">
<rect key="frame" x="280" y="289" />
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="OaO-rT-mzv">
<rect key="frame" x="276" y="318" />
<connections>
<action selector="valueChanged:" destination="xxX-RS-ZVZ" eventType="valueChanged" id="lUx-Cz-lJl"/>
</connections>
</switch>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="OaO-rT-mzv" firstAttribute="centerX" secondItem="MhC-PV-UBQ" secondAttribute="centerX" id="3U1-6y-cKV"/>
<constraint firstItem="MhC-PV-UBQ" firstAttribute="centerY" secondItem="rpm-D4-Tqn" secondAttribute="centerY" id="Bge-Ih-jR4"/>
<constraint firstItem="OaO-rT-mzv" firstAttribute="top" secondItem="MhC-PV-UBQ" secondAttribute="bottom" constant="8" symbolic="YES" id="gTI-yU-mMe"/>
<constraint firstItem="MhC-PV-UBQ" firstAttribute="centerX" secondItem="rpm-D4-Tqn" secondAttribute="centerX" id="rJq-ad-5Ur"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="vQZ-k5-cBN" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1078" y="356"/>
</scene>
<!--View Controller-->
<scene sceneID="636-Kw-KXs">
<objects>
<viewController storyboardIdentifier="Back" useStoryboardIdentifierAsRestorationIdentifier="YES" id="ICA-gO-3et" customClass="ViewController" customModule="***_38980301" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="z7N-pb-J88"/>
<viewControllerLayoutGuide type="bottom" id="wrb-EO-FlO"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="XiV-R4-9yG">
<rect key="frame" x="0.0" y="0.0" />
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Back" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="JHB-ln-wGN">
<rect key="frame" x="281" y="289" />
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="MLj-y6-3eJ">
<rect key="frame" x="276" y="318" />
<connections>
<action selector="valueChanged:" destination="ICA-gO-3et" eventType="valueChanged" id="ae6-jj-HHQ"/>
</connections>
</switch>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="JHB-ln-wGN" firstAttribute="centerX" secondItem="MLj-y6-3eJ" secondAttribute="centerX" id="KOk-1n-svY"/>
<constraint firstItem="JHB-ln-wGN" firstAttribute="centerY" secondItem="XiV-R4-9yG" secondAttribute="centerY" id="LQa-xT-oAG"/>
<constraint firstItem="MLj-y6-3eJ" firstAttribute="top" secondItem="JHB-ln-wGN" secondAttribute="bottom" constant="8" symbolic="YES" id="WzC-ed-F8F"/>
<constraint firstItem="JHB-ln-wGN" firstAttribute="centerX" secondItem="XiV-R4-9yG" secondAttribute="centerX" id="tte-Vg-fy5"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Q0j-fT-PZ9" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1740" y="356"/>
</scene>
</scenes>
</document>
【讨论】:
以上是关于禁用和启用页面滚动 (Swift)的主要内容,如果未能解决你的问题,请参考以下文章