ios - uipageviewcontroller disable right swipe -
i have uipageviewcontroller 4 individual view controllers in it. want disable right swipe action first view controller , disable left swipe action fourth view controller. when app launches, want user able swipe left , not right, because swiping right, takes user fourth view controller , swiping left on fourth view controller takes user first view controller. here code.
class tutorialpageviewcontroller: uipageviewcontroller, uipageviewcontrollerdatasource, uipageviewcontrollerdelegate { weak var tutorialdelegate: tutorialpageviewcontrollerdelegate? lazy var vcarr: [uiviewcontroller] = { return [self.vcinstance(name: "firstvc"), self.vcinstance(name: "secondvc"), self.vcinstance(name: "thirdvc"), self.vcinstance(name: "fourthvc")] }() private func vcinstance(name: string) -> uiviewcontroller { return uistoryboard(name: "main", bundle: nil).instantiateviewcontroller(withidentifier: name) } override func viewdidload() { super.viewdidload() let pagecontrol = uipagecontrol.appearance() //customizing pagecontrol.pageindicatortintcolor = uicolor.lightgray pagecontrol.currentpageindicatortintcolor = uicolor(red:0.23, green:0.73, blue:1.00, alpha:1.0) datasource = self delegate = self self.datasource = self self.delegate = self if let firstvc = vcarr.first { setviewcontrollers([firstvc], direction: .forward, animated: true, completion: nil) tutorialdelegate?.tutorialpageviewcontroller(tutorialpageviewcontroller: self, didupdatepagecount: vcarr.count) } } override func viewdidlayoutsubviews() { super.viewdidlayoutsubviews() view in self.view.subviews { if view uiscrollview { view.frame = uiscreen.main.bounds } else if view uipagecontrol { view.backgroundcolor = uicolor.clear } } } public func pageviewcontroller(_ pageviewcontroller: uipageviewcontroller, viewcontrollerbefore viewcontroller: uiviewcontroller) -> uiviewcontroller? { guard let viewcontrollerindex = vcarr.index(of: viewcontroller) else { return nil } let previousindex = viewcontrollerindex - 1 guard previousindex >= 0 else { return vcarr.last } guard vcarr.count > previousindex else { return nil } return vcarr[previousindex] } public func pageviewcontroller(_ pageviewcontroller: uipageviewcontroller, viewcontrollerafter viewcontroller: uiviewcontroller) -> uiviewcontroller? { guard let viewcontrollerindex = vcarr.index(of: viewcontroller) else { return nil } let nextindex = viewcontrollerindex + 1 guard nextindex < vcarr.count else { return vcarr.first } guard vcarr.count > nextindex else { return nil } return vcarr[nextindex] } public func presentationcount(for pageviewcontroller: uipageviewcontroller) -> int { return vcarr.count } public func presentationindex(for pageviewcontroller: uipageviewcontroller) -> int { guard let firstviewcontroller = viewcontrollers?.first, let firstviewcontrollerindex = vcarr.index(of: firstviewcontroller) else { return 0 } return firstviewcontrollerindex } } extension tutorialpageviewcontroller { func pageviewcontroller(pageviewcontroller: uipageviewcontroller, didfinishanimating finished: bool, previousviewcontrollers: [uiviewcontroller], transitioncompleted completed: bool) { if let firstviewcontroller = viewcontrollers?.first, let index = vcarr.index(of: firstviewcontroller) { tutorialdelegate?.tutorialpageviewcontroller(tutorialpageviewcontroller: self, didupdatepageindex: index) } } }
basically need change following lines
guard previousindex >= 0 else { return nil } guard nextindex < vcarr.count else { return nil }
----- or -----
change viewcontrollerafter as
public func pageviewcontroller(_ pageviewcontroller: uipageviewcontroller, viewcontrollerafter viewcontroller: uiviewcontroller) -> uiviewcontroller? { guard let viewcontrollerindex = vcarr.index(of: viewcontroller) else { return nil } let nextindex = viewcontrollerindex + 1 guard nextindex < vcarr.count else { return nil } guard vcarr.count > nextindex else { return nil } return vcarr[nextindex] }
change viewcontrollerbefore as
public func pageviewcontroller(_ pageviewcontroller: uipageviewcontroller, viewcontrollerbefore viewcontroller: uiviewcontroller) -> uiviewcontroller? { guard let viewcontrollerindex = vcarr.index(of: viewcontroller) else { return nil } let previousindex = viewcontrollerindex - 1 guard previousindex >= 0 else { return nil } guard vcarr.count > previousindex else { return nil } return vcarr[previousindex] }
Comments
Post a Comment