Ios 使用UIscrollview的委托函数上下滚动以隐藏和显示视图

Ios 使用UIscrollview的委托函数上下滚动以隐藏和显示视图,ios,swift,uiscrollview,Ios,Swift,Uiscrollview,我已经实现了ScrollViewWillendDraging在用户向下滚动时隐藏视图,一旦向上滚动,视图将立即显示。但问题是使用下面的代码,当用户向上滚动时,视图会出现,但当用户手指离开屏幕时,视图会再次隐藏。对于创建显示和隐藏所需视图的操作,是否有更好的解决方案?谢谢大家 func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset:

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

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)
            }

        }
      }
func-scrollViewWillendDraging(scrollView:UIScrollView,withVelocity:CGPoint,targetContentOffset:UnsafemeutablePointer){
如果scrollView==searchCollection{
如果targetContentOffset.pointee.y
您好,根据我的意见,请使用Pangestree

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
    }

}
var-pangestrue=uipangestruerecognizer()
//内部viewDidLoad()
panGesture=UIPangESTURE识别器(目标:self,操作:#选择器(self.panGestureAction(:))
self.yourScrollview.AddGestureRecognitor(panGesture)
//创建如下所示的函数
@objc func panGestureAction(uUgestureRecognitor:UIPANGESTURERecognitor){
guard gestureRecognizer.view!=nil else{return}
让fileView=gestureRecognizer.view!
让directionVelocity=GestureRecognitor.velocity(在:myView中)
let translation=手势识别器.translation(在:self.view中)
切换手势识别器.state{
案例.开始:
打破
案例。更改:
如果directionVelocity.x>0{
打印(“向右滑动”)
}
如果方向速度.x<0{
打印(“向左滑动”)
}
如果directionVelocity.y>0{
打印(“向下滑动”)
}
如果方向速度.y<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");

        }

    }
@界面视图控制器(){
__弱IBUIView*view3;
__弱IBUIView*view2;
__弱IBUIView*view1;
布尔伊秀;
}
@结束
@实现视图控制器
#pragma标记-UIView控制器生命周期
-(无效)viewDidLoad{
[超级视图下载];
isshowing=是;
}
-(void)scrollViewDidScroll:(UIScrollView*)scrollView;{
CGFloat offset=scrollView.contentOffset.y;
CGFloat viewOffset=view1.frame.size.height+view1.frame.origin.y;
如果(视图偏移>偏移){
如果(isshowing==否){
view1.hidden=否;
view1.alpha=0;
[UIView animateWithDuration:0.3动画:^{
视图1.alpha=1;
}完成:^(布尔完成){
}];
}
isshowing=是;
NSLog(“视图1显示”);
}
如果(视图偏移<偏移){
如果(isshowing==是){
[UIView animateWithDuration:0.3动画:^{
view1.alpha=0;
}完成:^(布尔完成){
view1.hidden=是;
}];
}
isshowing=否;
NSLog(@“视图1隐藏”);
}
}

问题和标记的语言是swift而非客观的cbro我尝试了您的代码,当用户向上滚动时视图无法显示,只有当滚动视图滚动到顶部时视图才会显示。您必须计算高度和y轴的总和,以便再次检查滚动视图偏移。给我你的代码,让我检查一下;兄弟,我尝试了你的代码,我的uicollectionview变得不可滚动,我做错什么了吗?@klkl你能在集合视图中添加你的手势,比如“self.collectionview.addgestureRecograinizer(Pangestre)”是的,我试过了,但是uicollectionview仍然没有scrollable@klkl请检查序列图像板中的层次结构UIView。@klkl您可以设置collectionView的用户交互吗?请确认是否设置为false,然后:-“self.parentView.addgestureRecognitizer(panGesture)”