欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IOS类似android的view.gone/view.visable

程序员文章站 2022-05-28 22:50:42
...

IOS 不像android一样,有神view。gone的方法,可以既隐藏当前控件,也可以使底下控件上移。因此,这个自己写个方法:

例如:

//类似于android里的View.Gone
- (void)ViewGone:(UIView *)senderView{
    if (senderView.hidden == NO) {
        senderView.hidden = YES;
        //获取源控件的View
        NSInteger targetY = senderView.frame.origin.y;
        NSInteger targetHeightY = senderView.frame.origin.y+senderView.frame.size.height+10;
        //
        //    NSLog(@"%d",targetY);
        BOOL isHidden = senderView.hidden;
        if (isHidden) {
            for (UIView *view in self.view.subviews) {
                //            NSLog(@"%@",view);
                NSInteger tempY = view.frame.origin.y;
                //如果遇到view的Y坐标等于目标的Y坐标,且不为hidden的话,则暂停,恐会覆盖调
                if(tempY == targetY && !view.hidden){
                    break;
                }
                if (tempY >= targetHeightY ) {
                    if ([view isMemberOfClass:[UIImageView class]]) {
                        continue;
                    }
                    
                    //上移到目标位置,达到隐藏的效果
                    CGRect tmpFrame = CGRectMake(view.frame.origin.x, view.frame.origin.y-senderView.frame.size.height-10, view.frame.size.width, view.frame.size.height);
                    view.frame = tmpFrame;
                }
            }
            //senderView = nil;
            
        }
    }
    
}

 同理,显示出来的效果是:

//类似于android里的View.Gone
- (void)ViewVis:(UIView *)senderView{
    if (senderView.hidden == YES) {
        
        senderView.hidden = NO;
        //获取源控件的View
        NSInteger targetY = senderView.frame.origin.y;
        NSInteger targetHeightY = senderView.frame.origin.y+senderView.frame.size.height+10;
        //
        //    NSLog(@"%d",targetY);
        BOOL isHidden = senderView.hidden;
        if (!isHidden) {
            for (UIView *view in self.view.subviews) {
                //            NSLog(@"%@",view);
                NSInteger tempY = view.frame.origin.y;
                //如果遇到view的Y坐标等于目标的Y坐标,且不为hidden的话,则暂停,恐会覆盖调
                if(tempY == targetY && !view.hidden && view != senderView){
                    if ([view isMemberOfClass:[UIImageView class]]) {
                        continue;
                    }
                    
                    //上移到目标位置,达到隐藏的效果
                    CGRect tmpFrame = CGRectMake(view.frame.origin.x, view.frame.origin.y+senderView.frame.size.height+10, view.frame.size.width, view.frame.size.height);
                    view.frame = tmpFrame;
                }
                if (tempY >= targetHeightY) {
                    if ([view isMemberOfClass:[UIImageView class]]) {
                        continue;
                    }
                    
                    //上移到目标位置,达到隐藏的效果
                    CGRect tmpFrame = CGRectMake(view.frame.origin.x, view.frame.origin.y+senderView.frame.size.height+10, view.frame.size.width, view.frame.size.height);
                    view.frame = tmpFrame;
                }
            }
            //senderView = nil;
            
        }

    }
}