Created
March 11, 2013 03:07
-
-
Save helloworld116/5131642 to your computer and use it in GitHub Desktop.
ios:customcontrol:模仿Android中的Toast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Toast.h | |
// UCBook | |
// | |
// Created by apple on 13-1-22. | |
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved. | |
// | |
#define DEFAULT_DISPLAY_DURATION 2.0f | |
@interface Toast : NSObject { | |
NSString *text; | |
UIButton *contentView; | |
CGFloat duration; | |
} | |
+ (void)showWithText:(NSString *) text_; | |
+ (void)showWithText:(NSString *) text_ | |
duration:(CGFloat)duration_; | |
+ (void)showWithText:(NSString *) text_ | |
topOffset:(CGFloat) topOffset_; | |
+ (void)showWithText:(NSString *) text_ | |
topOffset:(CGFloat) topOffset | |
duration:(CGFloat) duration_; | |
+ (void)showWithText:(NSString *) text_ | |
bottomOffset:(CGFloat) bottomOffset_; | |
+ (void)showWithText:(NSString *) text_ | |
bottomOffset:(CGFloat) bottomOffset_ | |
duration:(CGFloat) duration_; | |
@end | |
// | |
// Toast.m | |
// UCBook | |
// | |
// Created by apple on 13-1-22. | |
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved. | |
// | |
#import "Toast.h" | |
#import <QuartzCore/QuartzCore.h> | |
@interface Toast (private) | |
- (id)initWithText:(NSString *)text_; | |
- (void)setDuration:(CGFloat) duration_; | |
- (void)dismisToast; | |
- (void)toastTaped:(UIButton *)sender_; | |
- (void)showAnimation; | |
- (void)hideAnimation; | |
- (void)show; | |
- (void)showFromTopOffset:(CGFloat) topOffset_; | |
- (void)showFromBottomOffset:(CGFloat) bottomOffset_; | |
@end | |
@implementation Toast | |
- (void)dealloc{ | |
[[NSNotificationCenter defaultCenter] removeObserver:self | |
name:UIDeviceOrientationDidChangeNotification | |
object:[UIDevice currentDevice]]; | |
contentView = nil; | |
text = nil; | |
} | |
- (id)initWithText:(NSString *)text_{ | |
if (self = [super init]) { | |
text = [text_ copy]; | |
UIFont *font = [UIFont boldSystemFontOfSize:14]; | |
CGSize textSize = [text sizeWithFont:font | |
constrainedToSize:CGSizeMake(280, MAXFLOAT) | |
lineBreakMode:UILineBreakModeWordWrap]; | |
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 12, textSize.height + 12)]; | |
textLabel.backgroundColor = [UIColor clearColor]; | |
textLabel.textColor = [UIColor whiteColor]; | |
textLabel.textAlignment = UITextAlignmentCenter; | |
textLabel.font = font; | |
textLabel.text = text; | |
textLabel.numberOfLines = 0; | |
contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textLabel.frame.size.width, textLabel.frame.size.height)]; | |
contentView.layer.cornerRadius = 5.0f; | |
contentView.layer.borderWidth = 1.0f; | |
contentView.layer.borderColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor; | |
contentView.backgroundColor = [UIColor colorWithRed:0.2f | |
green:0.2f | |
blue:0.2f | |
alpha:0.75f]; | |
[contentView addSubview:textLabel]; | |
contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth; | |
[contentView addTarget:self | |
action:@selector(toastTaped:) | |
forControlEvents:UIControlEventTouchDown]; | |
contentView.alpha = 0.0f; | |
duration = DEFAULT_DISPLAY_DURATION; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(deviceOrientationDidChanged:) | |
name:UIDeviceOrientationDidChangeNotification | |
object:[UIDevice currentDevice]]; | |
} | |
return self; | |
} | |
- (void)deviceOrientationDidChanged:(NSNotification *)notify_{ | |
[self hideAnimation]; | |
} | |
-(void)dismissToast{ | |
[contentView removeFromSuperview]; | |
} | |
-(void)toastTaped:(UIButton *)sender_{ | |
[self hideAnimation]; | |
} | |
- (void)setDuration:(CGFloat) duration_{ | |
duration = duration_; | |
} | |
-(void)showAnimation{ | |
[UIView beginAnimations:@"show" context:NULL]; | |
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; | |
[UIView setAnimationDuration:0.3]; | |
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]; | |
contentView.alpha = 0.0f; | |
[UIView commitAnimations]; | |
} | |
- (void)show{ | |
UIWindow *window = [UIApplication sharedApplication].keyWindow; | |
contentView.center = window.center; | |
[window addSubview:contentView]; | |
[self showAnimation]; | |
[self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration]; | |
} | |
- (void)showFromTopOffset:(CGFloat) top_{ | |
UIWindow *window = [UIApplication sharedApplication].keyWindow; | |
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].keyWindow; | |
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]; | |
} | |
+ (void)showWithText:(NSString *)text_{ | |
[Toast showWithText:text_ duration:DEFAULT_DISPLAY_DURATION]; | |
} | |
+ (void)showWithText:(NSString *)text_ | |
duration:(CGFloat)duration_{ | |
Toast *toast = [[Toast alloc] initWithText:text_]; | |
[toast setDuration:duration_]; | |
[toast show]; | |
} | |
+ (void)showWithText:(NSString *)text_ | |
topOffset:(CGFloat)topOffset_{ | |
[Toast showWithText:text_ topOffset:topOffset_ duration:DEFAULT_DISPLAY_DURATION]; | |
} | |
+ (void)showWithText:(NSString *)text_ | |
topOffset:(CGFloat)topOffset_ | |
duration:(CGFloat)duration_{ | |
Toast *toast = [[Toast alloc] initWithText:text_]; | |
[toast setDuration:duration_]; | |
[toast showFromTopOffset:topOffset_]; | |
} | |
+ (void)showWithText:(NSString *)text_ | |
bottomOffset:(CGFloat)bottomOffset_{ | |
[Toast showWithText:text_ bottomOffset:bottomOffset_ duration:DEFAULT_DISPLAY_DURATION]; | |
} | |
+ (void)showWithText:(NSString *)text_ | |
bottomOffset:(CGFloat)bottomOffset_ | |
duration:(CGFloat)duration_{ | |
Toast *toast = [[Toast alloc] initWithText:text_]; | |
[toast setDuration:duration_]; | |
[toast showFromBottomOffset:bottomOffset_]; | |
} | |
@end | |
//范例 | |
//[Toast showWithText:@"请先登录" bottomOffset:60 duration:3]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment