天天看點

簡潔的錯誤提示吐司-- Status 使用詳解

在APP運作中會有一些錯誤提示,比如輸入賬号為空、密碼錯誤之類,相信很多人都會選擇 UIAlertView 來實作彈窗提示,但是對于一些小錯誤提示,再使用 UIAlertView 會不會顯得有些笨拙了,這個時候有更好的選擇 如下圖所示的效果相信會更便捷。

首先我們建立檔案命名為UILabel+Status.h

随後在需要用到的地方導入頭檔案#import "UILabel+Status.h"

        UILabel+Status.h的 .h檔案中

#import <UIKit/UIKit.h>

@interface UILabel (Status)

- (void)showStatusLable:(NSString *)title;

@end

UILabel+Status.h的 .m檔案中

#import "UILabel+Status.h"

@implementation UILabel (Status)

- (void)showStatusLable:(NSString *)title

{

    self.text = [NSStringstringWithFormat:@"提示:%@", title];//彈出的提示文字

    CABasicAnimation* shake = [CABasicAnimationanimationWithKeyPath:@"transform.translation.x"];

    shake.fromValue = [NSNumbernumberWithFloat:-5];

    shake.toValue = [NSNumbernumberWithFloat:5];

    shake.duration =0.1;

    shake.autoreverses =YES;

    shake.repeatCount =2;

    [self.layeraddAnimation:shake forKey:@"shakeAnimation"];

    self.alpha =1.0;

    self.hidden =NO;

    [UIViewanimateWithDuration:3.0delay:0.0options:UIViewAnimationOptionCurveEaseIn |UIViewAnimationOptionAllowUserInteractionanimations:^{

        self.alpha =0.0;

    } completion:nil];

}

//如果需要調整彈出吐司提示的基本樣式,以上屬性便可修改

@end

最後在需要用到的地方

.h檔案

@property (weak,nonatomic) IBOutletUILabel *tusilabel;

.m檔案

 [self.tusilabelshowStatusLable:@"請輸入正确車牌号"];即可

繼續閱讀