iOS 实现角标 新消息提示红点 数字角标

镔哥今天写写实习新消息提示的小圆圈数字角标

直接上代码吧。

1:直接复杂uibarButton类

//

//  UIBarButtonItem+Badge.h

//  therichest

//

//  Created by 淘股 on 2015-05-05.

//  Copyright (c) 2015 taogu Inc. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (Badge)

@property (strong,
nonatomic) UILabel *badge;

// Badge value to be display

@property (nonatomic)
NSString *badgeValue;

// Badge background color

@property (nonatomic)
UIColor *badgeBGColor;

// Badge text color

@property (nonatomic)
UIColor *badgeTextColor;

// Badge font

@property (nonatomic)
UIFont *badgeFont;

// Padding value for the badge

@property (nonatomic)
CGFloat badgePadding;

// Minimum size badge to small

@property (nonatomic)
CGFloat badgeMinSize;

// Values for offseting the badge over the BarButtonItem you picked

@property (nonatomic)
CGFloat badgeOriginX;

@property (nonatomic)
CGFloat badgeOriginY;

// In case of numbers, remove the badge when reaching zero

@property
BOOL shouldHideBadgeAtZero;

// Badge has a bounce animation when value changes

@property
BOOL shouldAnimateBadge;

@end

//

//  UIBarButtonItem+Badge.h

//  therichest

//

//  Created by 淘股 on 2015-05-05.

//  Copyright (c) 2015 taogu Inc. All rights reserved.

//

#import <objc/runtime.h>

#import "UIBarButtonItem+Badge.h"

NSString
const *badgeKey =
@"badgeKey";

NSString
const *badgeBGColorKey =
@"badgeBGColorKey";

NSString
const *badgeTextColorKey =
@"badgeTextColorKey";

NSString
const *badgeFontKey =
@"badgeFontKey";

NSString
const *badgePaddingKey =
@"badgePaddingKey";

NSString
const *badgeMinSizeKey =
@"badgeMinSizeKey";

NSString
const *badgeOriginXKey =
@"badgeOriginXKey";

NSString
const *badgeOriginYKey =
@"badgeOriginYKey";

NSString
const *shouldHideBadgeAtZeroKey =
@"shouldHideBadgeAtZeroKey";

NSString
const *shouldAnimateBadgeKey =
@"shouldAnimateBadgeKey";

NSString
const *badgeValueKey =
@"badgeValueKey";

@implementation UIBarButtonItem (Badge)

@dynamic badgeValue, badgeBGColor, badgeTextColor, badgeFont;

@dynamic badgePadding, badgeMinSize, badgeOriginX, badgeOriginY;

@dynamic shouldHideBadgeAtZero, shouldAnimateBadge;

- (void)badgeInit

{

// Default design initialization

self.badgeBGColor   = [UIColor
redColor];

self.badgeTextColor = [UIColor
whiteColor];

self.badgeFont      = [UIFont
systemFontOfSize:12.0];

self.badgePadding   =
6;

self.badgeMinSize   =
8;

self.badgeOriginX   =
self.customView.frame.size.width -
self.badge.frame.size.width/2;

self.badgeOriginY   = -4;

self.shouldHideBadgeAtZero =
YES;

self.shouldAnimateBadge =
YES;

// Avoids badge to be clipped when animating its scale

self.customView.clipsToBounds =
NO;

}

#pragma mark - Utility methods

// Handle badge display when its properties have been changed (color, font, ...)

- (void)refreshBadge

{

// Change new attributes

self.badge.textColor        =
self.badgeTextColor;

self.badge.backgroundColor  =
self.badgeBGColor;

self.badge.font             =
self.badgeFont;

}

- (CGSize) badgeExpectedSize

{

// When the value changes the badge could need to get bigger

// Calculate expected size to fit new value

// Use an intermediate label to get expected size thanks to sizeToFit

// We don‘t call sizeToFit on the true label to avoid bad display

UILabel *frameLabel = [self
duplicateLabel:self.badge];

[frameLabel
sizeToFit];

CGSize expectedLabelSize = frameLabel.frame.size;

return expectedLabelSize;

}

- (void)updateBadgeFrame

{

CGSize expectedLabelSize = [self
badgeExpectedSize];

// Make sure that for small value, the badge will be big enough

CGFloat minHeight = expectedLabelSize.height;

// Using a const we make sure the badge respect the minimum size

minHeight = (minHeight <
self.badgeMinSize) ?
self.badgeMinSize : expectedLabelSize.height;

CGFloat minWidth = expectedLabelSize.width;

CGFloat padding =
self.badgePadding;

// Using const we make sure the badge doesn‘t get too smal

minWidth = (minWidth < minHeight) ? minHeight : expectedLabelSize.width;

self.badge.frame =
CGRectMake(self.badgeOriginX,
self.badgeOriginY, minWidth + padding, minHeight + padding);

self.badge.layer.cornerRadius = (minHeight + padding) /
2;

self.badge.layer.masksToBounds
= YES;

}

// Handle the badge changing value

- (void)updateBadgeValueAnimated:(BOOL)animated

{

// Bounce animation on badge if value changed and if animation authorized

if (animated && self.shouldAnimateBadge && ![self.badge.text
isEqualToString:self.badgeValue]) {

CABasicAnimation * animation = [CABasicAnimation
animationWithKeyPath:@"transform.scale"];

[animation setFromValue:[NSNumber
numberWithFloat:1.5]];

[animation
setToValue:[NSNumber
numberWithFloat:1]];

[animation
setDuration:0.2];

[animation setTimingFunction:[CAMediaTimingFunction
functionWithControlPoints:.4f :1.3f :1.f
:1.f]];

[self.badge.layer
addAnimation:animation
forKey:@"bounceAnimation"];

}

// Set the new value

self.badge.text =
self.badgeValue;

// Animate the size modification if needed

NSTimeInterval duration = animated ?
0.2 : 0;

[UIView
animateWithDuration:duration animations:^{

[self
updateBadgeFrame];

}];

}

- (UILabel *)duplicateLabel:(UILabel *)labelToCopy

{

UILabel *duplicateLabel = [[UILabel
alloc] initWithFrame:labelToCopy.frame];

duplicateLabel.text = labelToCopy.text;

duplicateLabel.font = labelToCopy.font;

return duplicateLabel;

}

- (void)removeBadge

{

// Animate badge removal

[UIView
animateWithDuration:0.2
animations:^{

self.badge.transform
= CGAffineTransformMakeScale(0,
0);

}
completion:^(BOOL finished) {

[self.badge
removeFromSuperview];

self.badge =
nil;

}];

}

#pragma mark - getters/setters

-(UILabel*) badge {

return
objc_getAssociatedObject(self, &badgeKey);

}

-(void)setBadge:(UILabel *)badgeLabel

{

objc_setAssociatedObject(self, &badgeKey, badgeLabel,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

// Badge value to be display

-(NSString *)badgeValue {

return
objc_getAssociatedObject(self, &badgeValueKey);

}

-(void) setBadgeValue:(NSString *)badgeValue

{

objc_setAssociatedObject(self, &badgeValueKey, badgeValue,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);

// When changing the badge value check if we need to remove the badge

if (!badgeValue || [badgeValue
isEqualToString:@""] || ([badgeValue
isEqualToString:@"0"] &&
self.shouldHideBadgeAtZero)) {

[self
removeBadge];

}
else if (!self.badge) {

// Create a new badge because not existing

self.badge                      = [[UILabel
alloc] initWithFrame:CGRectMake(self.badgeOriginX,
self.badgeOriginY,
20, 20)];

self.badge.textColor            =
self.badgeTextColor;

self.badge.backgroundColor      =
self.badgeBGColor;

self.badge.font                 =
self.badgeFont;

self.badge.textAlignment        =
NSTextAlignmentCenter;

[self
badgeInit];

[self.customView
addSubview:self.badge];

[self
updateBadgeValueAnimated:NO];

}
else {

[self
updateBadgeValueAnimated:YES];

}

}

// Badge background color

-(UIColor *)badgeBGColor {

return
objc_getAssociatedObject(self, &badgeBGColorKey);

}

-(void)setBadgeBGColor:(UIColor *)badgeBGColor

{

objc_setAssociatedObject(self, &badgeBGColorKey, badgeBGColor,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);

if (self.badge) {

[self
refreshBadge];

}

}

// Badge text color

-(UIColor *)badgeTextColor {

return
objc_getAssociatedObject(self, &badgeTextColorKey);

}

-(void)setBadgeTextColor:(UIColor *)badgeTextColor

{

objc_setAssociatedObject(self, &badgeTextColorKey,
badgeTextColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

if (self.badge) {

[self
refreshBadge];

}

}

// Badge font

-(UIFont *)badgeFont {

return
objc_getAssociatedObject(self, &badgeFontKey);

}

-(void)setBadgeFont:(UIFont *)badgeFont

{

objc_setAssociatedObject(self, &badgeFontKey, badgeFont,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);

if (self.badge) {

[self
refreshBadge];

}

}

// Padding value for the badge

-(CGFloat) badgePadding {

NSNumber *number =
objc_getAssociatedObject(self, &badgePaddingKey);

return number.floatValue;

}

-(void) setBadgePadding:(CGFloat)badgePadding

{

NSNumber *number = [NSNumber
numberWithDouble:badgePadding];

objc_setAssociatedObject(self, &badgePaddingKey, number,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);

if (self.badge) {

[self
updateBadgeFrame];

}

}

// Minimum size badge to small

-(CGFloat) badgeMinSize {

NSNumber *number =
objc_getAssociatedObject(self, &badgeMinSizeKey);

return number.floatValue;

}

-(void) setBadgeMinSize:(CGFloat)badgeMinSize

{

NSNumber *number = [NSNumber
numberWithDouble:badgeMinSize];

objc_setAssociatedObject(self, &badgeMinSizeKey, number,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);

if (self.badge) {

[self
updateBadgeFrame];

}

}

// Values for offseting the badge over the BarButtonItem you picked

-(CGFloat) badgeOriginX {

NSNumber *number =
objc_getAssociatedObject(self, &badgeOriginXKey);

return number.floatValue;

}

-(void) setBadgeOriginX:(CGFloat)badgeOriginX

{

NSNumber *number = [NSNumber
numberWithDouble:badgeOriginX];

objc_setAssociatedObject(self, &badgeOriginXKey, number,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);

if (self.badge) {

[self
updateBadgeFrame];

}

}

-(CGFloat) badgeOriginY {

NSNumber *number =
objc_getAssociatedObject(self, &badgeOriginYKey);

return number.floatValue;

}

-(void) setBadgeOriginY:(CGFloat)badgeOriginY

{

NSNumber *number = [NSNumber
numberWithDouble:badgeOriginY];

objc_setAssociatedObject(self, &badgeOriginYKey, number,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);

if (self.badge) {

[self
updateBadgeFrame];

}

}

// In case of numbers, remove the badge when reaching zero

-(BOOL) shouldHideBadgeAtZero {

NSNumber *number =
objc_getAssociatedObject(self, &shouldHideBadgeAtZeroKey);

return number.boolValue;

}

- (void)setShouldHideBadgeAtZero:(BOOL)shouldHideBadgeAtZero

{

NSNumber *number = [NSNumber
numberWithBool:shouldHideBadgeAtZero];

objc_setAssociatedObject(self, &shouldHideBadgeAtZeroKey,
number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

// Badge has a bounce animation when value changes

-(BOOL) shouldAnimateBadge {

NSNumber *number =
objc_getAssociatedObject(self, &shouldAnimateBadgeKey);

return number.boolValue;

}

- (void)setShouldAnimateBadge:(BOOL)shouldAnimateBadge

{

NSNumber *number = [NSNumber
numberWithBool:shouldAnimateBadge];

objc_setAssociatedObject(self, &shouldAnimateBadgeKey,
number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

@end

第二部实现调用

// Build your regular UIBarButtonItem with Custom View

UIImage *image = [UIImage
imageNamed:@"someImage"];

UIButton *button = [UIButton
buttonWithType:UIButtonTypeCustom];

button.frame =
CGRectMake(0,0,image.size.width, image.size.height);

[button addTarget:self
action:@selector(buttonPress:)
forControlEvents:UIControlEventTouchDown];

[button setBackgroundImage:image
forState:UIControlStateNormal];

// 角标测试写1

UIBarButtonItem *navLeftButton = [[UIBarButtonItem
alloc] initWithCustomView:button];

self.navigationItem.leftBarButtonItem = navLeftButton;

self.navigationItem.leftBarButtonItem.badgeValue
= @"1";

self.navigationItem.leftBarButtonItem.badgeBGColor
= self.navigationController.navigationBar.tintColor;

#pragma mark - Private

-(void)buttonPress:(id)sender

{

self.navigationItem.leftBarButtonItem.badgeValue
= @"";//方法调用直接清楚角标

}

时间: 2024-12-24 14:50:14

iOS 实现角标 新消息提示红点 数字角标的相关文章

仿微信新消息提示音

怕有些人不知道怎么进入微信的新消息提示音功能,我这里说下操作步骤: 打开微信----我---设置---新消息提醒---新消息提示音. 经过以上的步骤就进入了这样的界面 具体实现的步骤. 难点之一:获取到手机系统的提示音,并将它们显示在一个listview里面. 参考如下代码: // 获得RingtoneManager对象   RingtoneManager manager = new RingtoneManager(this);   // 设置RingtoneManager对象的类型为TYPE_

高仿微信新消息提示音功能

最近公司在做一个项目,有一个切换消息提示音的功能,可以切换本应用收到消息的提示音,而不影响系统提示音.我就按照微信的那个样式进行了编程,最终得到想要的效果. 转载请注明出处,谢谢:http://blog.csdn.net/harryweasley/article/details/46408037 怕有些人不知道怎么进入微信的新消息提示音功能,我这里说下操作步骤: 打开微信----我---设置---新消息提醒---新消息提示音. 经过以上的步骤就进入了这样的界面 这个是微信的效果图. 下面是我自己

CSS+jQuery实现可关闭的智能定位的浮动消息提示框

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>CSS+jQuery实现可关闭的智能定位的

IOS 新消息通知提示-声音、震动

一.APNS 1.注册 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];复制代码2.服务器推送(JAVA) PushNotificationPayload payLoad =  PushNotificationPayl

自定义iOS 中推送消息 提示框

看到标题你可能会觉得奇怪 推送消息提示框不是系统自己弹出来的吗? 为什么还要自己自定义呢? 因为项目需求是这样的:最近需要做一个客服系统 包括店铺客服和官方客服两个模块 如果用户当前不在客服界面的时候 要求无论是在app前台 还是app退到后台 顶部都要弹出系统的那种消息提示框 这样的需求 我们就只能自定义一个在app内 弹出消息提示框 实现步骤如下: 1.我们自定义一个view 为 STPushView 推送消息的提示框view

iOS开发系列之常用自定义控件开发集—自定义状态栏消息提示控件开发

在实际开发中消息提示时很常见的需求,为了个性化和拥有简洁的UI状态栏提示是比较好的方案,好处很多如:不遮挡主UI,新意,下面贴出实现代码. WHC_StatusBarMessage.h头文件如下: // // WHCStatusBarMessage.m // WHCStatusBarMessage // // Created by apple on 14-3-28. // Copyright (c) 2014年 apple. All rights reserved. // #import "WH

从仿QQ消息提示框来谈弹出式对话框

<代码里的世界> -UI篇 用文字札记描绘自己 android学习之路 转载请保留出处 by Qiao http://blog.csdn.net/qiaoidea/article/details/45896477 [导航] - 自定义弹出式对话框的简单用法 列举各种常见的对话框实现方案 1.概述 android原生控件向来以丑著称(新推出的Material Design当另说),因此几乎所有的应用都会特殊定制自己的UI样式.而其中弹出式提示框的定制尤为常见,本篇我们将从模仿QQ退出提示框来看一

View(视图)——AutoCompleteTextView 、Spinner和消息提示

一.AutoCompleteTextView 自动提示文本输入框 1.属性:不区分大小写. 2.ArrayAdapter  适配器 数据源是String[ ]或集合,保存要提示的文本:layout文件. 3.completionThreshold:设置提示需要的最少字符数,默认是2. 二.Spinner 下拉列表 1.ArrayAdapter  适配器 数据源是String[ ]或集合,保存要提示的文本:layout文件. 2.监听器   AdapterView.OnItemSelectedLi

winform消息提示框

摘自:http://www.cnblogs.com/risen/archive/2008/01/15/1039751.html public partial class AlertForm : Form    {        /*         * 在类AlertForm定义的下方,         * 我们创建用于显示的字符串和其颜色的变量,         * 再定义几个Rectangle对象的变量用于放置标题.         * 提示内容以及可以拖动窗体的区域和关闭按钮的区域.