当应用程序在前台时,有没有办法显示本地通知横幅
Is there a way to show local notification banner when the app in foreground
通知横幅显示在后台模式,但不在前台,这是正常的。
当我检测到本地通知时,我正在显示 UIAlertView
/ UIAlertController
(ios 7,ios 8),但最好有当我使用该应用程序时,在后台弹出相同的通知横幅。
我知道有些应用程序正在使用自定义动画来执行此操作,但我正在寻找可以帮助我在前台模式下生成横幅的 API。或者关于如何制作看起来像 ios 默认横幅的自定义动画的提示。
是的当然!
您需要为此绘制自己的自定义 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);
}
您可以使用一些开源代码,
尝试在此处查看通知视图
https://github.com/cjwirth/awesome-ios-ui#notifications-and-alerts ,你可以选择任何你喜欢的,并显示消息视图而不是警报。
通知横幅显示在后台模式,但不在前台,这是正常的。
当我检测到本地通知时,我正在显示 UIAlertView
/ UIAlertController
(ios 7,ios 8),但最好有当我使用该应用程序时,在后台弹出相同的通知横幅。
我知道有些应用程序正在使用自定义动画来执行此操作,但我正在寻找可以帮助我在前台模式下生成横幅的 API。或者关于如何制作看起来像 ios 默认横幅的自定义动画的提示。
是的当然!
您需要为此绘制自己的自定义 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);
}
您可以使用一些开源代码,
尝试在此处查看通知视图 https://github.com/cjwirth/awesome-ios-ui#notifications-and-alerts ,你可以选择任何你喜欢的,并显示消息视图而不是警报。