简体   繁体   中英

Animate only views that are on-screen

So I want to have transition between my app's views that consist of a special animation. In a nutshell, said animation fades out every single subview of the page you are on, one after another, before continuing to the next. The obvious problem is, if I use an UIScrollView at some point, my animation would just fade out the contents of the entire scrollview, which would take way too long, and not just the ones you see on-screen. Which brings me to my question:

Is it possible to get all the subviews of an UIView that are currently visible on-screen?

Thanks in advance

You could check which views are on the visible area of the scroll view with something like this:

CGRect visibleArea = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.view.frame.size.width, scrollView.view.frame.size.height);
NSMutableArray *visibleViews = [[NSMutableArray alloc] init];
for(UIView *view in scrollView.subviews){
  if(CGRectIntersectsRect(visibleArea, view.frame)
    [visibleViews addObject:view];
}

The result would be that you'd have an array (visibleViews) with all the views which's rects intersect with the visible rect of the scroll view. You could then animate only the views in said array.

PS. I didn't test that code, but it should give you the general idea.

Loop through all of the subviews in an animation and remove them from the superview, and before commitAnimations, and then change VC:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0f];
for (UIView *view in self.view.subviews) {
    [view removeFromSuperview];
}
[UIView commitAnimations];
[self.navigationController performSelector:@selector(pushViewController:) withObject:viewControllerToSwitchTo afterDelay:1.0f];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM