這篇文章為您講述IOS 相似微信紅點顯示功用的文章,詳細辦法請看引見
設計思緒:給UIView添加一個分類 一切的視圖都可以依據需求來停止紅點顯示
#import <UIKit/UIKit.h> @interface UIView (CHRRedDot) @property (readonly, nonatomic) CALayer * chr_redDotLayer; /** 紅點圓心的地位,與各個邊之間的間隔。假如間隔<=0,則疏忽間隔 */ @property (nonatomic, assign) UIEdgeInsets chr_redDotEdgeInsets; /** 紅點的半徑,默許為4 */ @property (nonatomic, assign) CGFloat chr_redDotRadius; /** 紅點的顏色,默許為0xFF5A5A */ @property (nonatomic, strong) UIColor * chr_redDotColor; /** 紅點能否顯示 */ @property (nonatomic, assign) BOOL chr_redDotShow; @end #pragma mark - method - (void)chr_updateRedDot { CALayer *redDot = self.chr_redDotLayer; if (self.chr_redDotShow) { if (redDot == nil) { redDot = [CALayer layer]; self.chr_redDotLayer = redDot; [self.layer addSublayer:redDot]; } redDot.backgroundColor = self.chr_redDotColor.CGColor; [self chr_layoutRedDot]; } else { [redDot removeFromSuperlayer]; self.chr_redDotLayer = nil; } } - (void)chr_layoutRedDot { CALayer *redDot = self.chr_redDotLayer; if (redDot == nil) return; CGFloat radius = self.chr_redDotRadius; redDot.cornerRadius = radius; UIEdgeInsets edgeInsets = self.chr_redDotEdgeInsets; CGFloat originX = edgeInsets.right <= 0 ? edgeInsets.left - radius : self.bounds.size.width - edgeInsets.right + radius; CGFloat originY = edgeInsets.bottom <= 0 ? edgeInsets.top - radius : self.bounds.size.height - edgeInsets.bottom + radius; CGFloat length = radius * 2; redDot.frame = CGRectMake(originX, originY, length, length); }
經過本文的學習希望對您理解和學習IOS開發的相關知識有一些好的協助.感激關注本站.我們將為您搜集更多更好的IOS開發教程.
【ios 相似微信紅點顯示功用】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!