Scroll a background in a different speed on a UIScrollView

前端 未结 4 1107
青春惊慌失措
青春惊慌失措 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:50

    You can easily do this by implementing scroll view did scroll with a UIImageView under it... You'll end up with something like this... with the backgroundImageView being a UIImageView added to the view before the subview... you can layer as much image views as you want without performance issues

        - (void)scrollViewDidScroll:(UIScrollView *)scrollView
        {    
             float factor = scrollView.contentOffset.x / (scrollView.contentSize.width - 320);
             if (factor < 0) factor = 0;
             if (factor > 1) factor = 1;
    
             CGRect frame = backgroundImageView.frame;
             frame.origin.x = factor * (320 - backgroundImageView.frame.size.width);
             backgroundImageView.frame = frame;
        }
    

提交回复
热议问题