ios 上下滚动以隐藏和显示带有UIscrollview代理功能的视图

yi0zb3m4  于 2023-01-06  发布在  iOS
关注(0)|答案(3)|浏览(154)

我已经实现了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)
            }

        }
      }
qacovj5a

qacovj5a1#

你好,根据我的意见使用pangesture

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
    }

}

希望对你有帮助,谢谢。

idfiyjo8

idfiyjo82#

试试这个,它在我这边起作用了。

@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");

        }

    }
7kqas0il

7kqas0il3#

我的Swift解决方案

var isShowing: Bool = true
var animationDuration: CGFloat {
    0.3
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset.y
    let viewOffset = (self.frame.size.height / 2) + self.frame.origin.y
    if viewOffset < offset, !self.isShowing {
        self.isHidden = false
        self.alpha = 0
        UIView.animate(withDuration: self.animationDuration) {
            self.alpha = 1
        }
        self.isShowing = true
    } else if viewOffset > offset, self.isShowing {
        UIView.animate(withDuration: self.animationDuration, animations: {
            self.alpha = 0
        }, completion: { (finished) in
            self.isHidden = true
        })
        self.isShowing = false
    }
}

相关问题