开发者

uiscrollview catch scroll attempt

开发者 https://www.devze.com 2023-04-12 14:30 出处:网络
Im working with an UIScrollView subclass and im just trying to catch a particuliar event but i am at a loss as to how to approach it.

Im working with an UIScrollView subclass and im just trying to catch a particuliar event but i am at a loss as to how to approach it. basically i have a horizontal uiscrollview and it has bouncing turned off. when i scroll to the either end of the scroll view it should stop dead which it does. but now i want to catch an event if the user tries swiping in the direction of the currently occupied end of content. i.e. if the user scrolls all the way to the right and tries to swipe right again how do i catch this gesture.

I have tried adding gesture recognisers to the uiscrollview subclass and when it reaches the end it turns off scrolling enabled which allows these gestures to be caught. But if the user tries to swipe in the direction that is available the scrolling effect wont happen as scrolling is turned off. i.e. when the user has scrolled to the right, then tries to scroll back to the left nothing will happen as scrolling was turned off to catch a possible gesture to the right.

Any ideas on how to catch an attempt to scroll in a parti开发者_开发百科culiar direction? Thanks


One way to catch that event is by setting the bounces to property YES and implementing the stopped bounce yourself in a scrollview subclass. By overriding the setContentOffset methods, you can replace the coordinate it scrolls to. When this happens, you can let other things happen as well.

-(void)setContentOffset:(CGPoint)contentOffset
{
    CGPoint validOffset = [self validVersionOfOffset:contentOffset];

    if(contentOffset.x == validOffset.x)
    {
        [super setContentOffset:validOffset];
    }
    else
    {
        [super setContentOffset:validOffset animated:NO];
        [bounceCatchDelegate scrollViewTriedBouncing:self];
    }
}

-(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
{
    CGPoint validOffset = [self validVersionOfOffset:contentOffset];
    [super setContentOffset:validOffset animated:animated];
    if(contentOffset.x == validOffset.x)
    {
        [super setContentOffset:validOffset animated:animated];
    }
    else
    {
        [super setContentOffset:validOffset animated:NO];
        [bounceCatchDelegate scrollViewTriedBouncing:self];
    }
}

-(CGPoint)validVersionOfOffset:(CGPoint)offset
{
    //Prevents bouncing at the left and right:
    offset.x = fminf(offset.x, self.contentSize.width-CGRectGetWidth(self.frame));
    offset.x = fmaxf(offset.x, 0.0f);
    return offset;
}

The usage of a delegate is only a suggestion, any method call would work.


It's a workaround, but you can set the contentSize larger that it's actually is. Then in the scrollViewDidScroll delegate method you can check the contentOffset of the scrollview. If it's higher then the desired value, you set it back to your cap and do wathever you want. Maybe it won't look that good visually, but it can work. Hard to tell what's your goal with it.


kinda hacked something together its not pretty but working

start with bouncing turned off

using - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{}

i check the contentoffset and if at either end of the scroll view i then flag it as so and set bouncing as on. then in

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{}

i also check the contentoffset in here and of it is going beyond either of the end of the scroll content then turn off scrolling and set the content view to the end position. below is the code. very messy and completely ashamed of it but ill deal with it.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    int t = self.contentOffset.x;
    if (t==0) {
        leftSet = YES;
        rightSet = NO;
        self.bounces = YES;
    } else if (t>=imagePosX-480) {//if scrollview reaches the end 
        rightSet = YES;
        leftSet = NO;
        self.bounces = YES;
    }

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    int t = self.contentOffset.x; 

    if (t<=0) {
        if (leftSet) {
            self.bounces = NO;
            self.scrollEnabled = NO;

            //perform code i wanted here


        }


    } else if (t>=imagePosX-480) {//if scrollview reaches the end then snap back to start

        if (rightSet) {
            self.bounces = NO;
            self.scrollEnabled = NO;
            //perform code i wanted here

        }

    }
    else{
        rightSet = NO;
        leftSet = NO;
        self.bounces = NO;
    }



}

I in no way recommend the above but god damn it worked and unfortunately have bigger fish to fry. I was close to getting aberrants way of working for me and certainly seems cleaner so if anyone says his works ill def vote it as answer.


In my case I had the contentSize for the scrollView set AFTER using:

    self.scrollView.minimumZoomScale = (self.scrollView.frame.size.width/self.scrollView.contentSize.width);

which meant that as soon as the user started zooming on the scrollview the nan was hit and the exception triggered.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号