設計思路:給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中類似微信紅點顯示功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!