Pon OnetwoDream
Pon OnetwoDream

Reputation: 33

Animate when scrolling using UIScrollView SWIFT

Im having abit of trouble animating a label to move into another position when a user scrolls horizontally.

Here is the snippet i have:

override func viewDidLoad() {
    super.viewDidLoad()

    buttonTextSpacing(UIButton: vysionButton, lineSpacing: 2.5, UIColor: .black)
    buttonTextSpacing(UIButton: converseButton, lineSpacing: 2.5, UIColor: .black)
    buttonTextSpacing(UIButton: storiesButton, lineSpacing: 2.5, UIColor: .black)

    self.storiesCollectionView.delegate = self
    self.storiesCollectionView.dataSource = self

    // Scroll View
    self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width*2, height: self.scrollView.frame.height)
    self.scrollView.addSubview(storiesCollectionView)
    self.scrollView.showsHorizontalScrollIndicator = false
    self.scrollView.delegate = self


}

func scrollViewDidScroll(scrollUIView: UIScrollView) {
    print("scrollViewDidScroll")
    DispatchQueue.main.async {
        UIView.animate(withDuration: 0.4, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
            self.scrollLabel.center.x = self.storiesButton.center.x - 1
        }, completion: nil)
    }
}

Basically what im trying to achieve is that when a user scrolls, the animation starts but does not finish until the scroll is finished to the next custom uiview (I have paging enabled also).

Currently it doesnt do that, im not able to call the scrolling event nor even make the label animate (To the new position) while scrolling.

Many thanks in advance for any advice or help.

Cheers, Pon

Upvotes: 3

Views: 4131

Answers (1)

panychyk.dima
panychyk.dima

Reputation: 176

As I understand you have some label as subview on scrollView and that label is located over some views that you scroll. You may use scrollView.contentOffset.x instead of animation. For example: You need to calculate the distance between the start position of label and end position. That distance divide by 100, you get step (1 percent of distance width) that you need to multiply by scrollView.contentOffset.x. Give more details of your question.

Upvotes: 1

Related Questions