Xcode 9 UIpageViewController导致子视图UI按钮延迟按下

时间:2017-11-10 17:05:51

标签: ios iphone swift xcode uibutton

我遇到了一个问题,我的UI按钮没有响应。通常我可以像平时一样点击它但是当在UIpageviewcontrol中加载视图时,必须按下按钮以获得响应。这是为什么?非常感谢

UIpageViewControl:

abstract class Employee
{
    public Superior: Employee;

    public getSuperiorOfType<T extends Employee>(type: { new(...args: any[]): T}): T
    {
        if (this instanceof type) return this;
        else if (this.Superior !== undefined) return this.Superior.getSuperiorOfType(type);
    }
}

class Manager extends Employee {}
class TeamLead extends Employee {}
class Developer extends Employee {}

let tom = new Manager();
let suzanne = new TeamLead();
let ben = new Developer();

ben.Superior = suzanne;
suzanne.Superior = tom;


let x = ben.getSuperiorOfType(Manager); 
console.log(x === tom); // true

带有按钮的ViewController不是正常的

import UIKit

class mainPageViewController: UIPageViewController , 
UIPageViewControllerDelegate , UIPageViewControllerDataSource{


lazy var orderedViewControllers : [UIViewController] = {
    return [self.newVC(viewController: "messages"),
            self.newVC(viewController: "discover"),
            self.newVC(viewController: "profile")
    ]

}()

var pageControl = UIPageControl()

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self;
    if let firstVC = orderedViewControllers.first{
        setViewControllers([firstVC],
                           direction: .forward,
                           animated: true,
                           completion: nil);
        self.delegate = self;

        // ads dots if i want later

        //configPageControl()
    }
    // Do any additional setup after loading the view.
}

func newVC(viewController: String ) -> UIViewController{
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController);
}


func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    let pageContentViewContoller = pageViewController.viewControllers![0];
    self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewContoller)!
}

func configPageControl(){
    pageControl = UIPageControl(frame: CGRect(x: 0, y: UIScreen.main.bounds.maxY - 50, width: UIScreen.main.bounds.width, height: 50))
    pageControl.numberOfPages = orderedViewControllers.count;
    pageControl.currentPage = 0;
    pageControl.tintColor = UIColor.white;
    pageControl.pageIndicatorTintColor = UIColor.white;
    pageControl.currentPageIndicatorTintColor = UIColor.red;
    self.view.addSubview(pageControl);
}


func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let  viewControllerIndex = orderedViewControllers.index(of : viewController)else{
        return nil;
    }

    let previousIndex = viewControllerIndex - 1;

    guard previousIndex >= 0 else{
        // infinatly swipe in a loop
        return orderedViewControllers.last;
        // no swipe back
        //return nil;
    }

    guard orderedViewControllers.count > previousIndex else{
        return nil;
    }

    return orderedViewControllers[previousIndex];
}


func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let  viewControllerIndex = orderedViewControllers.index(of : viewController)else{
        return nil;
    }

    let nextIndex = viewControllerIndex + 1;

    guard orderedViewControllers.count != nextIndex   else{

        // infinatly swipe in a loop
        return orderedViewControllers.first;
        // no swipe back
        //return nil
    }

    guard orderedViewControllers.count > nextIndex else{
        return nil;
    }

    return orderedViewControllers[nextIndex];
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
} 

}

1 个答案:

答案 0 :(得分:-1)

尝试将此添加到mainPageViewController班级:

override func touchesShouldCancel(in view: UIView) -> Bool {
    if view is UIButton {
        return true
    }
    return super.touchesShouldCancel(in: view)
}

在旁注中,对类名使用前导大写,对变量和函数名称使用小写:

class MainPageViewController: UIPageViewController ...