码迷,mamicode.com
首页 > 微信 > 详细

ios 类似微信红点显示功能

时间:2016-12-18 09:54:31      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:设计   frame   super   ado   self   readonly   set   blog   end   

设计思路:给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 类似微信红点显示功能

标签:设计   frame   super   ado   self   readonly   set   blog   end   

原文地址:http://www.cnblogs.com/rongStep/p/6193683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!