Check if scrollview.contentset passed a specific x value

I have scrollview and above some image. When scrollview scrollView.contentOffset.x passes by a specific X, my image above should be animated.

I know how to revive. At the moment, I am doing this in the method - (void)scrollViewDidScroll:(UIScrollView *)scrollView .

 if (scrollView.contentOffset.x == 160) { //animate Image } 

but sometimes he gets 160, but in other cases he goes through 160. How can I solve this?

+4
source share
3 answers

Add an instance variable, set it to the offset you saw in the last call to scrollViewDidScroll: and use it to decide if you want to animate:

 // Instance variable CGPoint lastOffset; ... - (void)scrollViewDidScroll:(UIScrollView *)scrollView { ... if (lastOffset.x < 160 && scrollView.contentOffset.x >= 160) { //animate Image } lastOffset = scrollView.contentOffset; } 

This will allow you to animate the image every time the scroll view crosses from 160 to 160.

+3
source

Use >= 160 , but also use the flag so that you know that you have already done the animation:

 if (scrollView.contentOffset.x == 160 && !self.animatedImage) { self.animatedImage = YES; ... } 
+2
source

I think you should add a flag to animate the image, and control that flag during the scroll / after animation

 BOOL isCanAnimate_; // some code here - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.x >= imageView.frame.size.width / 2 && isCanAnimate_) { isCanAnimate_ = FALSE; [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^ { // Animation here } completion:^(BOOL finished) { isCanAnimate_ = TRUE; }]; } } 
0
source

Source: https://habr.com/ru/post/1480134/


All Articles