viewDidLayoutSubviews called after touchesBegan - again and again

耗尽温柔 提交于 2019-12-22 11:24:57

问题


In an iPhone word game I have an UIScrollView (holding UIImageView) and 7 draggable custom UIViews placed initially at the bottom (and outside the scroll view):

In the single ViewController.m I have overwritten viewDidLayoutSubviews so that it always sets the zoomScale of the scroll view - to have the UIImageView fill exactly 100% of the screen width.

This works well - for portrait and landscape modes. And when the app is just started:

My problem is however, when I first pinch/zoom/double-tap the scroll view and then move one of the seven Tile.m views:

Suddenly (not every time) viewDidLayoutSubviews is called after touchesBegan.

This resets the zoom of the scroll view - unexpectedly for the user.

My question is if there is any way to disable this behavior?

Is it possible to prevent viewDidLayoutSubviews call of the parent, when its child UIView is being touched/dragged?

UPDATE:

I've moved the zoomScale setting code from viewDidLayoutSubviews to didRotateFromInterfaceOrientation and the scroll view zoom is okay now, but its contentOffset is reset to {0,0}, when (not always) I drag a Tile - i.e. the scroll view jumps suddenly, the app is unusable.

Adding the following to viewDidLoad hasn't helped:

if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)])
   self.automaticallyAdjustsScrollViewInsets = NO;

回答1:


viewDidLayoutSubviews is called whenever the system performs layout on the view (layoutSubviews). This can be called for a plethora of reasons; you can subclass your view, implement an empty layoutSubviews method (don't forget to call the super implementation!) and put a breakpoint there to see who causes the layout. You may also want to implement setNeedsLayout and layoutIfNeeded, and put breakpoints there also for your investigation, as these trigger layout on followup runloops.

But you will never be able to prevent layout. The system performs layout on many occasions which are outside of your control. For example, if a user makes a call, exists the phone app and returns to your app; a layout is triggered because the bounds and frame of the window have changed. The call ends; layout is again triggered because now the window is back to previous size.

You should be responsible for figuring out when to set the zoom scale. For example, if the user starts a gesture, you should signal your code not to perform changes, even if a layout was performed.



来源:https://stackoverflow.com/questions/22237271/viewdidlayoutsubviews-called-after-touchesbegan-again-and-again

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