123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- //
- // JKToast.m
- // JKToast
- //
- // Created by Jakey on 14-10-27.
- // Copyright (c) 2014年 www.skyfox.org. All rights reserved.
- //
- #import "JKToast.h"
- #import <QuartzCore/QuartzCore.h>
- @implementation JKToast
- - (id)initWithText:(NSString *)text{
- if (self = [super init]) {
-
- text = [text copy];
-
- UIFont *font = [UIFont systemFontOfSize:14];
- // CGSize textSize = [text sizeWithFont:font
- // constrainedToSize:CGSizeMake(280, MAXFLOAT)
- // lineBreakMode:NSLineBreakByCharWrapping];
-
- CGSize textSize = [text boundingRectWithSize:CGSizeMake(280, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size;
-
-
- CGRect textFrame = CGRectMake(0, 0, textSize.width + 40, textSize.height + 30);
- if(textLab){
- textLab.frame = textFrame;
- }else{
- textLab = [[UILabel alloc] initWithFrame:textFrame];
- textLab.backgroundColor = [UIColor clearColor];
- textLab.textColor = [UIColor darkTextColor];
- textLab.textAlignment = NSTextAlignmentCenter;
- textLab.numberOfLines = 0;
- textLab.font = font;
- }
- textLab.text = text;
-
-
- if (contentView) {
- contentView.frame = textFrame;
- }else{
- contentView = [[UIButton alloc] initWithFrame:textFrame];
- contentView.alpha = 0.0f;
- contentView.layer.cornerRadius = 23.0f;
- contentView.backgroundColor = RDSLightGrayColor;
- contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- [contentView addSubview:textLab];
- [contentView addTarget:self
- action:@selector(toastTaped:)
- forControlEvents:UIControlEventTouchDown];
-
- duration = DEFAULT_DISPLAY_DURATION;
- }
-
- // [[NSNotificationCenter defaultCenter] addObserver:self
- // selector:@selector(deviceOrientationDidChanged:)
- // name:UIDeviceOrientationDidChangeNotification
- // object:[UIDevice currentDevice]];
- }
- return self;
- }
- - (void)deviceOrientationDidChanged:(NSNotification *)notify{
- UIDevice* device = [notify valueForKey:@"object"];
- //[self hideAnimation];
- }
- -(void)toastTaped:(UIButton *)sender{
- [self hideAnimation];
- }
- - (void)setDuration:(CGFloat) duration{
- duration = duration;
- }
- -(void)showAnimation{
-
- [UIView animateWithDuration:0.3 animations:^{
- self->contentView.alpha = 1.0f;
- } completion:^(BOOL finished) {
-
- }];
- }
- -(void)hideAnimation{
-
- [UIView animateWithDuration:0.3 animations:^{
- self->contentView.alpha = 0.0f;
- } completion:^(BOOL finished) {
-
- }];
- }
- - (void)showWithLocal:(iToastLocal)local{
-
- // 取消延时任务
- dispatch_async(dispatch_get_main_queue(), ^{
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideAnimation) object:nil];
- });
-
- UIWindow *window = [UIApplication sharedApplication].delegate.window;
- [window endEditing:YES];
-
- CGFloat centerY;
- if(local == localCenter){
- centerY = window.center.y;
- }else{
- centerY = window.bounds.size.height -160;
- }
- CGPoint center = CGPointMake(window.center.x, centerY);
- contentView.center = center;
-
- [window addSubview:contentView];
- [self showAnimation];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:self->duration];
- });
- }
- - (void)showFromTopOffset:(CGFloat) top{
- UIWindow *window = [UIApplication sharedApplication].delegate.window;
- contentView.center = CGPointMake(window.center.x, top + contentView.frame.size.height/2);
- [window addSubview:contentView];
- [self showAnimation];
- [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
- }
- - (void)showFromBottomOffset:(CGFloat) bottom{
- UIWindow *window = [UIApplication sharedApplication].delegate.window;
- contentView.center = CGPointMake(window.center.x, window.frame.size.height-(bottom + contentView.frame.size.height/2));
- [window addSubview:contentView];
- [self showAnimation];
- [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
- }
- + (JKToast*)sharedView {
- static dispatch_once_t once;
-
- static JKToast *sharedView;
- dispatch_once(&once, ^{ sharedView = [[self alloc] init]; });
-
- return sharedView;
- }
- + (void)showTopWithText:(NSString *)text{
- [JKToast showWithText:text local:localTop duration:3];
- }
- + (void)showBottomWithText:(NSString *)text{
- [JKToast showWithText:text local:localBottom duration:3];
- }
- + (void)showCenterWithText:(NSString *)text{
- [JKToast showWithText:text local:localCenter duration:3];
- }
- + (void)showWithText:(NSString *)text
- local:(iToastLocal)local
- duration:(CGFloat)duration{
- JKToast *toast = [[self sharedView] initWithText:text];
- [toast setDuration:duration];
- [toast showWithLocal:local];
- }
- + (void)showWithText:(NSString *)text
- topOffset:(CGFloat)topOffset{
- [JKToast showWithText:text topOffset:topOffset duration:DEFAULT_DISPLAY_DURATION];
- }
- + (void)showWithText:(NSString *)text
- topOffset:(CGFloat)topOffset
- duration:(CGFloat)duration{
- JKToast *toast = [[JKToast alloc] initWithText:text];
- [toast setDuration:duration];
- [toast showFromTopOffset:topOffset];
- }
- + (void)showWithText:(NSString *)text
- bottomOffset:(CGFloat)bottomOffset{
- [JKToast showWithText:text bottomOffset:bottomOffset duration:DEFAULT_DISPLAY_DURATION];
- }
- + (void)showWithText:(NSString *)text
- bottomOffset:(CGFloat)bottomOffset
- duration:(CGFloat)duration{
- JKToast *toast = [[JKToast alloc] initWithText:text] ;
- [toast setDuration:duration];
- [toast showFromBottomOffset:bottomOffset];
- }
- @end
|