美文网首页工作生活
MBProgressHUD 添加关闭按钮

MBProgressHUD 添加关闭按钮

作者: 白色天空729 | 来源:发表于2019-07-03 15:36 被阅读0次

    找到文件 MBProgressHUD

    - (void)drawRect:(CGRect)rect {
        CGContextRef context = UIGraphicsGetCurrentContext();
        UIGraphicsPushContext(context);
    
        if (self.dimBackground) {
            //Gradient colours
            size_t gradLocationsNum = 2;
            CGFloat gradLocations[2] = {0.0f, 1.0f};
            CGFloat gradColors[8] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.75f}; 
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);
            CGColorSpaceRelease(colorSpace);
            //Gradient center
            CGPoint gradCenter= CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
            //Gradient radius
            float gradRadius = MIN(self.bounds.size.width , self.bounds.size.height) ;
            //Gradient draw
            CGContextDrawRadialGradient (context, gradient, gradCenter,
                                         0, gradCenter, gradRadius,
                                         kCGGradientDrawsAfterEndLocation);
            CGGradientRelease(gradient);
        }
    
        // Set background rect color
        if (self.color) {
            CGContextSetFillColorWithColor(context, self.color.CGColor);
        } else {
            CGContextSetGrayFillColor(context, 0.0f, self.opacity);
        }
    
        
        // Center HUD
        CGRect allRect = self.bounds;
        // Draw rounded HUD backgroud rect
        CGRect boxRect = CGRectMake(round((allRect.size.width - size.width) / 2) + self.xOffset,
                                    round((allRect.size.height - size.height) / 2) + self.yOffset, size.width, size.height);
        float radius = self.cornerRadius;
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect));
        CGContextAddArc(context, CGRectGetMaxX(boxRect) - radius, CGRectGetMinY(boxRect) + radius, radius, 3 * (float)M_PI / 2, 0, 0);
        CGContextAddArc(context, CGRectGetMaxX(boxRect) - radius, CGRectGetMaxY(boxRect) - radius, radius, 0, (float)M_PI / 2, 0);
        CGContextAddArc(context, CGRectGetMinX(boxRect) + radius, CGRectGetMaxY(boxRect) - radius, radius, (float)M_PI / 2, (float)M_PI, 0);
        CGContextAddArc(context, CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect) + radius, radius, (float)M_PI, 3 * (float)M_PI / 2, 0);
        CGContextClosePath(context);
        CGContextFillPath(context);
    
        UIGraphicsPopContext();
        }
    

    在drawRect函数最后添加:

        UIButton *view = [[UIButton alloc] initWithFrame:CGRectMake(boxRect.origin.x, boxRect.origin.y, 25, 25)];
            view.backgroundColor = UIColor.darkGrayColor;
        view.clipsToBounds = YES;
        view.layer.cornerRadius = 12.5;
        [view setTitle:@"✕" forState:UIControlStateNormal];
        [view addTarget:self action:@selector(touchToDismiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:view];
    
    - (void)touchToDismiss {
        [self hide:YES];
        /// 发出通知以便后期接收处理对应的内容
        [[NSNotificationCenter defaultCenter] postNotificationName:@"cancelTask" object:nil];
    }
    
    效果图: QQ20190703-153606-HD.gif

    相关文章

      网友评论

        本文标题:MBProgressHUD 添加关闭按钮

        本文链接:https://www.haomeiwen.com/subject/luzqhctx.html