Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key-value observing for QLPreviewController.currentPreviewItemIndex

I have an object that need to be notified when a QLPreviewController changes the shown document. QLPreviewController have the property currentPreviewItemIndex that is updated when the document change. I've added my object as observer for currentPreviewItemIndex and it receives the notification when in my code is changed the property, so far so good. The problem is that the user can change the shown document swiping in the screen and I've found out that in this case the notification isn't generated.

Any solution to receive the notification also in this case? I suppose that the notification is generated when is called the setter of the property currentPreviewItemIndex and probably when the user swipe the property is changed internally in the object QLPreviewController.

Another solution may be to disable the horizontal swipe in QLPreviewController but preserving the vertical swipe (there are the arrows buttons to change the shown document). How do you do that?

Thanks in advance for the help.

Giannandrea

like image 345
jean71 Avatar asked Nov 04 '22 11:11

jean71


1 Answers

make a category on the QLPreviewController and swizzle the appropriate method and either add the willChange/didChange for KVO ;)

seriously though: I tried KVO and it didnt work for me either.. 1) id file a bug with apple for that saying you need this

BUT as a workaround

  • (id )previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index {

this is called ok and everytime we swipe so I would 'hack' this to FIRE your own correct KVO. something like

     static NSInteger oldIndex = -1; //reset when the panel is hidden or shown
     int newIndex = qlController.displayedIndex;
     if(oldIndex != newIndex) {
         oldIndex = newIndex;
         [qlController willChangeValueForKey:@"displayedIndex"];
         [qlController didChangeValueForKey:@"displayedIndex"];
     }

I wrote it inline here so there are bound to be typos and mistakes but I think the general approach could work.

like image 165
Daij-Djan Avatar answered Nov 09 '22 13:11

Daij-Djan