《省钱达人》与《猎豆优选》UI相同版。域名tbk

XHToast.m 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // XHToast.m
  3. // Copyright (c) 2016 XHToast ( https://github.com/CoderZhuXH/XHToast )
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import "XHToast.h"
  22. //Toast默认停留时间
  23. #define ToastDispalyDuration 1.2f
  24. //Toast到顶端/底端默认距离
  25. #define ToastSpace 100.0f
  26. //Toast背景颜色
  27. #define ToastBackgroundColor [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75]
  28. @interface XHToast ()
  29. @property(nonatomic,strong)UIButton *contentView;
  30. @property(nonatomic,assign)CGFloat duration;
  31. @end
  32. @implementation XHToast
  33. - (id)initWithText:(NSString *)text{
  34. if (self = [super init]) {
  35. UIFont *font = [UIFont boldSystemFontOfSize:16];
  36. NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName];
  37. CGRect rect=[text boundingRectWithSize:CGSizeMake(250,CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
  38. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,rect.size.width + 40, rect.size.height+ 20)];
  39. textLabel.backgroundColor = [UIColor clearColor];
  40. textLabel.textColor = [UIColor whiteColor];
  41. textLabel.textAlignment = NSTextAlignmentCenter;
  42. textLabel.font = font;
  43. textLabel.text = text;
  44. textLabel.numberOfLines = 0;
  45. self.contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textLabel.frame.size.width, textLabel.frame.size.height)];
  46. self.contentView.layer.cornerRadius = 20.0f;
  47. self.contentView.backgroundColor = ToastBackgroundColor;
  48. [self.contentView addSubview:textLabel];
  49. self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  50. [self.contentView addTarget:self action:@selector(toastTaped:) forControlEvents:UIControlEventTouchDown];
  51. self.contentView.alpha = 0.0f;
  52. self.duration = ToastDispalyDuration;
  53. }
  54. return self;
  55. }
  56. -(void)dismissToast{
  57. [self.contentView removeFromSuperview];
  58. }
  59. -(void)toastTaped:(UIButton *)sender{
  60. [self hideAnimation];
  61. }
  62. -(void)showAnimation{
  63. [UIView beginAnimations:@"show" context:NULL];
  64. [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
  65. [UIView setAnimationDuration:0.3];
  66. self.contentView.alpha = 1.0f;
  67. [UIView commitAnimations];
  68. }
  69. -(void)hideAnimation{
  70. [UIView beginAnimations:@"hide" context:NULL];
  71. [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
  72. [UIView setAnimationDelegate:self];
  73. [UIView setAnimationDidStopSelector:@selector(dismissToast)];
  74. [UIView setAnimationDuration:0.3];
  75. self.contentView.alpha = 0.0f;
  76. [UIView commitAnimations];
  77. }
  78. +(UIWindow *)window
  79. {
  80. UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
  81. if(window && !window.hidden) return window;
  82. window = [UIApplication sharedApplication].delegate.window;
  83. return window;
  84. }
  85. - (void)showIn:(UIView *)view{
  86. self.contentView.center = view.center;
  87. [view addSubview:self.contentView];
  88. [self showAnimation];
  89. [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:self.duration];
  90. }
  91. - (void)showIn:(UIView *)view fromTopOffset:(CGFloat)top{
  92. self.contentView.center = CGPointMake(view.center.x, top + self.contentView.frame.size.height/2);
  93. [view addSubview:self.contentView];
  94. [self showAnimation];
  95. [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:self.duration];
  96. }
  97. - (void)showIn:(UIView *)view fromBottomOffset:(CGFloat)bottom{
  98. self.contentView.center = CGPointMake(view.center.x, view.frame.size.height-(bottom + self.contentView.frame.size.height/2));
  99. [view addSubview:self.contentView];
  100. [self showAnimation];
  101. [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:self.duration];
  102. }
  103. #pragma mark-中间显示
  104. + (void)showCenterWithText:(NSString *)text{
  105. [XHToast showCenterWithText:text duration:ToastDispalyDuration];
  106. }
  107. + (void)showCenterWithText:(NSString *)text duration:(CGFloat)duration{
  108. XHToast *toast = [[XHToast alloc] initWithText:text];
  109. toast.duration = duration;
  110. [toast showIn:[self window]];
  111. }
  112. #pragma mark-上方显示
  113. + (void)showTopWithText:(NSString *)text{
  114. [XHToast showTopWithText:text topOffset:ToastSpace duration:ToastDispalyDuration];
  115. }
  116. + (void)showTopWithText:(NSString *)text duration:(CGFloat)duration
  117. {
  118. [XHToast showTopWithText:text topOffset:ToastSpace duration:duration];
  119. }
  120. + (void)showTopWithText:(NSString *)text topOffset:(CGFloat)topOffset{
  121. [XHToast showTopWithText:text topOffset:topOffset duration:ToastDispalyDuration];
  122. }
  123. + (void)showTopWithText:(NSString *)text topOffset:(CGFloat)topOffset duration:(CGFloat)duration{
  124. XHToast *toast = [[XHToast alloc] initWithText:text];
  125. toast.duration = duration;
  126. [toast showIn:[self window] fromTopOffset:topOffset];
  127. }
  128. #pragma mark-下方显示
  129. + (void)showBottomWithText:(NSString *)text{
  130. [XHToast showBottomWithText:text bottomOffset:ToastSpace duration:ToastDispalyDuration];
  131. }
  132. + (void)showBottomWithText:(NSString *)text duration:(CGFloat)duration
  133. {
  134. [XHToast showBottomWithText:text bottomOffset:ToastSpace duration:duration];
  135. }
  136. + (void)showBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset{
  137. [XHToast showBottomWithText:text bottomOffset:bottomOffset duration:ToastDispalyDuration];
  138. }
  139. + (void)showBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset duration:(CGFloat)duration{
  140. XHToast *toast = [[XHToast alloc] initWithText:text];
  141. toast.duration = duration;
  142. [toast showIn:[self window] fromBottomOffset:bottomOffset];
  143. }
  144. @end
  145. @implementation UIView (XHToast)
  146. #pragma mark-中间显示
  147. - (void)showXHToastCenterWithText:(NSString *)text
  148. {
  149. [self showXHToastCenterWithText:text duration:ToastDispalyDuration];
  150. }
  151. - (void)showXHToastCenterWithText:(NSString *)text duration:(CGFloat)duration
  152. {
  153. XHToast *toast = [[XHToast alloc] initWithText:text];
  154. toast.duration = duration;
  155. [toast showIn:self];
  156. }
  157. #pragma mark-上方显示
  158. - (void)showXHToastTopWithText:(NSString *)text
  159. {
  160. [self showXHToastTopWithText:text topOffset:ToastSpace duration:ToastDispalyDuration];
  161. }
  162. - (void)showXHToastTopWithText:(NSString *)text duration:(CGFloat)duration
  163. {
  164. [self showXHToastTopWithText:text topOffset:ToastSpace duration:duration];
  165. }
  166. - (void)showXHToastTopWithText:(NSString *)text topOffset:(CGFloat)topOffset
  167. {
  168. [self showXHToastTopWithText:text topOffset:topOffset duration:ToastDispalyDuration];
  169. }
  170. - (void)showXHToastTopWithText:(NSString *)text topOffset:(CGFloat)topOffset duration:(CGFloat)duration
  171. {
  172. XHToast *toast = [[XHToast alloc] initWithText:text];
  173. toast.duration = duration;
  174. [toast showIn:self fromTopOffset:topOffset];
  175. }
  176. #pragma mark-下方显示
  177. - (void)showXHToastBottomWithText:(NSString *)text
  178. {
  179. [self showXHToastBottomWithText:text bottomOffset:ToastSpace duration:ToastDispalyDuration];
  180. }
  181. - (void)showXHToastBottomWithText:(NSString *)text duration:(CGFloat)duration
  182. {
  183. [self showXHToastBottomWithText:text bottomOffset:ToastSpace duration:duration];
  184. }
  185. - (void)showXHToastBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset
  186. {
  187. [self showXHToastBottomWithText:text bottomOffset:bottomOffset duration:ToastDispalyDuration];
  188. }
  189. - (void)showXHToastBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset duration:(CGFloat)duration
  190. {
  191. XHToast *toast = [[XHToast alloc] initWithText:text];
  192. toast.duration = duration;
  193. [toast showIn:self fromBottomOffset:bottomOffset];
  194. }
  195. @end