当应用程序在前台时,有没有办法显示本地通知横幅

时间:2015-10-26 12:12:14

标签: objective-c ios7

通知横幅显示在后台模式中,但不显示在正常的前景中。

当我检测到本地通知时,我正显示UIAlertView / UIAlertController(ios 7,ios 8),但如果有相同的通知横幅弹出,那将会很不错我正在使用该应用程序的背景。

我知道有些应用程序正在使用自定义动画来执行此操作,但我正在寻找可以帮助我在前台模式下生成横幅的API。或者提示如何制作看起来像ios默认横幅的自定义动画。

2 个答案:

答案 0 :(得分:2)

当然!

您需要为此绘制自己的自定义UIView。我在我的项目中这样做。只是为了让您了解我的自定义视图的drawRect:的样子:

还有其他初始化和动画代码,我不相信你可以建立它们!

- (void)drawRect:(CGRect)iRect {
    CGRect aMarkFrame = iRect;
    aMarkFrame.origin.x = iRect.origin.x + 6;
    aMarkFrame.origin.y = iRect.origin.y + 6;
    aMarkFrame.size.height = iRect.size.height;
    aMarkFrame.size.width = iRect.size.width - 12;

    CGContextRef aContext = UIGraphicsGetCurrentContext();
    [self drawRoundedRectWithContext:aContext withRect:aMarkFrame];
    [self drawViewGradientWithContext:aContext withRect:aMarkFrame];
}


- (void)drawRoundedRectWithContext:(CGContextRef)iContext withRect:(CGRect)iRect {
    // This method draws arcs and fills color
    CGContextSaveGState(iContext);

    CGFloat aRadius = kAlertViewCornerRadius;
    CGFloat aBuffer = floorf(CGRectGetMaxY(iRect) * kAlertViewBuffer);
    CGFloat aMaxX = floorf(CGRectGetMaxX(iRect) - aBuffer);
    CGFloat aMaxY = floorf(CGRectGetMaxY(iRect) - aBuffer);
    CGFloat aMinX = floorf(CGRectGetMinX(iRect) + aBuffer);
    CGFloat aMinY = floorf(CGRectGetMinY(iRect) + aBuffer);

    CGContextBeginPath(iContext);
    CGContextSetLineWidth(iContext, kAlertViewBorderWidth);
    CGContextSetStrokeColorWithColor(iContext, [[UIColor whiteColor] CGColor]);
    CGContextAddArc(iContext, aMaxX - aRadius, aMinY + aRadius, aRadius, M_PI + M_PI/2, 0, 0);
    CGContextAddArc(iContext, aMaxX - aRadius, aMaxY - aRadius, aRadius, 0, M_PI/2, 0);
    CGContextAddArc(iContext, aMinX + aRadius, aMaxY - aRadius, aRadius, M_PI/2, M_PI, 0);
    CGContextAddArc(iContext, aMinX + aRadius, aMinY + aRadius, aRadius, M_PI, M_PI + M_PI/2, 0);
    CGContextClosePath(iContext);
    CGContextStrokePath(iContext);
    CGContextRestoreGState(iContext);
}


- (void)drawViewGradientWithContext:(CGContextRef)iContext withRect:(CGRect)iRect {
    // Draws the View Frame with Quartz
    CGContextSaveGState(iContext);

    CGRect aMarkFrame = iRect;
    aMarkFrame.origin.x = iRect.origin.x + 1;
    aMarkFrame.origin.y = iRect.origin.y + 1;
    aMarkFrame.size.width = iRect.size.width - 2;
    aMarkFrame.size.height = iRect.size.height - 2;

    CGFloat aRadius = kAlertViewCornerRadius;
    CGFloat aBuffer = CGRectGetMaxY(aMarkFrame) * kAlertViewBuffer;
    CGFloat aMaxX = CGRectGetMaxX(aMarkFrame) - aBuffer;
    CGFloat aMaxY = CGRectGetMaxY(aMarkFrame) - aBuffer;
    CGFloat aMinX = CGRectGetMinX(aMarkFrame) + aBuffer;
    CGFloat aMinY = CGRectGetMinY(aMarkFrame) + aBuffer;

    CGContextBeginPath(iContext);
    CGContextAddArc(iContext, aMaxX - aRadius, aMinY + aRadius, aRadius, M_PI + M_PI/2, 0, 0);
    CGContextAddArc(iContext, aMaxX - aRadius, aMaxY - aRadius, aRadius, 0, M_PI/2, 0);
    CGContextAddArc(iContext, aMinX + aRadius, aMaxY - aRadius, aRadius, M_PI/2, M_PI, 0);
    CGContextAddArc(iContext, aMinX + aRadius, aMinY + aRadius, aRadius, M_PI, M_PI + M_PI/2, 0);
    CGContextClip(iContext);

    size_t aNumberOfLocations = 2;
    CGFloat aLocations[2] = {0.0, 0.5};
    CGFloat aComponents[8] = {0.510, 0.514, 0.514, 0.8, 0.102, 0.102, 0.110, 0.8};

    CGColorSpaceRef aColorSpace;
    CGGradientRef aGradient;
    aColorSpace = CGColorSpaceCreateDeviceRGB();
    aGradient = CGGradientCreateWithColorComponents (aColorSpace, aComponents, aLocations, aNumberOfLocations);

    CGPoint aStartPoint;
    CGPoint anEndPoint;
    aStartPoint.x = 0;
    aStartPoint.y = 0;
    anEndPoint.x = 0;
    anEndPoint.y = aMaxY;

    CGContextDrawLinearGradient (iContext, aGradient, aStartPoint, anEndPoint, 0);
    CGColorSpaceRelease(aColorSpace);
    CGGradientRelease(aGradient);
    CGContextRestoreGState(iContext);   
}

答案 1 :(得分:2)

你可以使用一些开源代码,

尝试查看此处的通知视图 https://github.com/cjwirth/awesome-ios-ui#notifications-and-alerts,您可以选择自己喜欢的内容并显示消息视图而不是警报。