你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> ios 類似微信紅點顯示功能

ios 類似微信紅點顯示功能

編輯:IOS技巧綜合
[摘要]本文是對ios 類似微信紅點顯示功能的講解,對學習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);
}
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved