iCarousel icon indicating copy to clipboard operation
iCarousel copied to clipboard

Scrolling by max one page for each gesture

Open ghost opened this issue 9 years ago • 7 comments

Hello All,

is there any way a user can scroll max by one page for each gesture? Now, i can swipe by two pages when swiping slow from the side.

Thank you. Best. Matt

ghost avatar Jan 22 '16 15:01 ghost

Are you using the pagingEnabled? property?

nicklockwood avatar Jan 22 '16 15:01 nicklockwood

I tried to set it, without any change in behaviour. When I drag from the side, I can still drag over two pages.

ghost avatar Jan 22 '16 15:01 ghost

It's intentional that the carousel should move with your finger, so with pagingEnabled, it's still possible to drag over two items. What pagingEnabled affects is the momentum - with pagingEnabled set to YES, the carousel will never skip multiple items if you flick it - it will always stop on whichever item was selected when you lifted your finger.

I think that's the behavior that users would expect, but if you really want to prevent them from scrolling two items at a time, the best option is probably to set scrollingEnabled = NO, then add two UISwipeGestureRecognizers to your view (one for forwards, one for backwards), and in their actions, call [carousel scrollByNumberOfItems:1 animated:YES] and [carousel scrollByNumberOfItems:-1 animated:YES] respectively.

nicklockwood avatar Jan 22 '16 15:01 nicklockwood

Here is the implementation if anyone will need:

func viewDidLoad() {

   // your viewDidLoad code here .... 

    let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: "swipeCarouselLeft:")
    swipeGestureLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.carousel.addGestureRecognizer(swipeGestureLeft)

    let swipeGestureRight = UISwipeGestureRecognizer(target: self, action: "swipeCarouselRight:")
    swipeGestureRight.direction = UISwipeGestureRecognizerDirection.Right
    self.carousel.addGestureRecognizer(swipeGestureRight)

    self.carousel.scrollEnabled = false
}


// MARK: - Carousel Swiping
func swipeCarouselLeft(sender: UISwipeGestureRecognizer) {
    self.carousel.scrollByNumberOfItems(1, duration: 0.4)
}

func swipeCarouselRight(sender: UISwipeGestureRecognizer) {
    self.carousel.scrollByNumberOfItems(-1, duration: 0.4)
}

nicklockwood, thank you for your help.

Matt

ghost avatar Jan 22 '16 16:01 ghost

@nicklockwood Our requirements

  1. After scrolling stops, the center needs to show full item. It can't show half of an item. This can be achieved by setting pagingEnabled=YES.
  2. Flick needs to be smooth and will skip multiple items if the user flicks hard. From your comment it appears pagingEnabled=YES will disable this.

Is there a way to satisfy both? I actually think pagingEnabled=YES shouldn't disable the skipping. If user wants to disable skipping, he can set decelerationRate to 0. decelerationRate and pagingEnabled should work together. Right now pagingEnabled=YES will cause the effect as if decelerationRate=0.

slotmaster avatar Mar 17 '16 04:03 slotmaster

@nicklockwood Do you think it's possible to use these gestures while still allowing you to drag the items left and right?

GabeJacobs avatar Apr 14 '16 18:04 GabeJacobs

pagingEnabled set to YES

smindia1988 avatar Oct 05 '18 14:10 smindia1988