zoukankan      html  css  js  c++  java
  • 简单的自定义弹框

      作为初学者,很多人都是用的系统自带的弹框,非常的简单,完全不能满足用户的交互,所以这里,我们需要自定义一个弹框,把输入框、图片、按钮等添加到弹框里面。为了避免重复冗余的代码,参考了别人的代码,自己做了一个自定义弹框,可以在项目中使用到。给大家一个思路。  

      这是代码的接口定义,只需要调用一行代码就可以弹出一个自定义的视图啦。还会添加一些动画效果,让弹框弹出跟消失更美观。

    + (void)showPromptBoxWithCustomView:(UIView *)customView;
    
    + (void)promptBoxHiden;
    
    // 不会消失,需要手动点击
    + (void)showPromptBoxWithImage:(UIImage *)image text:(NSString *)text;
    
    // 在几秒有消失
    + (void)showPromptBoxWithText:(NSString *)text;

      动画效果跟显示框效果也是必须有的

    /**
     *  显隐提示框
     */
    - (void)promptBoxHidenWithView:(SQPromptBox *)view speed:(CGFloat)speed {
        [UIView animateWithDuration:speed animations:^{
            view.coverView.alpha = 0.0f;
        } completion:^(BOOL finished) {
            if (finished) {
                [view.coverView removeFromSuperview];
            }
        }];
    }
    
    /**
     *  动画效果
     */
    - (void)presentWithDuration:(CGFloat)duration speed:(CGFloat)speed {
        [UIView animateWithDuration:speed animations:^{
            self.alpha = 1.0f;
            self.coverView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5f];
        } completion:^(BOOL finished) {
            if (finished) {
                if (!self.customView) {
                    [self performBlock:^{
                        [self promptBoxHidenWithView:self speed:speed];
                    } afterDelay:duration];
                }
            }
        }];
    }

      

      还有一步很重要,需要通过字数来计算label的高度,如果没有这一步完成的效果就没有那么好看了。

    - (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize {
        NSDictionary *attrs = @{NSFontAttributeName : font};
        return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    }

      这是一个布局的方法,需要计算好,不然就不能自适应不能尺寸的iPhone了

    - (void)layoutSubviews {
        
        if (!self.customView) {
            CGSize textSize = [self sizeWithText:self.textLabel.text font:self.textLabel.font maxSize:CGSizeMake(kWidth - 2 * kMagin, MAXFLOAT)];
            if (self.imageView.image) {
                self.imageView.frame = CGRectMake((self.frame.size.width - self.image.size.width) / 2.0f, kMagin, self.image.size.width, self.image.size.height);
                self.textLabel.frame = CGRectMake((kWidth - textSize.width) / 2.0f, CGRectGetMaxY(self.imageView.frame) +  kMagin, textSize.width, textSize.height);
                
            }else {
                CGFloat height = textSize.height < kHeight ? kHeight : textSize.height;
                self.textLabel.frame = CGRectMake((kWidth - textSize.width) / 2.0f, kMagin, textSize.width, height);
            }
            CGRect rect = [self typeFrame];
            rect.size.height = CGRectGetMaxY(self.textLabel.frame) + kMagin;
            self.frame = rect;
        }
    }

      

      这是把弹框和蒙版加载当前显示控制器的view上,通过下面的方法来遍历,能获取到控制器

    /**
     *  得到currentViewController
     */
    - (UIViewController *)getCurrenViewController {
        UIViewController *currenVIewController = nil;
        
        // 返回一个app的实例,keyWindow(只读)
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (window.windowLevel != UIWindowLevelNormal) {
            NSArray *windows = [[UIApplication sharedApplication] windows];
            for (UIWindow *tempWindow in windows) {
                if (tempWindow.windowLevel == UIWindowLevelNormal) {
                    window = tempWindow;
                    break;
                }
            }
        }
        
        UIView *frontView = [[window subviews] objectAtIndex:0];
        // 接受下一个响应者
        id nextResponder = [frontView nextResponder];
        
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            currenVIewController = nextResponder;
        } else {
            currenVIewController = window.rootViewController;
        }
        return currenVIewController;
    }

      因为代码比较多,就没有一一展示出来,就提供一个小小的思路,供大家参考。

      最后就是上面代码的网站
          https://github.com/empty-sq/-.git

  • 相关阅读:
    如何在IE, Firefox, Opera浏览器中支持页面弹出并回传数据
    访问IIS 6.0 web服务时, 显示service unavailable
    新blog新气象
    jQuery 参考手册 选择器
    如何在博客中添加目录并链接到当前页面中的某个位置?
    Excel中左中右截取及去空格
    jQuery 遍历 slice() 方法
    如何知道一个工作簿中有多少个工作表?
    jQuery 选择器
    邮政编码查询网址
  • 原文地址:https://www.cnblogs.com/shensq/p/5203445.html
Copyright © 2011-2022 走看看