Scroll a background in a different speed on a UIScrollView

前端 未结 4 1132
青春惊慌失措
青春惊慌失措 2021-01-13 13:32

When somebody does a wipe gesture to scroll the content from left to right, I would like to have a background image scrolling into the same direction, but at a different spe

4条回答
  •  佛祖请我去吃肉
    2021-01-13 13:42

    For example you have multiple scrollviews, want them scroll difference speed. here is the modification code base on Salamatizm answer:

    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
      float factor = scrollView.contentOffset.x / (scrollView.contentSize.width - screenSize.width);
      if (factor < 0) factor = 0;
      if (factor > 1) factor = 1;
    
      CGSize parralaxSize = self.parralaxBackgroundView.contentSize;
      CGPoint parallaxOffset = CGPointMake(-(factor * (screenSize.width - parralaxSize.width)), 0);
      [self.parralaxBackgroundView setContentOffset:parallaxOffset animated:NO];
    

提交回复
热议问题