首页滑动隐藏浮动购物车图标效果

2018-03-20  本文已影响0人  godBlessMe__

前段时间做项目是一个简单的需求,先看效果图:

image

底部使用的是tableView,最开始我仅仅是监听了tableView的偏移量,判断当tableView开始滑动执行隐藏动画,之后在tableView停止滑动后执行显示动画,出现的问题是,停止滑动后浮动图标不能显示出来,需要再滑动一次.


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

    WS(weakSelf);

    CGFloat y = scrollView.contentOffset.y / 50.0;

    UIImageView *imageView = [self.view viewWithTag:1000];

    [UIView animateWithDuration:0.5 animations:^{

        if (isIphoneX) {

            imageView.origin = CGPointMake(kScreenWidth, kScreenHeight - 174);

        } else {

            imageView.origin = CGPointMake(kScreenWidth, kScreenHeight - 140);

        }

    }];

    if (scrollView.contentOffset.y < 0) {

        [UIView animateWithDuration:0.3 animations:^{

            weakSelf.transNavigationBar.alpha = 0;

        }];

    } else {

        [UIView animateWithDuration:0.3 animations:^{

            weakSelf.transNavigationBar.alpha = 1;

        }];

    }

}

这么写就会出现当快速滑动手指立刻脱离屏幕浮动图标不会再次显示出来.
因此,我将代码更改后解决了bug

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

    WS(weakSelf);

//通过使用cancelPreviousPerformRequestsWithTarget:取消前面所注册过performSelector方法来解决问题

    [NSObject cancelPreviousPerformRequestsWithTarget:self];

//之后在0.3秒后去执行scrollViewDidEndScrollingAnimation停止滑动的代理方法,完美解决

    [self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:nil afterDelay:0.3];

    UIImageView *imageView = [self.view viewWithTag:1000];

    [UIView animateWithDuration:0.5 animations:^{

        if (isIphoneX) {

            imageView.origin = CGPointMake(kScreenWidth, kScreenHeight - 174);

        } else {

            imageView.origin = CGPointMake(kScreenWidth, kScreenHeight - 140);

        }

    }];

    if (scrollView.contentOffset.y < 0) {

        [UIView animateWithDuration:0.3 animations:^{

            weakSelf.transNavigationBar.alpha = 0;

        }];

    } else {

        [UIView animateWithDuration:0.3 animations:^{

            weakSelf.transNavigationBar.alpha = 1;

        }];

    }

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

//取消前面所注册过performSelector方法

    [NSObject cancelPreviousPerformRequestsWithTarget:self];

    UIImageView *imageView = [self.view viewWithTag:1000];

    [UIView animateWithDuration:0.5 animations:^{

        if (isIphoneX) {

            imageView.origin = CGPointMake(kScreenWidth - 80, kScreenHeight - 174);

        } else {

            imageView.origin = CGPointMake(kScreenWidth - 80, kScreenHeight - 140);

        }

    }];

}

第一次写简书,记录一下最近做碰到的问题

上一篇下一篇

猜你喜欢

热点阅读