ios - Handling UIPanGestureRecognizer gestures for multiple Views (one covers the other) -


sorry such long question, felt should convey have tried.

i've got view viewa within navigation controller. adding subview viewb (that contains uitableview) viewa , offsetting origin height covers half screen (with other half overflowing off out bottom of screen). want able drag viewb upwards stopped when hits bottom of navigation bar , stopped when dragged down when hits origin offset point. have achieved successfully.

however, want uitableview interaction enabled when viewb in upper position , not respond gestures in other position. essentially, dragging viewb covers viewa should enable interaction uitableview.

the tricky part here want following:

  1. if viewb in upper position covering screen, uitableview content offset 0 (i.e. @ top of table) , user makes pan gesture downwards, gesture should not interact uitableview should move viewb downwards.
  2. any other pan gesture in above condition should interaction uitableview.
  3. if viewb in upper position covering screen, uitableview content offset not @ 0 (i.e. not @ top of table) , user makes pan gesture downwards, gesture should interact uitableview.

i've been close achieving can't quite right.

attempts far

i'm using uipangesturerecognizer handle dragging of view. have tried adding to:

  • viewb uitableview user interaction disabled. allows me drag viewb , down without interfering uitableview. once viewb in upper position enable uitableview user interaction correctly allows me interact uitableview without moving viewb.

    however, enabling uitableview user interaction, means touches never reach uipangesturerecognizer, meaning can never detect scenario described in point (1.) above , can't re-disable uitableview user interaction make viewb movable again.

    maybe possible way overriding gesture recognition methods used uitableview? if possible can point me in right direction?

  • a new view added in front of uitableview. thought maybe forward touch gestures uitableview behind when necessary still haven't found way this.

    all have been able disable gesture recognizer allows me interact uitableview, have same issue above. can't detect when re-enable it.

  • the uitableview within viewb. seemed promising way far. setting return values of following methods can enable , disable recognition of either viewb , uitableview.

    func gesturerecognizer(gesturerecognizer: uigesturerecognizer, shouldreceivetouch touch: uitouch) -> bool {     if pullovervc.view.frame.origin.y == bottomnavbary &&         pullovervc.tableview?.contentoffset.y == 0 { // need add gesture direction check condition         viewbisattop = true         return false // disable pullover control     }     return true // enable pullover control }  func gesturerecognizer(gesturerecognizer: uigesturerecognizer, shouldrecognizesimultaneouslywithgesturerecognizer othergesturerecognizer: uigesturerecognizer) -> bool {     if (gesturerecognizer as! uipangesturerecognizer).velocityinview(view).y < 0 && viewbisattop { // gesture direction check not wanted here         return true // enable tableview control     }     viewbisattop = false     return false // disable tableview control } 

    the top method called first when gesture made (i have checked print statements) followed bottom method. making different combinations of true/false 2 methods can alternate interaction between viewb , uitableview.

    to detect whether user swiping downwards calling velocityinview() on recognizer (as shown in bottom method). intending on making check in top methods if statement , think work, however, although velocityinview() works fine in bottom method, not in top 1 (velocity 0).

i have scoured solution , find many similar queries gesture handling views cover each other, these seem regarding 1 gesture type, e.g. pinch, on 1 view, , type, e.g. pan, on other. in case gesture type same both.

maybe has clever idea? or maybe simple , have made incredibly complicated? xd

managed working.

of methods described in question above removed top 1 keeping (it has few changes):

func gesturerecognizer(gesturerecognizer: uigesturerecognizer, shouldrecognizesimultaneouslywithgesturerecognizer othergesturerecognizer: uigesturerecognizer) -> bool {     if ((gesturerecognizer as! uipangesturerecognizer).velocityinview(view).y < 0         || pullovervc.tableview.contentoffset.y > 0)         && pullovervc.view.frame.origin.y == bottomnavbary {             return true // enable tableview control     }     return false } 

the if statement checks covering uitableview in upper position and either user is not dragging downwards or table content offset (we not @ top of table). if true, return true enable tableview.

after method called, standard method implemented handle pan gesture called. in here have if statement sort of checks opposite above, , if that's true, prevents control on covering viewb moving:

func handlepangesture(recognizer: uipangesturerecognizer) {      let gestureisdraggingfromtoptobottom = (recognizer.velocityinview(view).y > 0)      if pullovervc.view.frame.origin.y != bottomnavbary || (pullovervc.view.frame.origin.y == bottomnavbary && gestureisdraggingfromtoptobottom && pullovervc.tableview.contentoffset.y == 0) {      ... 

this keeps uitableview interaction off unless parent view viewb in correct position, , when is, disables movement of viewb interaction uitableview works.

then when, @ top of table, , drag downwards, interaction uitableview re-disabled , interaction parent view viewb re-enabled.

a wordy post , answer, if can make sense of i'm saying, you.


Comments