禁用UIPageViewController Swipe - Swift

时间:2016-08-02 02:37:49

标签: swift xcode uipageviewcontroller

在我的项目中,我使用UIPageViewController在5个子UIViewController之间滑动。在一些子视图控制器中,我需要禁用UIPageViewController的滑动手势,因此当用户滑动它时不会更改为其他视图。 那么我如何禁用子视图控制器的滑动?

感谢您的帮助..谢谢

2 个答案:

答案 0 :(得分:17)

在页面视图控制器中,添加以下内容

override func viewDidLoad(){
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourpageviewcontroller.enableSwipe(_:)), name:"enableSwipe", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourpageviewcontroller.disableSwipe(_:)), name:"disableSwipe", object: nil)

}
func disableSwipe(notification: NSNotification){
    self.dataSource = nil
}

func enableSwipe(notification: NSNotification){
    self.dataSource = self
}

在您的子视图控制器中,您可以通过以下方式发布通知。

NSNotificationCenter.defaultCenter().postNotificationName("enableSwipe", object: nil)

OR

NSNotificationCenter.defaultCenter().postNotificationName("disableSwipe", object: nil)

答案 1 :(得分:0)

您可以使用此扩展名:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
    title = 'cross-tab';
    csvContent: string;
    parsedCsv: string[][];
    headers: string[];

    ngOnInit(){
    }

    onFileLoad(fileLoadedEvent) {
        const csvSeparator = ",";
        const textFromFileLoaded = fileLoadedEvent.target.result;
        this.csvContent = textFromFileLoaded;
        // alert(this.csvContent);

        const txt = textFromFileLoaded;
        const csv = [];
        const lines = txt.split('\n');
        lines.forEach(element => {
            const cols: string[] = element.split(csvSeparator);
            csv.push(cols);
            });
        this.parsedCsv = csv;
        //this.headers = this.parsedCsv[0];
        //console.log(this.parsedCsv);
    }

    onFileSelect(input: HTMLInputElement) {
        const files = input.files;
        var content = this.csvContent;

        if(files && files.length)
        {
            const fileToRead = files[0];
            const fileReader = new FileReader();
            fileReader.onload = this.onFileLoad.bind(this);
            fileReader.readAsText(fileToRead, "UTF-8");
            //this.headers = ["A", "B", "C"];
            this.headers = this.parsedCsv[0];
            console.log(this.parsedCsv);
        }
    }
}

并称呼它:

extension UIPageViewController {
     var isPagingEnabled: Bool {
        get {
           var isEnabled: Bool = true
           for view in view.subviews {
               if let subView = view as? UIScrollView {
                   isEnabled = subView.isScrollEnabled
               }
           }
           return isEnabled
       }
       set {
           for view in view.subviews {
               if let subView = view as? UIScrollView {
                   subView.isScrollEnabled = newValue
               }
           }
       }
   }    
}