Adding an entirely different view behind the current view

江枫思渺然 提交于 2019-12-24 11:35:23

问题


Summary: I am trying to implement a pan gesture that works just like Apple's default pan back gesture (interactivePopGestureRecognizer). The only difference is that it snaps all the way back to the primary view of a splitview controller rather than just going back one view on the stack. This is accomplished with popToRootViewController.

What I can currently do: My subclass of the UIScreenEdgePanGestureRecognizer is able to follow the user's touch and animate to the proper location based on where they lift their finger. That is, if they have only moved the view over a little, it snaps back to it original position and the gesture recognizer is reset. If the user moves it to the right far enough and lets go (past a threshold that is slightly less than halfway across the screen), then the view is slid off the right side of the screen and the primary view slides into view.

What I need: While moving my top detail view to the right, I would like to use a snapshot I have taken of the primary view as it was before I navigated away, to show BEHIND my current detail view (I use the snapshot to create a UIImageView - lets call it "dummyMasterview"). My problem is that functions like addSubView seem to place dummyMasterview as a background on that view. I want to be able to smoothly pull the top view to the right to reveal the dummyMasterview below it. Then, when I release, I will animate the view appropriately (popping all the way down to my actual root view if the threshold was crossed). So far I have only been able to use addSubview and the other subview methods to place this image as a background ON the current view. Not as a completely new view behind it.


回答1:


Ok, I figured this out... I was unable to add my "dummyMasterview" behind the currently visible view (let's call it currentView) because I was adding it as a subview of currentView. That results in dummyMasterview just being plopped right on top of currentView, not behind it. In order to place dummyMasterview behind currentView you must make dummyMasterview a subview of the main window:

// Create a dummyMasterView from the image and add it as a subview of the main
// window so it appears behind the current visible view while panning
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
UIImageView* dummyMasterView = [[UIImageView alloc] initWithImage:dummyMasterImage];
UIView* mainWindowSubview = mainWindow.subviews[0];

 //Add dummyMaster to main window subview.
[mainWindowSubview addSubview:self.dummyMasterView];

 //Move it to the back so it doesn't cover up currentView
[mainWindowSubview sendSubviewToBack:self.dummyMasterView];

So basically, I was silly to assume that the subview of currentView would be placed BEHIND that currentView. Since I wanted an entirely new view to appear behind currentView, the new view needed to be a sibling of currentView. That is, both currentView and dummyMasterView are now both subviews of the main window. Pictures will be posted for clarification once I get a chance.



来源:https://stackoverflow.com/questions/32954552/adding-an-entirely-different-view-behind-the-current-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!