zepojo / UPCarouselFlowLayout

A fancy carousel flow layout for UICollectionView on iOS.
MIT License
1.66k stars 236 forks source link

scroll not smooth while scroll small space and release your hand quickly #37

Open Beyond-Chao opened 6 years ago

Beyond-Chao commented 6 years ago

scroll not smooth while scroll small space and release your hand quickly

iamsanketray123 commented 6 years ago

Yes this issue exists. Please fix. The animation isn't smooth when we try to scroll but it stop scrolling the very next instant. It leads to a jerk effect. Maybe you need to fix your snapping code. Adding 0.1-0.2 seconds of more animation time.

Beyond-Chao commented 6 years ago

thanks!Let me try

gk704297 commented 6 years ago

Hi guys, i found solution for this problem. Just Replace this code in targetContentOffset method

let proposedContentOffsetCenterOrigin = (isHorizontal ? proposedContentOffset.x + 1000 velocity.x : proposedContentOffset.y + 1000 velocity.y) + midSide

thanks.

Sulinskii commented 5 years ago

Unfortunately this is not working for me. Does anyone have any other solution?

MrFuFuFu commented 5 years ago

Or try to change it to int targetContentOffset method

let proposedContentOffsetCenterOrigin = (isHorizontal ? proposedContentOffset.x + (proposedContentOffset.x * velocity.x) : proposedContentOffset.y + (proposedContentOffset.y * velocity.y)) + midSide
suraj-ios commented 5 years ago

@Beyond-Chao

You can try this code.

override func viewDidLayoutSubviews() { self.setupLayout() if self.indexNumber == 0{ // Set indexNumber = 0 for the 1st cell only. let layout = UPCarouselFlowLayout() layout.itemSize = CGSize(width:view.frame.width, height:448) layout.scrollDirection = .horizontal self.collectionView.collectionViewLayout = layout } self.rotationDidChange() }

santaasus commented 5 years ago

I resolved the problem let differenceBetweenOffsets = velocity.x > 0 ? targetContentOffset.x - prevOffset : prevOffset - targetContentOffset.x

   ` let jerkEffect = prevOffset == targetContentOffset.x || // Didn't move anywhere
                        targetContentOffset.x =< 0 || // Didn't move anywhere
                                differenceBetweenOffsets < 100 // had difference offsets between prev and current, but not enough for scroll by the next item `

  `  if jerkEffect {
        print("Ops occurred jerk effect")
        let correctOffset = velocity.x > 0 ? targetContentOffset.x + itemSize.width + self.minimumLineSpacing
                                            : targetContentOffset.x - itemSize.width - self.minimumLineSpacing
        targetContentOffset.x = correctOffset
    }
    prevOffset = targetContentOffset.x`
petr-fiala commented 4 years ago

I resolved the problem let differenceBetweenOffsets = velocity.x > 0 ? targetContentOffset.x - prevOffset : prevOffset - targetContentOffset.x

   ` let jerkEffect = prevOffset == targetContentOffset.x || // Didn't move anywhere
                        targetContentOffset.x =< 0 || // Didn't move anywhere
                                differenceBetweenOffsets < 100 // had difference offsets between prev and current, but not enough for scroll by the next item `

  `  if jerkEffect {
        print("Ops occurred jerk effect")
        let correctOffset = velocity.x > 0 ? targetContentOffset.x + itemSize.width + self.minimumLineSpacing
                                            : targetContentOffset.x - itemSize.width - self.minimumLineSpacing
        targetContentOffset.x = correctOffset
    }
    prevOffset = targetContentOffset.x`

works on iOS13, also you should update prevOffset in viewDidLayoutSubviews, to make it work right after initialization and rotation

ghost commented 4 years ago

Can anyone help as I am unable to fix this issue? and I have used pod for the same.

petr-fiala commented 4 years ago

Can anyone help as I am unable to fix this issue? and I have used pod for the same.

i also added extra condition for jerkEffect

        // jerk effect fix
        let layout = carouselCollectionView.collectionViewLayout as! UPCarouselFlowLayout
        let differenceBetweenOffsets = velocity.x > 0 ? targetContentOffset.pointee.x - prevOffset : prevOffset - targetContentOffset.pointee.x
        let jerkEffect = (prevOffset == targetContentOffset.pointee.x || // Didn't move anywhere
            targetContentOffset.pointee.x <= 0 || // Didn't move anywhere
            differenceBetweenOffsets < 100) && // had difference offsets between prev and current, but not enough for scroll by the next item `
            abs(velocity.x) > 0.1
        if jerkEffect {
            print("Ops occurred jerk effect")
            let correctOffset = velocity.x > 0 ? targetContentOffset.pointee.x + layout.itemSize.width + layout.minimumLineSpacing : targetContentOffset.pointee.x - layout.itemSize.width - layout.minimumLineSpacing
            targetContentOffset.pointee.x = correctOffset
        }
        prevOffset = targetContentOffset.pointee.x

also, don't forget to update prevOffset whenever you manually set the offset and during initialization