iOS中類似微信紅點顯示功能_IOS

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.