123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- //
- // XHToast.m
- // Copyright (c) 2016 XHToast ( https://github.com/CoderZhuXH/XHToast )
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- #import "XHToast.h"
- //Toast默认停留时间
- #define ToastDispalyDuration 1.2f
- //Toast到顶端/底端默认距离
- #define ToastSpace 100.0f
- //Toast背景颜色
- #define ToastBackgroundColor [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75]
- @interface XHToast ()
- @property(nonatomic,strong)UIButton *contentView;
- @property(nonatomic,assign)CGFloat duration;
- @end
- @implementation XHToast
- - (id)initWithText:(NSString *)text{
- if (self = [super init]) {
- UIFont *font = [UIFont boldSystemFontOfSize:16];
- NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName];
- CGRect rect=[text boundingRectWithSize:CGSizeMake(250,CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
- UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,rect.size.width + 40, rect.size.height+ 20)];
- textLabel.backgroundColor = [UIColor clearColor];
- textLabel.textColor = [UIColor whiteColor];
- textLabel.textAlignment = NSTextAlignmentCenter;
- textLabel.font = font;
- textLabel.text = text;
- textLabel.numberOfLines = 0;
-
- self.contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textLabel.frame.size.width, textLabel.frame.size.height)];
- self.contentView.layer.cornerRadius = 20.0f;
- self.contentView.backgroundColor = ToastBackgroundColor;
- [self.contentView addSubview:textLabel];
- self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- [self.contentView addTarget:self action:@selector(toastTaped:) forControlEvents:UIControlEventTouchDown];
- self.contentView.alpha = 0.0f;
- self.duration = ToastDispalyDuration;
-
- }
-
- return self;
- }
- -(void)dismissToast{
-
- [self.contentView removeFromSuperview];
- }
- -(void)toastTaped:(UIButton *)sender{
-
- [self hideAnimation];
- }
- -(void)showAnimation{
- [UIView beginAnimations:@"show" context:NULL];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
- [UIView setAnimationDuration:0.3];
- self.contentView.alpha = 1.0f;
- [UIView commitAnimations];
- }
- -(void)hideAnimation{
- [UIView beginAnimations:@"hide" context:NULL];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
- [UIView setAnimationDelegate:self];
- [UIView setAnimationDidStopSelector:@selector(dismissToast)];
- [UIView setAnimationDuration:0.3];
- self.contentView.alpha = 0.0f;
- [UIView commitAnimations];
- }
- +(UIWindow *)window
- {
- UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
- if(window && !window.hidden) return window;
- window = [UIApplication sharedApplication].delegate.window;
- return window;
- }
- - (void)showIn:(UIView *)view{
- self.contentView.center = view.center;
- [view addSubview:self.contentView];
- [self showAnimation];
- [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:self.duration];
- }
- - (void)showIn:(UIView *)view fromTopOffset:(CGFloat)top{
- self.contentView.center = CGPointMake(view.center.x, top + self.contentView.frame.size.height/2);
- [view addSubview:self.contentView];
- [self showAnimation];
- [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:self.duration];
- }
- - (void)showIn:(UIView *)view fromBottomOffset:(CGFloat)bottom{
- self.contentView.center = CGPointMake(view.center.x, view.frame.size.height-(bottom + self.contentView.frame.size.height/2));
- [view addSubview:self.contentView];
- [self showAnimation];
- [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:self.duration];
- }
- #pragma mark-中间显示
- + (void)showCenterWithText:(NSString *)text{
-
- [XHToast showCenterWithText:text duration:ToastDispalyDuration];
- }
- + (void)showCenterWithText:(NSString *)text duration:(CGFloat)duration{
-
- XHToast *toast = [[XHToast alloc] initWithText:text];
- toast.duration = duration;
- [toast showIn:[self window]];
- }
- #pragma mark-上方显示
- + (void)showTopWithText:(NSString *)text{
-
- [XHToast showTopWithText:text topOffset:ToastSpace duration:ToastDispalyDuration];
- }
- + (void)showTopWithText:(NSString *)text duration:(CGFloat)duration
- {
- [XHToast showTopWithText:text topOffset:ToastSpace duration:duration];
- }
- + (void)showTopWithText:(NSString *)text topOffset:(CGFloat)topOffset{
- [XHToast showTopWithText:text topOffset:topOffset duration:ToastDispalyDuration];
- }
- + (void)showTopWithText:(NSString *)text topOffset:(CGFloat)topOffset duration:(CGFloat)duration{
- XHToast *toast = [[XHToast alloc] initWithText:text];
- toast.duration = duration;
- [toast showIn:[self window] fromTopOffset:topOffset];
- }
- #pragma mark-下方显示
- + (void)showBottomWithText:(NSString *)text{
-
- [XHToast showBottomWithText:text bottomOffset:ToastSpace duration:ToastDispalyDuration];
- }
- + (void)showBottomWithText:(NSString *)text duration:(CGFloat)duration
- {
- [XHToast showBottomWithText:text bottomOffset:ToastSpace duration:duration];
- }
- + (void)showBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset{
- [XHToast showBottomWithText:text bottomOffset:bottomOffset duration:ToastDispalyDuration];
- }
- + (void)showBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset duration:(CGFloat)duration{
- XHToast *toast = [[XHToast alloc] initWithText:text];
- toast.duration = duration;
- [toast showIn:[self window] fromBottomOffset:bottomOffset];
- }
- @end
- @implementation UIView (XHToast)
- #pragma mark-中间显示
- - (void)showXHToastCenterWithText:(NSString *)text
- {
- [self showXHToastCenterWithText:text duration:ToastDispalyDuration];
- }
- - (void)showXHToastCenterWithText:(NSString *)text duration:(CGFloat)duration
- {
- XHToast *toast = [[XHToast alloc] initWithText:text];
- toast.duration = duration;
- [toast showIn:self];
- }
- #pragma mark-上方显示
- - (void)showXHToastTopWithText:(NSString *)text
- {
- [self showXHToastTopWithText:text topOffset:ToastSpace duration:ToastDispalyDuration];
- }
- - (void)showXHToastTopWithText:(NSString *)text duration:(CGFloat)duration
- {
- [self showXHToastTopWithText:text topOffset:ToastSpace duration:duration];
- }
- - (void)showXHToastTopWithText:(NSString *)text topOffset:(CGFloat)topOffset
- {
- [self showXHToastTopWithText:text topOffset:topOffset duration:ToastDispalyDuration];
- }
- - (void)showXHToastTopWithText:(NSString *)text topOffset:(CGFloat)topOffset duration:(CGFloat)duration
- {
- XHToast *toast = [[XHToast alloc] initWithText:text];
- toast.duration = duration;
- [toast showIn:self fromTopOffset:topOffset];
- }
- #pragma mark-下方显示
- - (void)showXHToastBottomWithText:(NSString *)text
- {
- [self showXHToastBottomWithText:text bottomOffset:ToastSpace duration:ToastDispalyDuration];
- }
- - (void)showXHToastBottomWithText:(NSString *)text duration:(CGFloat)duration
- {
- [self showXHToastBottomWithText:text bottomOffset:ToastSpace duration:duration];
- }
- - (void)showXHToastBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset
- {
- [self showXHToastBottomWithText:text bottomOffset:bottomOffset duration:ToastDispalyDuration];
- }
- - (void)showXHToastBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset duration:(CGFloat)duration
- {
- XHToast *toast = [[XHToast alloc] initWithText:text];
- toast.duration = duration;
- [toast showIn:self fromBottomOffset:bottomOffset];
- }
- @end
|