UIView frame animation, subview UIScrollView not always animates

In this example.

I am animating a PhotoViewerViewController frame when I am animating a tabBarController (for full screen effect). PhotoViewer uses uiscrollview to create the same effect as Apple photos. For some reason, sometimes it is animated with my PhotoViewer framework, and sometimes not.

In the first example, you can see that it increases with increasing frame size, but it improves noticeably with decreasing frame size (and restoring the tab bar).

However, in this example, when the photo is vertical, it jumps in both directions.

In both cases, if I enlarge the photo on the scroll screen at all, it animates correctly in both directions.

I know that there is evidence, but I can not say what is happening here.

Here is my animation block:

void (^updateProperties) (void) = ^ { self.navigationController.navigationBar.alpha = hidden ? 0.0f : 1.0f; self.navigationController.toolbar.alpha = hidden ? 0.0f : 1.0f; if (self.tabBarController) { int height = self.tabBarController.tabBar.bounds.size.height; for(UIView *view in self.tabBarController.view.subviews) { int newHeight; UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (UIInterfaceOrientationIsPortrait(orientation)) { newHeight = hidden ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.height - height; } else { newHeight = hidden ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.width - height; } if([view isKindOfClass:[UITabBar class]]) { [view setFrame:CGRectMake(view.frame.origin.x, newHeight, view.frame.size.width, view.frame.size.height)]; } else { CGRect newFrame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, newHeight); [view setFrame:newFrame]; // update our VC frame with animation [self.view setFrame:newFrame]; } } } }; 

Code adapted from SO: UITabBar message will not be hidden

Full github source.

+6
source share
2 answers

In the general case, if the animation sometimes works, and sometimes not, because in the latter case, delayed execution forces them to cancel effectively by setting the corresponding property to its final value.

In the special case of UIScrollView often happens that -layoutSubviews is called without an animation, one run cycle after closing the animation blocks. For this reason, I usually try to avoid making a layout in the method where I have a UIScrollView in the view hierarchy (because, especially before iOS 5, UIScrollView will really work to set itself up for the layout).

I would recommend removing your call -[EGOPhotoImageView layoutScrollViewAnimated:] from -layoutSubviews and adding it to the overridden -setFrame: .

If you can customize iOS4 and higher, also look at UIViewAnimationOptionLayoutSubviews . Using block animation with this as an option can work without changing anything else.

+6
source

I tried my github code on iPad 4.2 and 4.3 simulators and it works great ... without resizing the image! There may be some version issues. In addition, I tried to change your animation block, and the following actions serve the same purpose:

 void (^updateProperties) (void) = ^ { self.navigationController.navigationBar.alpha = hidden ? 0.0f : 1.0f; self.navigationController.toolbar.alpha = hidden ? 0.0f : 1.0f; } 

Let me know if I missed something :)

0
source

All Articles