iCarousel
iCarousel copied to clipboard
Scrolling by max one page for each gesture
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
Are you using the pagingEnabled? property?
I tried to set it, without any change in behaviour. When I drag from the side, I can still drag over two pages.
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 UISwipeGestureRecognizer
s 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.
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
@nicklockwood Our requirements
- 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
. - 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
.
@nicklockwood Do you think it's possible to use these gestures while still allowing you to drag the items left and right?
pagingEnabled set to YES