D-32 / SegmentedProgressBar

Snapchat / Instagram Stories like progress indicator
MIT License
471 stars 56 forks source link

Rewind one story #12

Open michael-martinez opened 6 years ago

michael-martinez commented 6 years ago

I am trying to load a previous story but cannot manage to do so.

Here is the function I am trying to implement which should be the opposite action to skip() :

func rewindOne() { let currentSegment = segments[currentAnimationIndex] currentSegment.topSegmentView.frame.size.width = 0 currentSegment.topSegmentView.layer.removeAllAnimations()

var newIndex = self.currentAnimationIndex - 1
    if newIndex < 0 { newIndex = 0 }

    if newIndex >= 0 {
        self.delegate?.segmentedProgressBarChangedIndex(index: newIndex)
        self.animate(animationIndex: newIndex)
    }

}

The animation does not stop, I have 3 items, tried: segments.forEach { $0.topSegmentView.layer.removeAllAnimations() } The segment keeps being animated.

michael-martinez commented 6 years ago

Found one really inefficient but working solution : spb.removeAllAnimations() spb.removeFromSuperview() spb = nil

        spb = SegmentedProgressBar(numberOfSegments: self.playerItems.count, durations: self.playerItemsDuration)
        spb.frame = CGRect(x: 15, y: 15, width: view.frame.width - 30, height: 4)
        view.addSubview(spb)

        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.001) {
            self.spb.startAnimation()
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.001) {
                for _ in 0..<self.currentStory {
                    self.spb.skip()
                }

}

dineshguptha commented 5 years ago

Hello @michael-martinez are you using this with collection view ?

kiet1993 commented 5 years ago

I found a solution. Add following code in SegmentedProgressBar class You can call segmentedProgressBarChangedIndex delegate if you want to notify.

func rewindCurrentSegment(){ let currentSegment = segments[currentAnimationIndex] currentSegment.topSegmentView.layer.removeAllAnimations() currentSegment.topSegmentView.frame.size.width = 0 self.animate(animationIndex: currentAnimationIndex) }

This is a simple project but very helpful. Easy to custome. Thanks dev team.