上下滚动以使用UIscrollview的委托功能隐藏和显示视图

时间:2018-07-13 07:00:04

标签: ios swift uiscrollview

我已经实现了scrollViewWillEndDragging以在用户向下滚动时隐藏视图,一旦向上滚动,视图将立即显示。但是问题是通过使用下面的代码,当用户向上滚动时,该视图会出现,但是当用户的手指离开屏幕时会再次隐藏。是否有更好的解决方案来创建显示或隐藏所需视图的动作?谢谢大家

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        if scrollView == searchCollection{
            if targetContentOffset.pointee.y < scrollView.contentOffset.y {
                // it's going up
                headerViewHeightConstraint.constant = 110
                UIView.animate(withDuration: 0.5, delay: 0, options: [.allowUserInteraction], animations: {
                    self.view.layoutIfNeeded()
                }, completion: nil)
            } else {
                // it's going down
                headerViewHeightConstraint.constant = 0
                UIView.animate(withDuration: 0.5, delay: 0, options: [.allowUserInteraction], animations: {
                    self.view.layoutIfNeeded()
                }, completion: nil)
            }

        }
      }

2 个答案:

答案 0 :(得分:0)

你好,根据我的看法,使用手势

var panGesture = UIPanGestureRecognizer()

//inSide viewDidLoad()
panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGestureAction(_:)))
self.yourScrollview.addGestureRecognizer(panGesture)

//Create function like below
    @objc func panGestureAction(_ gestureRecognizer : UIPanGestureRecognizer) {

        guard gestureRecognizer.view != nil else {return}

        let fileView = gestureRecognizer.view!
        let directionVelocity = gestureRecognizer.velocity(in: myView)
        let translation = gestureRecognizer.translation(in: self.view)
        switch gestureRecognizer.state {

        case .began :

        break
        case .changed:

            if directionVelocity.x > 0 {

                print("swipe right")
            }

            if directionVelocity.x < 0 {

                print("swipe left")
            }

            if directionVelocity.y > 0 {

                print("swipe down")
            }

            if directionVelocity.y < 0 {

               print("swipe up")

            }
        break

    case .ended :

        print("end")

    default:
        break
    }

}

希望它将对您有帮助, 谢谢。

答案 1 :(得分:0)

尝试一下,它在我的工作端。

@interface ViewController (){

    __weak IBOutlet UIView *view3;
    __weak IBOutlet UIView *view2;
    __weak IBOutlet UIView *view1;

    BOOL isshowing;
}

@end

@implementation ViewController

#pragma mark - UIView Controller Life Cycle

- (void)viewDidLoad {

    [super viewDidLoad];
    isshowing = YES;

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

        CGFloat offset = scrollView.contentOffset.y;
        CGFloat viewOffset = view1.frame.size.height + view1.frame.origin.y;

        if (viewOffset > offset) {

            if (isshowing == NO) {
                view1.hidden = NO;
                view1.alpha = 0;
                [UIView animateWithDuration:0.3 animations:^{
                    view1.alpha = 1;
                } completion:^(BOOL finished) {

                }];
            }
            isshowing = YES;



            NSLog(@"View 1 show");
        }
        if (viewOffset < offset) {
            if (isshowing == YES) {
                [UIView animateWithDuration:0.3 animations:^{
                    view1.alpha = 0;
                } completion:^(BOOL finished) {
                    view1.hidden = YES;
                }];
            }


            isshowing = NO;

            NSLog(@"View 1 Hide");

        }

    }