欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS中类似微信红点显示功能

程序员文章站 2023-12-22 10:19:22
设计思路:给uiview增加一个分类 所有的视图都可以根据需要来进行红点显示 #import @interface...

设计思路:给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中类似微信红点显示功能,希望对大家有所帮助

上一篇:

下一篇: