react-native-navigation的迁移库

MMDrawerBarButtonItem.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Copyright (c) 2013 Mutual Mobile (http://mutualmobile.com/)
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #import "MMDrawerBarButtonItem.h"
  21. @interface MMDrawerMenuButtonView : UIButton
  22. @property (nonatomic,strong) UIColor * menuButtonNormalColor;
  23. @property (nonatomic,strong) UIColor * menuButtonHighlightedColor;
  24. @property (nonatomic,strong) UIColor * shadowNormalColor;
  25. @property (nonatomic,strong) UIColor * shadowHighlightedColor;
  26. -(UIColor *)menuButtonColorForState:(UIControlState)state;
  27. -(void)setMenuButtonColor:(UIColor *)color forState:(UIControlState)state;
  28. -(UIColor *)shadowColorForState:(UIControlState)state;
  29. -(void)setShadowColor:(UIColor *)color forState:(UIControlState)state;
  30. @end
  31. @implementation MMDrawerMenuButtonView
  32. -(instancetype)initWithFrame:(CGRect)frame{
  33. self = [super initWithFrame:frame];
  34. if(self){
  35. [self setMenuButtonNormalColor:[[UIColor whiteColor] colorWithAlphaComponent:0.9f]];
  36. [self setMenuButtonHighlightedColor:[UIColor colorWithRed:139.0/255.0
  37. green:135.0/255.0
  38. blue:136.0/255.0
  39. alpha:0.9f]];
  40. [self setShadowNormalColor:[[UIColor blackColor] colorWithAlphaComponent:0.5f]];
  41. [self setShadowHighlightedColor:[[UIColor blackColor] colorWithAlphaComponent:0.2f]];
  42. }
  43. return self;
  44. }
  45. -(UIColor *)menuButtonColorForState:(UIControlState)state{
  46. UIColor * color;
  47. switch (state) {
  48. case UIControlStateNormal:
  49. color = self.menuButtonNormalColor;
  50. break;
  51. case UIControlStateHighlighted:
  52. color = self.menuButtonHighlightedColor;
  53. break;
  54. default:
  55. break;
  56. }
  57. return color;
  58. }
  59. -(void)setMenuButtonColor:(UIColor *)color forState:(UIControlState)state{
  60. switch (state) {
  61. case UIControlStateNormal:
  62. [self setMenuButtonNormalColor:color];
  63. break;
  64. case UIControlStateHighlighted:
  65. [self setMenuButtonHighlightedColor:color];
  66. break;
  67. default:
  68. break;
  69. }
  70. [self setNeedsDisplay];
  71. }
  72. -(UIColor *)shadowColorForState:(UIControlState)state{
  73. UIColor * color;
  74. switch (state) {
  75. case UIControlStateNormal:
  76. color = self.shadowNormalColor;
  77. break;
  78. case UIControlStateHighlighted:
  79. color = self.shadowHighlightedColor;
  80. break;
  81. default:
  82. break;
  83. }
  84. return color;
  85. }
  86. -(void)setShadowColor:(UIColor *)color forState:(UIControlState)state{
  87. switch (state) {
  88. case UIControlStateNormal:
  89. [self setShadowNormalColor:color];
  90. break;
  91. case UIControlStateHighlighted:
  92. [self setShadowHighlightedColor:color];
  93. break;
  94. default:
  95. break;
  96. }
  97. [self setNeedsDisplay];
  98. }
  99. -(void)drawRect:(CGRect)rect{
  100. //// General Declarations
  101. CGContextRef context = UIGraphicsGetCurrentContext();
  102. //Sizes
  103. CGFloat buttonWidth = CGRectGetWidth(self.bounds)*.80;
  104. CGFloat buttonHeight = CGRectGetHeight(self.bounds)*.16;
  105. CGFloat xOffset = CGRectGetWidth(self.bounds)*.10;
  106. CGFloat yOffset = CGRectGetHeight(self.bounds)*.12;
  107. CGFloat cornerRadius = 1.0;
  108. //// Color Declarations
  109. UIColor* buttonColor = [self menuButtonColorForState:self.state];
  110. UIColor* shadowColor = [self shadowColorForState:self.state];
  111. //// Shadow Declarations
  112. UIColor* shadow = shadowColor;
  113. CGSize shadowOffset = CGSizeMake(0.0, 1.0);
  114. CGFloat shadowBlurRadius = 0;
  115. //// Top Bun Drawing
  116. UIBezierPath* topBunPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(xOffset, yOffset, buttonWidth, buttonHeight) cornerRadius:cornerRadius];
  117. CGContextSaveGState(context);
  118. CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow.CGColor);
  119. [buttonColor setFill];
  120. [topBunPath fill];
  121. CGContextRestoreGState(context);
  122. //// Meat Drawing
  123. UIBezierPath* meatPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(xOffset, yOffset*2 + buttonHeight, buttonWidth, buttonHeight) cornerRadius:cornerRadius];
  124. CGContextSaveGState(context);
  125. CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow.CGColor);
  126. [buttonColor setFill];
  127. [meatPath fill];
  128. CGContextRestoreGState(context);
  129. //// Bottom Bun Drawing
  130. UIBezierPath* bottomBunPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(xOffset, yOffset*3 + buttonHeight*2, buttonWidth, buttonHeight) cornerRadius:cornerRadius];
  131. CGContextSaveGState(context);
  132. CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow.CGColor);
  133. [buttonColor setFill];
  134. [bottomBunPath fill];
  135. CGContextRestoreGState(context);
  136. }
  137. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  138. [super touchesBegan:touches withEvent:event];
  139. [self setNeedsDisplay];
  140. }
  141. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
  142. [super touchesEnded:touches withEvent:event];
  143. [self setNeedsDisplay];
  144. }
  145. -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
  146. [super touchesCancelled:touches withEvent:event];
  147. [self setNeedsDisplay];
  148. }
  149. -(void)setSelected:(BOOL)selected{
  150. [super setSelected:selected];
  151. [self setNeedsDisplay];
  152. }
  153. -(void)setHighlighted:(BOOL)highlighted{
  154. [super setHighlighted:highlighted];
  155. [self setNeedsDisplay];
  156. }
  157. -(void)setTintColor:(UIColor *)tintColor{
  158. if([super respondsToSelector:@selector(setTintColor:)]){
  159. [super setTintColor:tintColor];
  160. }
  161. }
  162. -(void)tintColorDidChange{
  163. [self setNeedsDisplay];
  164. }
  165. @end
  166. @interface MMDrawerBarButtonItem ()
  167. @property (nonatomic,strong) MMDrawerMenuButtonView * buttonView;
  168. @end
  169. @implementation MMDrawerBarButtonItem
  170. +(UIImage*)drawerButtonItemImage{
  171. static UIImage *drawerButtonImage = nil;
  172. static dispatch_once_t onceToken;
  173. dispatch_once(&onceToken, ^{
  174. UIGraphicsBeginImageContextWithOptions( CGSizeMake(26, 26), NO, 0 );
  175. //// Color Declarations
  176. UIColor* fillColor = [UIColor whiteColor];
  177. //// Frames
  178. CGRect frame = CGRectMake(0, 0, 26, 26);
  179. //// Bottom Bar Drawing
  180. UIBezierPath* bottomBarPath = [UIBezierPath bezierPathWithRect: CGRectMake(CGRectGetMinX(frame) + floor((CGRectGetWidth(frame) - 16) * 0.50000 + 0.5), CGRectGetMinY(frame) + floor((CGRectGetHeight(frame) - 1) * 0.72000 + 0.5), 16, 1)];
  181. [fillColor setFill];
  182. [bottomBarPath fill];
  183. //// Middle Bar Drawing
  184. UIBezierPath* middleBarPath = [UIBezierPath bezierPathWithRect: CGRectMake(CGRectGetMinX(frame) + floor((CGRectGetWidth(frame) - 16) * 0.50000 + 0.5), CGRectGetMinY(frame) + floor((CGRectGetHeight(frame) - 1) * 0.48000 + 0.5), 16, 1)];
  185. [fillColor setFill];
  186. [middleBarPath fill];
  187. //// Top Bar Drawing
  188. UIBezierPath* topBarPath = [UIBezierPath bezierPathWithRect: CGRectMake(CGRectGetMinX(frame) + floor((CGRectGetWidth(frame) - 16) * 0.50000 + 0.5), CGRectGetMinY(frame) + floor((CGRectGetHeight(frame) - 1) * 0.24000 + 0.5), 16, 1)];
  189. [fillColor setFill];
  190. [topBarPath fill];
  191. drawerButtonImage = UIGraphicsGetImageFromCurrentImageContext();
  192. });
  193. return drawerButtonImage;
  194. }
  195. -(instancetype)initWithTarget:(id)target action:(SEL)action{
  196. if((floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)){
  197. return [self initWithImage:[self.class drawerButtonItemImage]
  198. style:UIBarButtonItemStylePlain
  199. target:target
  200. action:action];
  201. }
  202. else {
  203. MMDrawerMenuButtonView * buttonView = [[MMDrawerMenuButtonView alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
  204. [buttonView addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
  205. self = [self initWithCustomView:buttonView];
  206. if(self){
  207. [self setButtonView:buttonView];
  208. }
  209. self.action = action;
  210. self.target = target;
  211. return self;
  212. }
  213. }
  214. -(instancetype)initWithCoder:(NSCoder *)aDecoder{
  215. // non-ideal way to get the target/action, but it works
  216. UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCoder: aDecoder];
  217. return [self initWithTarget:barButtonItem.target action:barButtonItem.action];
  218. }
  219. -(void)touchUpInside:(id)sender{
  220. #pragma clang diagnostic push
  221. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  222. [self.target performSelector:self.action withObject:sender];
  223. #pragma clang diagnostic pop;
  224. }
  225. -(UIColor *)menuButtonColorForState:(UIControlState)state{
  226. return [self.buttonView menuButtonColorForState:state];
  227. }
  228. -(void)setMenuButtonColor:(UIColor *)color forState:(UIControlState)state{
  229. [self.buttonView setMenuButtonColor:color forState:state];
  230. }
  231. -(UIColor *)shadowColorForState:(UIControlState)state{
  232. return [self.buttonView shadowColorForState:state];
  233. }
  234. -(void)setShadowColor:(UIColor *)color forState:(UIControlState)state{
  235. [self.buttonView setShadowColor:color forState:state];
  236. }
  237. -(void)setTintColor:(UIColor *)tintColor{
  238. if([super respondsToSelector:@selector(setTintColor:)]){
  239. [super setTintColor:tintColor];
  240. }
  241. if([self.buttonView respondsToSelector:@selector(setTintColor:)]){
  242. [self.buttonView setTintColor:tintColor];
  243. }
  244. }
  245. @end