4

When a UIView appears on screen what method does it call? For example if a have a UIScrollView and I populate it with a hundred views, one under the other, what method is called by the UIViews when they are scrolled such that they appear on screen?

is it -(void)didMoveToWindow ? something else ?

Zigglzworth
  • 6,645
  • 9
  • 68
  • 107
  • 1
    None. What are you trying to achieve? – Wain Oct 13 '13 at 12:20
  • 1
    I want to create a custom uiview subclass with a delegate that calls a delegate function when it appears on screen to the user – Zigglzworth Oct 13 '13 at 12:27
  • The view doesn't have that knowledge. Scroll views can tell you what area of their content they are currently making visible, but not automatically what subviews are in that area. The views know nothing. – Wain Oct 13 '13 at 12:30

3 Answers3

2

If you are trying to find out when a view gets scrolled into scrollView bounds, it might be easier and probably more performant to use a table view... Table view has methods that let you know when a cell will appear and you can query visible cells at any time.

Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
1

Specifically for a scroll view, the delegate (usually the controller) can get notified that the scroll view has scrolled. When you get this callback you can write a bit of code that iterates through the subviews and checks which are in the visible frame of the scroll view (using CGRectIntersectsRect, contentOffset and bounds).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Too much processing. Not smooth enough. – Zigglzworth Oct 13 '13 at 12:32
  • Scrollviews can be very long and have many subviews loaded on them.. I am aware that I could use a tableview to avoid such an overload but I am looking to see if the scrollview way is possible first – Zigglzworth Oct 13 '13 at 12:51
  • Generally speaking, there is no reason to have views that aren't visible loaded. So really you should be monitoring the scroll and loading as required (which is exactly what table views do). – Wain Oct 13 '13 at 12:52
  • @Filip states this in his answer so I will tick it as correct but I agree with you. – Zigglzworth Oct 13 '13 at 14:24
0

None, BUT if you implement the UIScrollView delegate the UIScrollView does call a method that gives you the scroll position and more (many methods, most likely scrollViewDidScroll).

https://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollviewdelegate_protocol/Reference/UIScrollViewDelegate.html

To see which view, you have to calculate which view is visible by seeing if CGRectIntersectsRect(scrollView.bounds, subview.frame) returns true.

William Falcon
  • 9,813
  • 14
  • 67
  • 110
  • Yes but it doesn't tell me which view came into visibility which is what i am looking for. Now I am aware that I could store them in an array and find our which one by iterating through their positions but that is too much processing and not smooth enough for what I need. – Zigglzworth Oct 13 '13 at 12:31
  • You have to calculate which view is visible. I've amended the answer – William Falcon Oct 13 '13 at 12:35