1. 程式人生 > >iOS 實現角標 新訊息提示紅點 數字角標

iOS 實現角標 新訊息提示紅點 數字角標

轉自:

http://www.mamicode.com/info-detail-648885.html

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 (strongnonatomic

UILabel *badge;

// Badge value to be display

@property (nonatomicNSString *badgeValue;

// Badge background color

@property (nonatomicUIColor *badgeBGColor;

// Badge text color

@property (nonatomicUIColor *badgeTextColor;

// Badge font

@property (nonatomicUIFont *badgeFont;

// Padding value for the badge

@property (nonatomicCGFloat badgePadding;

// Minimum size badge to small

@property (nonatomicCGFloat badgeMinSize;

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

@property (nonatomicCGFloat badgeOriginX;

@property (nonatomicCGFloat 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 = [UIColorwhiteColor];

    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.badgeOriginXself.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 = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];

        [animation setFromValue:[NSNumbernumberWithFloat:1.5]];

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

        [animation setDuration:0.2];

        [animation setTimingFunction:[CAMediaTimingFunctionfunctionWithControlPoints:.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;

    [UIViewanimateWithDuration:duration animations:^{

        [selfupdateBadgeFrame];

    }];

}

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

{

    UILabel *duplicateLabel = [[UILabel allocinitWithFrame:labelToCopy.frame];

    duplicateLabel.text = labelToCopy.text;

    duplicateLabel.font = labelToCopy.font;

    return duplicateLabel;

}

- (void)removeBadge

{

// Animate badge removal

    [UIViewanimateWithDuration:0.2animations:^{

self.badge.transform = CGAffineTransformMakeScale(00);

    } completion:^(BOOL finished) {

        [self.badgeremoveFromSuperview];

        self.badge = nil;

    }];

}

#pragma mark - getters/setters

-(UILabel*) badge {

returnobjc_getAssociatedObject(self, &badgeKey);

}

-(void)setBadge:(UILabel *)badgeLabel

{

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

}

// Badge value to be display

-(NSString *)badgeValue {

returnobjc_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 allocinitWithFrame:CGRectMake(self.badgeOriginX,self.badgeOriginY2020)];

        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];

        [selfupdateBadgeValueAnimated:NO];

    } else {

        [selfupdateBadgeValueAnimated:YES];

    }

}

// Badge background color

-(UIColor *)badgeBGColor {

returnobjc_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 {

returnobjc_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 {

returnobjc_getAssociatedObject(self, &badgeFontKey);

}

-(void)setBadgeFont:(UIFont *)badgeFont

{

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

    if (self.badge) {

        [self refreshBadge];

    }

相關推薦

iOS 實現 訊息提示 數字

轉自: http://www.mamicode.com/info-detail-648885.html 1:直接複雜uibarButton類 // //  UIBarButtonItem+Badge.h //  therichest // //  Cre

實現Web頁面訊息實時提醒

                1. 轉載地址:http://blog.sina.com.cn/s/blog_62cd5a98

IOS使用本地通知(UILocalNotification)刪除應用不刪除通知中心內容

1.本地通知需要當應用在後臺時執行,比如放在applicationDidEnterBackground方法裡; 2.需要註冊通知 - (BOOL)application:(UIApplication

16.IOS 訊息通知提示-聲音、震動

一、APNS  1.註冊 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificatio

ios實現下拉刷,上拉加載

nor llb __weak count stat 實現 sele share mode 1、自定義cell //#import "EnjoyListModel.h" @protocol EnjoyCollectionCellOfViewDelegate <NSO

C# WPF QQ訊息托盤懸浮窗效果實現

原文: C# WPF QQ新訊息托盤懸浮窗效果實現 今天在做一個專案的時候需要這麼一個效果,但是網上找了一會發現並沒有現成的給我參考(複製),但是呢,我千(到)辛(處)萬(抄)苦(襲)想(復)破(制)頭(粘)腦(貼)終於還是給做出來了~嘿嘿嘿 QQ新訊息懸浮窗即:QQ有新訊息時托盤圖示會閃動,此時移動滑鼠

React Native之通知欄訊息提示(ios)

React Native之通知欄訊息提示(ios)   一,需求分析與概述 詳情請檢視:React Native之通知欄訊息提示(android) 二,極光推送註冊與整合 2.1,註冊 詳情請檢視:React Native之通知欄訊息提示(android) 2.2,整合(ios) 第

方便快捷地實現底部導航欄,包含顯示未讀數、提示、動畫等功能

輕量級底部導航欄 目前市場上的App,幾乎都有底部頁籤導航欄,所以我們在開發的時候經常需要用到這個,雖然 github 上有不少已經封裝好的底部導航欄的工具,例如 bottombar,alphaIndicator(仿微信滑動漸變底部控制元件)等,但是這些控制元件由於功能太多,而且也沒有給

Android實現微信底部的帶訊息提示數量

今天一朋友問我一個佈局檔案的實現,就是底部能帶訊息提示的TabHost的實現。其實這個效果之前早就想過,當初的想法就是肯定猜到了用FrameLayout來實現,那麼今天就和大家分享這樣的一個成果吧。而且像這種應用非常的廣泛。好吧有圖有真相,進入正題吧。 實現效果:  

php 實現 redis 佇列,等待有訊息入列

1. 訊息入列程式碼, t1.php: $redis = new Redis(); $redis->connect('127.0.0.1', 6379); while(1){ try{ $value = "value_

MUI數字,用於進行數量提示

用於進行數量提示。 角標的核心類是.mui-badge,預設為實心灰色背景;同時,mui還內建了藍色(blue)、綠色(green)、黃色(yellow)、紅色(red)、紫色(purple)五種色系的

iOS-環信3.0聯絡人列表和未讀訊息提示,頭像和暱稱設定,訊息實時更新

搞來搞去,環信3.0也挺簡單的.一步步說 1.聯絡人列表 新建類LianxirenListViewController整合自EaseUsersListViewController. 在LianxirenListViewController類裡邊可以自由設定聯絡人tabview,自定義ce

iOS 接收訊息通知呼叫系統聲音 震動

新增系統框架: #import <AudioToolbox/AudioToolbox.h> 呼叫震動程式碼: AudioServicesPlaySystemSound(kSystemSo

Element-UI訊息提示元件Message在Vuex中的呼叫實現

在最近的專案開發中,前端部分使用 Vue 開發,整個頁面基於 Element-UI 實現。 由於是單頁面多元件應用,使用了 Vuex 做狀態管理。 為了頁面互動的友好和風格的統一,訊息提醒使用 Element-UI 的 Message訊息提示, 不使用 wi

iOS開發,tableView側滑刪除的實現,或者增多個側滑顯示按鈕

如果想要實現簡單的刪除操作,1 + 2 + 4方法就可以實現 ,如果你要滑動出現多個操作按鈕的話,就要實現方法5了,第五個方法說白了就是集合了前幾個方法,而且可以新增多個按鈕,如果用第5個方法,1、2、4方法可以不實現 //1 - (BOOL)tableView:(UITa

十一,iOS中的按鈕數字提示

1,具體效果如圖 2,按鈕數字紅點其中的巨集定義 #define SPColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)] 3,.h檔案程式碼 #i

HTML實現友好提示框(帶尖的框)

友好提示框的主要實現難點就是哪個尖角的問題,其實只要搞清楚三角形的實現原理,實現這種效果也並不難,下面介紹兩種實現方法,一種是通過邊框實現的三角形通過疊加實現,另一種是通過正方形旋轉實現 首先我們講述一下如何通過邊框實現三角形,首先現建立一個div元素,設定寬

JAVA中實現訊息提示功能程式碼

MessShow.prototype.onunload = function() {     return true; } // 訊息命令事件,要實現自己的連線,請重寫它 MessShow.prototype.oncommand = function() {     window.open(this.acti

Android之自定義(更新)——BadgeView

package com.readystatesoftware.viewbadger; import android.content.Context; import android.content.res.Resources; import android.graphics.Color; import and

JS 實現瀏覽器標題欄閃動提示訊息

<!doctype html> <html> <head> <meta charset='utf-8'> <title></title> <style></st