今天给大家分享3种提示三方。SVProgressHUD,JFMinimalNotification,UIView+Toast
SVProgressHUD
pod 'SVProgressHUD'
// 位置 偏移
// [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, 100)];
//要显示的内容
//显示文字
[SVProgressHUD showWithStatus:@"努力加载中"];
// //进度
// [SVProgressHUD showProgress:.2 status:@"进度"];
// //感叹号
// [SVProgressHUD showInfoWithStatus:@"感叹号"];
// //success
// [SVProgressHUD showSuccessWithStatus:@"Success!"];
// //error
// [SVProgressHUD showErrorWithStatus:@"Error"];
// 整个后面的背景选择
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
// 转圈的宽度(有文本)
[SVProgressHUD setRingThickness:3];
//无文本时设置圈宽
//[SVProgressHUD setRingNoTextRadius:3];
//无边框背景样式
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
// SVProgressHUDStyleLight 白底黑字
// SVProgressHUDStyleDark 黑底白字
//框颜色
[SVProgressHUD setBackgroundColor:[UIColor yellowColor]];
//字体和圈的颜色
[SVProgressHUD setForegroundColor:[UIColor redColor]];
JFMinimalNotification
显示在顶部
pod 'JFMinimalNotifications'
#import "ViewController.h"
#import "JFMinimalNotification.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self displaySuccess:@"成功"];
}
#pragma mark - JFMinimalNotification
static NSInteger const afterTime = 4.0;
- (void)displaySuccess:(NSString *)title
{
JFMinimalNotification *minimalNotification = [JFMinimalNotification notificationWithStyle:JFMinimalNotificationStyleSuccess
title:@"完成"
subTitle:title
dismissalDelay:afterTime touchHandler:^{
}];
[self.view addSubview:minimalNotification];
minimalNotification.presentFromTop = YES;
[minimalNotification show];
}
- (void)displayError:(NSString *)title
{
JFMinimalNotification *minimalNotification = [JFMinimalNotification notificationWithStyle:JFMinimalNotificationStyleError
title:@"错误"
subTitle:title
dismissalDelay:afterTime touchHandler:^{
}];
[self.view addSubview:minimalNotification];
minimalNotification.presentFromTop = YES;
[minimalNotification show];
}
- (void)displayWarning:(NSString *)title
{
JFMinimalNotification *minimalNotification = [JFMinimalNotification notificationWithStyle:JFMinimalNotificationStyleWarning
title:@"警告"
subTitle:title
dismissalDelay:afterTime touchHandler:^{
}];
[self.view addSubview:minimalNotification];
minimalNotification.presentFromTop = YES;
[minimalNotification show];
}
UIView+Toast
文字、图片(视图显示在屏幕底部)
// each makeToast method creates a view and displays it as toast
//提示文字
- (void)makeToast:(NSString *)message;
//持续interval秒的提示文字
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position;
//持续interval秒的多段文字
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position title:(NSString *)title;
//带有图片的持续interval秒的多段文字
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position title:(NSString *)title image:(UIImage *)image;
//带有图片的持续interval秒的1段文字
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position image:(UIImage *)image;
菊花(显示在屏幕中间)
// displays toast with an activity spinner
- (void)makeToastActivity;
- (void)makeToastActivity:(id)position;
- (void)hideToastActivity;
UIView+Toast源码如下
UIView+Toast.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface UIView (Toast)
// each makeToast method creates a view and displays it as toast
- (void)makeToast:(NSString *)message;
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position;
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position title:(NSString *)title;
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position title:(NSString *)title image:(UIImage *)image;
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position image:(UIImage *)image;
// displays toast with an activity spinner
- (void)makeToastActivity;
- (void)makeToastActivity:(id)position;
- (void)hideToastActivity;
// the showToast methods display any view as toast
- (void)showToast:(UIView *)toast;
- (void)showToast:(UIView *)toast duration:(CGFloat)interval position:(id)point;
@end
UIView+Toast.m
#import "UIView+Toast.h"
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
/*
* CONFIGURE THESE VALUES TO ADJUST LOOK & FEEL,
* DISPLAY DURATION, ETC.
*/
// general appearance
static const CGFloat CSToastMaxWidth = 0.8; // 80% of parent view width
static const CGFloat CSToastMaxHeight = 0.8; // 80% of parent view height
static const CGFloat CSToastHorizontalPadding = 10.0;
static const CGFloat CSToastVerticalPadding = 10.0;
static const CGFloat CSToastCornerRadius = 10.0;
static const CGFloat CSToastOpacity = 0.8;
static const CGFloat CSToastFontSize = 14.0;
static const CGFloat CSToastMaxTitleLines = 0;
static const CGFloat CSToastMaxMessageLines = 0;
static const CGFloat CSToastFadeDuration = 0.2;
// shadow appearance
static const CGFloat CSToastShadowOpacity = 0.8;
static const CGFloat CSToastShadowRadius = 6.0;
static const CGSize CSToastShadowOffset = { 4.0, 4.0 };
static const BOOL CSToastDisplayShadow = YES;
// display duration and position
static const CGFloat CSToastDefaultDuration = 1.2;
static const NSString * CSToastDefaultPosition = @"bottom";
// image view size
static const CGFloat CSToastImageViewWidth = 80.0;
static const CGFloat CSToastImageViewHeight = 80.0;
// activity
static const CGFloat CSToastActivityWidth = 100.0;
static const CGFloat CSToastActivityHeight = 100.0;
static const NSString * CSToastActivityDefaultPosition = @"center";
static const NSString * CSToastActivityViewKey = @"CSToastActivityViewKey";
@interface UIToastView : UIView
@end
@implementation UIToastView
@end
@interface UIView (ToastPrivate)
- (CGPoint)centerPointForPosition:(id)position withToast:(UIView *)toast;
- (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image;
@end
@implementation UIView (Toast)
#pragma mark - Toast Methods
- (void)makeToast:(NSString *)message {
[self makeToast:message duration:CSToastDefaultDuration position:CSToastDefaultPosition];
}
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position {
UIView *toast = [self viewForMessage:message title:nil image:nil];
[self showToast:toast duration:interval position:position];
}
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position title:(NSString *)title {
UIView *toast = [self viewForMessage:message title:title image:nil];
[self showToast:toast duration:interval position:position];
}
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position image:(UIImage *)image {
UIView *toast = [self viewForMessage:message title:nil image:image];
[self showToast:toast duration:interval position:position];
}
- (void)makeToast:(NSString *)message duration:(CGFloat)interval position:(id)position title:(NSString *)title image:(UIImage *)image {
UIView *toast = [self viewForMessage:message title:title image:image];
[self showToast:toast duration:interval position:position];
}
- (void)showToast:(UIView *)toast {
[self showToast:toast duration:CSToastDefaultDuration position:CSToastDefaultPosition];
}
- (void)showToast:(UIView *)toast duration:(CGFloat)interval position:(id)point {
toast.center = [self centerPointForPosition:point withToast:toast];
toast.alpha = 0.0;
UIToastView *prevToast = [self findExistingToast];
if (prevToast == nil) {
[self addSubview:toast];
[UIView animateWithDuration:CSToastFadeDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
toast.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:CSToastFadeDuration
delay:interval
options:UIViewAnimationOptionCurveEaseIn
animations:^{
toast.alpha = 0.0;
} completion:^(BOOL finished) {
[toast removeFromSuperview];
}];
}];
}
else {
[self addSubview:toast];
[UIView animateWithDuration:CSToastFadeDuration
delay:CSToastDefaultDuration
options:UIViewAnimationOptionCurveEaseOut
animations:^{
toast.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:CSToastFadeDuration
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
prevToast.alpha = 0.0;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:CSToastFadeDuration
delay:interval
options:UIViewAnimationOptionCurveEaseIn
animations:^{
toast.alpha = 0.0;
} completion:^(BOOL finished) {
[toast removeFromSuperview];
}];
}];
}];
}
}
- (UIToastView *)findExistingToast
{
UIToastView *toast = nil;
for (UIView *view in self.subviews) {
if ([view isMemberOfClass:[UIToastView class]]) {
toast = (UIToastView *)view; // find the last one
}
}
return toast;
}
#pragma mark - Toast Activity Methods
- (void)makeToastActivity {
[self makeToastActivity:CSToastActivityDefaultPosition];
}
- (void)makeToastActivity:(id)position {
// sanity
UIView *existingActivityView = (UIView *)objc_getAssociatedObject(self, &CSToastActivityViewKey);
if (existingActivityView != nil) return;
UIView *activityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CSToastActivityWidth, CSToastActivityHeight)];
activityView.center = [self centerPointForPosition:position withToast:activityView];
activityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:CSToastOpacity];
activityView.alpha = 0.0;
activityView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
activityView.layer.cornerRadius = CSToastCornerRadius;
if (CSToastDisplayShadow) {
activityView.layer.shadowColor = [UIColor blackColor].CGColor;
activityView.layer.shadowOpacity = CSToastShadowOpacity;
activityView.layer.shadowRadius = CSToastShadowRadius;
activityView.layer.shadowOffset = CSToastShadowOffset;
}
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicatorView.center = CGPointMake(activityView.bounds.size.width / 2, activityView.bounds.size.height / 2);
[activityView addSubview:activityIndicatorView];
[activityIndicatorView startAnimating];
// associate ourselves with the activity view
objc_setAssociatedObject (self, &CSToastActivityViewKey, activityView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self addSubview:activityView];
[UIView animateWithDuration:CSToastFadeDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
activityView.alpha = 1.0;
} completion:nil];
}
- (void)hideToastActivity {
UIView *existingActivityView = (UIView *)objc_getAssociatedObject(self, &CSToastActivityViewKey);
if (existingActivityView != nil) {
[UIView animateWithDuration:CSToastFadeDuration
delay:0.0
options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState)
animations:^{
existingActivityView.alpha = 0.0;
} completion:^(BOOL finished) {
[existingActivityView removeFromSuperview];
objc_setAssociatedObject (self, &CSToastActivityViewKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}];
}
}
#pragma mark - Private Methods
- (CGPoint)centerPointForPosition:(id)point withToast:(UIView *)toast {
if([point isKindOfClass:[NSString class]]) {
// convert string literals @"top", @"bottom", @"center", or any point wrapped in an NSValue object into a CGPoint
if([point caseInsensitiveCompare:@"top"] == NSOrderedSame) {
return CGPointMake(self.bounds.size.width/2, (toast.frame.size.height / 2) + CSToastVerticalPadding);
} else if([point caseInsensitiveCompare:@"bottom"] == NSOrderedSame) {
return CGPointMake(self.bounds.size.width/2, (self.bounds.size.height - (toast.frame.size.height / 2)) - CSToastVerticalPadding - 49); // 49 is the height of tab bar
} else if([point caseInsensitiveCompare:@"center"] == NSOrderedSame) {
return CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
}
} else if ([point isKindOfClass:[NSValue class]]) {
return [point CGPointValue];
}
// CC_LOG(@"Warning: Invalid position for toast.");
return [self centerPointForPosition:CSToastDefaultPosition withToast:toast];
}
- (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image {
// sanity
if((message == nil) && (title == nil) && (image == nil)) return nil;
// dynamically build a toast view with any combination of message, title, & image.
UILabel *messageLabel = nil;
UILabel *titleLabel = nil;
UIImageView *imageView = nil;
// create the parent view
UIView *wrapperView = [[UIToastView alloc] init];
wrapperView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
wrapperView.layer.cornerRadius = CSToastCornerRadius;
if (CSToastDisplayShadow) {
wrapperView.layer.shadowColor = [UIColor blackColor].CGColor;
wrapperView.layer.shadowOpacity = CSToastShadowOpacity;
wrapperView.layer.shadowRadius = CSToastShadowRadius;
wrapperView.layer.shadowOffset = CSToastShadowOffset;
}
wrapperView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:CSToastOpacity];
if(image != nil) {
imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.frame = CGRectMake(CSToastHorizontalPadding, CSToastVerticalPadding, CSToastImageViewWidth, CSToastImageViewHeight);
}
CGFloat imageWidth, imageHeight, imageLeft;
// the imageView frame values will be used to size & position the other views
if(imageView != nil) {
imageWidth = imageView.bounds.size.width;
imageHeight = imageView.bounds.size.height;
imageLeft = CSToastHorizontalPadding;
} else {
imageWidth = imageHeight = imageLeft = 0.0;
}
if (title != nil) {
titleLabel = [[UILabel alloc] init];
titleLabel.numberOfLines = CSToastMaxTitleLines;
titleLabel.font = [UIFont fontWithName:@"Microsoft YaHei" size:CSToastFontSize];
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
titleLabel.textColor=[UIColor colorWithRed:221/255.0 green:202/255.0 blue:140/255.0 alpha:1];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.alpha = 1.0;
titleLabel.text = title;
// size the title label according to the length of the text
CGSize maxSizeTitle = CGSizeMake((self.bounds.size.width * CSToastMaxWidth) - imageWidth, self.bounds.size.height * CSToastMaxHeight);
CGSize expectedSizeTitle = [title sizeWithFont:titleLabel.font constrainedToSize:maxSizeTitle lineBreakMode:titleLabel.lineBreakMode];
titleLabel.frame = CGRectMake(0.0, 0.0, expectedSizeTitle.width, expectedSizeTitle.height);
}
if (message != nil) {
messageLabel = [[UILabel alloc] init];
messageLabel.numberOfLines = CSToastMaxMessageLines;
messageLabel.font = [UIFont fontWithName:@"Microsoft YaHei" size:CSToastFontSize];
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
messageLabel.textColor = [UIColor colorWithRed:221/255.0 green:202/255.0 blue:140/255.0 alpha:1];
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.alpha = 1.0;
messageLabel.text = message;
// size the message label according to the length of the text
CGSize maxSizeMessage = CGSizeMake((self.bounds.size.width * CSToastMaxWidth) - imageWidth, self.bounds.size.height * CSToastMaxHeight);
CGSize expectedSizeMessage = [message sizeWithFont:messageLabel.font constrainedToSize:maxSizeMessage lineBreakMode:messageLabel.lineBreakMode];
messageLabel.frame = CGRectMake(0.0, 0.0, expectedSizeMessage.width, expectedSizeMessage.height);
}
// titleLabel frame values
CGFloat titleWidth, titleHeight, titleTop, titleLeft;
if(titleLabel != nil) {
titleWidth = titleLabel.bounds.size.width;
titleHeight = titleLabel.bounds.size.height;
titleTop = CSToastVerticalPadding;
titleLeft = imageLeft + imageWidth + CSToastHorizontalPadding;
} else {
titleWidth = titleHeight = titleTop = titleLeft = 0.0;
}
// messageLabel frame values
CGFloat messageWidth, messageHeight, messageLeft, messageTop;
if(messageLabel != nil) {
messageWidth = messageLabel.bounds.size.width;
messageHeight = messageLabel.bounds.size.height;
messageLeft = imageLeft + imageWidth + CSToastHorizontalPadding;
messageTop = titleTop + titleHeight + CSToastVerticalPadding;
} else {
messageWidth = messageHeight = messageLeft = messageTop = 0.0;
}
CGFloat longerWidth = MAX(titleWidth, messageWidth);
CGFloat longerLeft = MAX(titleLeft, messageLeft);
// wrapper width uses the longerWidth or the image width, whatever is larger. same logic applies to the wrapper height
CGFloat wrapperWidth = MAX((imageWidth + (CSToastHorizontalPadding * 2)), (longerLeft + longerWidth + CSToastHorizontalPadding));
CGFloat wrapperHeight = MAX((messageTop + messageHeight + CSToastVerticalPadding), (imageHeight + (CSToastVerticalPadding * 2)));
wrapperView.frame = CGRectMake(0.0, 0.0, wrapperWidth, wrapperHeight);
if(titleLabel != nil) {
titleLabel.frame = CGRectMake(titleLeft, titleTop, titleWidth, titleHeight);
[wrapperView addSubview:titleLabel];
}
if(messageLabel != nil) {
messageLabel.frame = CGRectMake(messageLeft, messageTop, messageWidth, messageHeight);
[wrapperView addSubview:messageLabel];
}
if(imageView != nil) {
[wrapperView addSubview:imageView];
}
return wrapperView;
}
@end
网友评论