iOS氣泡提示

  • 現在來說一下這款很好用的氣泡提示,名字** JDFTooltips**,一個外國人寫的,在GitHub可下載:
    https://github.com/JoeFryer/JDFTooltips
  • 首先來看一下大概就是這樣的效果了。


    大概就是這樣的效果了
  • 優點

1.滿足不同要求的初始化方法(四個)
2.可選不同的箭頭方向(上、下、左、右)
3.字體可自定義
4.字體顏色可自定義
5.背景顏色可自定義
6.背景寬度自定義
7.陰影,陰影的顏色可自定義

  • 初始化方法
    1)自定義尖角在哪個點的初始化方法
/**
 *  @author , 16-04-25 15:04:03
 *
 *  <#Description#>
 *
 *  @param targetPoint    提示框的尖角將在哪個點
 *  @param hostView       提示框加載到哪個視圖上
 *  @param tooltipText    提示語
 *  @param arrowDirection 尖角的方向
 *  @param width          提示框的寬度
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithTargetPoint:(CGPoint)targetPoint hostView:(UIView *)hostView tooltipText:(NSString *)tooltipText arrowDirection:(JDFTooltipViewArrowDirection)arrowDirection width:(CGFloat)width;

2)自定義尖角在哪個點,且有回調方法的初始化方法

/**
 *  @author , 16-04-25 15:04:33
 *
 *  <#Description#>
 *
 *  @param targetPoint         提示框的尖角將在哪個點
 *  @param hostView            提示框加載到哪個視圖上
 *  @param tooltipText         提示語
 *  @param arrowDirection      方向
 *  @param width               寬度
 *  @param showCompletionBlock 展示之后的回調
 *  @param hideCompletionBlock 隱藏之后的回調
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithTargetPoint:(CGPoint)targetPoint hostView:(UIView *)hostView tooltipText:(NSString *)tooltipText arrowDirection:(JDFTooltipViewArrowDirection)arrowDirection width:(CGFloat)width 
showCompletionBlock:(JDFTooltipViewCompletionBlock)showCompletionBlock hideCompletionBlock:(JDFTooltipViewCompletionBlock)hideCompletionBlock;

3)尖角會在目標view寬(方向上下)或者高(方向左右)的二分之一處的初始化方法

/**
 *  @author , 16-04-25 15:04:53
 *
 *  <#Description#>
 *
 *  @param targetView     目標view,尖角會在目標view寬(方向上下)或者高(方向左右)的二分之一處
 *  @param hostView       加載在哪個視圖上
 *  @param tooltipText    提示語
 *  @param arrowDirection 方向
 *  @param width          寬度
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithTargetView:(UIView *)targetView hostView:(UIView *)hostView tooltipText:(NSString *)tooltipText arrowDirection:(JDFTooltipViewArrowDirection)arrowDirection width:(CGFloat)width;

4)尖角會在目標view寬(方向上下)或者高(方向左右)的二分之一處,且有回調方法的初始化方法

/**
 *  @author fushengjun, 16-04-25 15:04:56
 *
 *  <#Description#>
 *
 *  @param targetView          目標view,尖角會在目標view寬(方向上下)或者高(方向左右)的二分之一
 *  @param hostView            加載在哪個視圖上
 *  @param tooltipText         提示語
 *  @param arrowDirection      方向
 *  @param width               寬度
 *  @param showCompletionBlock 展示之后的回調方法
 *  @param hideCompletionBlock 隱藏之后的回調方法
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithTargetView:(UIView *)targetView hostView:(UIView *)hostView tooltipText:(NSString *)tooltipText
                    arrowDirection:(JDFTooltipViewArrowDirection)arrowDirection width:(CGFloat)width
               showCompletionBlock:(JDFTooltipViewCompletionBlock)showCompletionBlock
               hideCompletionBlock:(JDFTooltipViewCompletionBlock)hideCompletionBlock;
  • 使用示例
//1.聲明
JDFTooltipView *tooltip1;
    
//2.初始化,but1是目標view,方向向下
tooltip1 = [[JDFTooltipView alloc] initWithTargetView:but1 hostView:self.view tooltipText:@"在投資期限內,每個月返還相同額度的利息,到期后返還最后一期利息和所有本金" arrowDirection:JDFTooltipViewArrowDirectionDown width:300.0f];
//提示語字體
tooltip1.font = [UIFont systemFontOfSize:12];
//提示語顏色
tooltip1.textColour = [UIColor redColor];
//點擊提示view本身是否使view消失
tooltip1.dismissOnTouch = NO;
//是否有陰影
tooltip1.shadowEnabled = YES;
//陰影顏色
tooltip1.shadowColour = [UIColor redColor];

//3.展示
[tooltip1 show];

//4.隱藏
[tooltip1 hideAnimated:YES]
示例演示
  • 提示串(即有好幾個提示語,可以一個接著一個的讓他們顯示)

  • 使用示例
    1)包含頭文件
    #import "JDFSequentialTooltipManager.h"
    2)聲明一個管理提示串對象
    @property(nonatomic, strong)JDFSequentialTooltipManager *tooltipManager;
    3)初始化管理串對象
    self.tooltipManager = [[JDFSequentialTooltipManager alloc] initWithHostView:self.view];
    4)像管理提示串的對象里添加幾個提示框View
    [self.tooltipManager addTooltipWithTargetView:label1 hostView:self.view
    tooltipText:@"《殺人狂魔》是弗蘭克·卡方執導的恐怖片,阿美莉嘉·奧利沃、伊利亞·伍德等參加演出。該片主要講述了殺人狂魔弗蘭克的故事。"
    arrowDirection:JDFTooltipViewArrowDirectionLeft width:200.0f];

    [self.tooltipManager addTooltipWithTargetView:label2 hostView:self.view
                                    tooltipText:@"程序員不是你們眼中的程序猿-后IT時代。程序猿是一種非常特殊的、可以從事程序開發、維護的動物。一般分為程序設計猿和程序編碼猿,但兩者的界限并不非常清楚,都可以進行開發、維護工作,特別是在中國,而且最重要的一點,二者都是一種非常悲劇的存在。"
                                 arrowDirection:JDFTooltipViewArrowDirectionRight width:200.0f];
    
    [self.tooltipManager addTooltipWithTargetView:label3 hostView:self.view
                                    tooltipText:@"身為一名程序媛,總結混跡于技術圈多年的經驗可以概括成三要三不要。"
                                 arrowDirection:JDFTooltipViewArrowDirectionUp width:200.0f];
    

5)展示
[self.tooltipManager showNextTooltip];
6)演示示例,我這里每點擊一下空白處展示下一個


演示示例


更新:下面是我同事封裝的,我們現在用這個。特點:簡單方便

  • 直接貼代碼
    .h文件
#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

  • 使用方法,即.h文件中的外接方法。
    三個參數分別是:
    text:要顯示的內容
    superView:彈窗要加載到哪個父視圖上
    relateView:彈窗顯示的位置在哪個視圖處

  • 效果示例:


    彈窗示例
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,908評論 6 541
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,324評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,018評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,675評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,417評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,783評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,779評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,960評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,522評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,267評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,471評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,009評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,698評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,099評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,386評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,204評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,436評論 2 378

推薦閱讀更多精彩內容