iOS头条干货iOS知识点iOS开发技术分享

iOS气泡提示

2016-04-25  本文已影响2466人  SPIREJ

1.满足不同要求的初始化方法(四个)
2.可选不同的箭头方向(上、下、左、右)
3.字体可自定义
4.字体颜色可自定义
5.背景颜色可自定义
6.背景宽度自定义
7.阴影,阴影的颜色可自定义

示例演示

5)展示
[self.tooltipManager showNextTooltip];
6)演示示例,我这里每点击一下空白处展示下一个


演示示例

更新:下面是我同事封装的,我们现在用这个。特点:简单方便

#import <UIKit/UIKit.h>
@interface XSPopoverView : UIView
+ (XSPopoverView *)showText:(NSString *)text
                     inView:(UIView *)superView
                 relateView:(UIView *)relateView;

@end

.m文件

#import "XSPopoverView.h"

@interface XSPopoverView ()

@property (nonatomic, strong) UILabel *textLB;
@property (nonatomic, strong) CAShapeLayer *triangleLayer;

@end

@implementation XSPopoverView

- (UILabel *)textLB
{
    if (_textLB == nil) {
        _textLB = [[UILabel alloc] initWithFrame:CGRectZero];
        _textLB.backgroundColor = ColorWithHex(0x0, 0.7);
        _textLB.textAlignment = NSTextAlignmentCenter;
        _textLB.numberOfLines = 0;
        _textLB.textColor = Color_whiteColor;
        _textLB.font = XSFont(12);
    }
    return _textLB;
}

- (void)addTriangleRelateView:(UIView *)view
{
    if (_triangleLayer == nil) {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(view.width / 2.0 - 8, view.height)];
        [path addLineToPoint:CGPointMake(view.width  / 2.0, view.height + 10)];
        [path addLineToPoint:CGPointMake(view.width  / 2.0 + 8, view.height)];
        _triangleLayer = [CAShapeLayer layer];
        _triangleLayer.path = path.CGPath;
        _triangleLayer.fillColor = view.backgroundColor.CGColor;
        [self.layer addSublayer:_triangleLayer];
    }
}

- (void)adjustBoundsWithMaxWidth:(CGFloat)maxWidth
{
    if ([XSHelper stringValid:_textLB.text]) {
        [_textLB sizeToFit];
        // 最大宽度,防止超出屏幕
        if (_textLB.width >=maxWidth) {
            _textLB.width = maxWidth;
        }
        [_textLB sizeToFit];
        _textLB.width += 10;
        _textLB.height += 10;
        _textLB.layer.cornerRadius = 6;
        _textLB.layer.masksToBounds = YES;
        
        self.width = _textLB.width;
        self.height = _textLB.height;
        //
        [self addSubview:self.textLB];
        [self addTriangleRelateView:self.textLB];
    }
}

+ (XSPopoverView *)showText:(NSString *)text inView:(UIView *)superView relateView:(UIView *)relateView
{
    XSPopoverView *popoverView = [[XSPopoverView alloc] init];
    popoverView.textLB.text = text;
    [superView addSubview:popoverView];
    
    // 距离左右屏幕的距离,取最小的
    CGFloat minWiddh = MIN(relateView.x + relateView.width / 2.0, superView.width - (relateView.x + relateView.width / 2.0));
    [popoverView adjustBoundsWithMaxWidth:(minWiddh - 10) * 2];
    popoverView.centerX = relateView.x + relateView.width / 2.0;
    popoverView.centerY = relateView.y - popoverView.height / 2.0;
    
    return popoverView;
    
}

@end

上一篇 下一篇

猜你喜欢

热点阅读