Swift - UIPageViewController中的UIPinchGestureRecognizer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift - UIPageViewController中的UIPinchGestureRecognizer相关的知识,希望对你有一定的参考价值。

好的,我已经创建了一个网页浏览滑块应用。一切都很好,但现在我希望能够使用UIPinchGestureRecognizer捏合我的UIPageViewController。

我已经在代码中设置了所有内容,但是当我捏时,我仍然会收到此错误。

Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[App.ViewController pinchDetected]: unrecognized selector sent 
to instance 0x7faadc90c6f0'

如何使用UIPinchGestureRecognizer在UIPageViewController中使用我的pinchDetected函数?

如果我做错了,请现在就告诉我。 swift / ios新手

我的代码

class ViewController: UIViewController, UIPageViewControllerDataSource {

var pageViewController: UIPageViewController!

private var allPages = [Page]()
var pages = NSMutableOrderedSet()

override func viewDidLoad() {
    super.viewDidLoad()

    ###PinchRecognizer
    var pinchRecognizer = UIPinchGestureRecognizer(target:self, action:"pinchDetected")
    self.view.addGestureRecognizer(pinchRecognizer)

    self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
    self.pageViewController.dataSource = self

    var startVC = self.viewControllerAtIndex(0) as ContentViewController
    var viewControllers = NSArray(object: startVC)
    self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)

    self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height)

    self.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)
}

###PinchRecognizer Function
func pinchDetected(pinchRecognizer: UIPinchGestureRecognizer) {
    var scale: CGFloat = pinchRecognizer.scale;
    self.view.transform = CGAffineTransformScale(self.view.transform, scale, scale);
    pinchRecognizer.scale = 1.0;
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    var vc = viewController as! ContentViewController
    var index = vc.pageIndex as Int

    if (index == 0 || index == NSNotFound) {
        return nil
    }

    index--
    return self.viewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    var vc = viewController as! ContentViewController
    var index = vc.pageIndex as Int

    if (index == NSNotFound) {
        return nil
    }

    index++

    if (index == self.pages.count) {
        return nil
    }

    return self.viewControllerAtIndex(index)

}

func viewControllerAtIndex(index: Int) -> ContentViewController
{
    if ((self.pages.count == 0) || (index >= self.pages.count)) {
        return ContentViewController()
    }

    var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController

    ###Store image in imageFile
    var image = (pages.objectAtIndex(index) as! Page).image        
    vc.imageFile = image
    vc.pageIndex = index

    return vc

}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
    return self.pages.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
    return 0
}

} 

以上是关于Swift - UIPageViewController中的UIPinchGestureRecognizer的主要内容,如果未能解决你的问题,请参考以下文章

UIPageViewController 手势覆盖我的 UITableView

UIPageViewControllers UIPageControl 在过渡期间出现故障

在 Monotouch 中的 UIPageViewController 中检测控件上的触摸事件

UIButton 没有为 .infoLight 显示带有“i”的圆圈

UIPageControl 将图像移到它上面(而不是在它上面)

由于 AVPlayer 和观察者的反初始化失败导致的内存泄漏