JXTopMenuView.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // JXTopMenuView.m
  3. // sjvodios
  4. //
  5. // Created by daxiong on 13-4-17.
  6. //
  7. //
  8. #import "JXTopMenuView.h"
  9. #import "JXBadgeView.h"
  10. @implementation JXTopMenuView
  11. @synthesize delegate,items,arrayBtns,selected;
  12. - (id)initWithFrame:(CGRect)frame
  13. {
  14. self = [super initWithFrame:frame];
  15. if (self) {
  16. for(int i=0;i<MAX_MENU_ITEM;i++)
  17. _showMore[i] = 0;
  18. self.backgroundColor = [UIColor clearColor];
  19. int width=frame.size.width/[items count];
  20. self.userInteractionEnabled = YES;
  21. arrayBtns = [[NSMutableArray alloc]init];
  22. arrayBage = [[NSMutableArray alloc]init];
  23. UIButton* btn;
  24. int i;
  25. for(i=0;i<[items count];i++){
  26. btn = [UIFactory createButtonWithTitle:[items objectAtIndex:i]
  27. titleFont:g_factory.font13
  28. titleColor:HEXCOLOR(0x2d2f32)
  29. normal:@"menu_bg"
  30. highlight:@"menu_bg_press"
  31. selected:@"menu_bg_bingo"];
  32. [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  33. btn.frame = CGRectMake(i*width, 0, width, frame.size.height);
  34. [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
  35. btn.tag = i;
  36. [self addSubview:btn];
  37. [arrayBtns addObject:btn];
  38. JXBadgeView* p = [[JXBadgeView alloc] initWithFrame:CGRectMake(btn.frame.size.width-16-2, 2, 16, 16)];
  39. p.badgeString = nil;
  40. p.userInteractionEnabled = NO;
  41. [btn addSubview:p];
  42. // [p release];
  43. [arrayBage addObject:p];
  44. }
  45. }
  46. return self;
  47. }
  48. -(void)dealloc{
  49. [arrayBage removeAllObjects];
  50. [arrayBtns removeAllObjects];
  51. // [arrayBage release];
  52. // [arrayBtns release];
  53. // [items release];
  54. // [super dealloc];
  55. }
  56. -(void)onClick:(UIButton*)sender{
  57. [self unSelectAll];
  58. sender.selected = YES;
  59. // NSLog(@"%d",sender.tag);
  60. selected = (int)sender.tag;
  61. if(self.delegate != nil && [self.delegate respondsToSelector:self.onClick])
  62. [self.delegate performSelectorOnMainThread:self.onClick withObject:sender waitUntilDone:NO];
  63. }
  64. -(void)unSelectAll{
  65. for(int i=0;i<[arrayBtns count];i++)
  66. ((UIButton*)[arrayBtns objectAtIndex:i]).selected = NO;
  67. selected = -1;
  68. }
  69. -(void)selectOne:(int)n{
  70. [self unSelectAll];
  71. if(n >= [self.arrayBtns count]-1 || n<0)
  72. return;
  73. ((UIButton*)[self.arrayBtns objectAtIndex:n]).selected=YES;
  74. selected = n;
  75. }
  76. -(void)setTitle:(int)n title:(NSString*)s{
  77. if(n >= [self.arrayBtns count])
  78. return;
  79. [[self.arrayBtns objectAtIndex:n] setTitle:s forState:UIControlStateNormal];
  80. }
  81. -(void)setBadge:(int)n title:(NSString*)s{
  82. if(n >= [self.arrayBtns count])
  83. return;
  84. [[arrayBage objectAtIndex:n] setBadgeString:s];
  85. }
  86. -(void)showMore:(int)index onSelected:(SEL)onSelected{
  87. if(index >= [self.arrayBtns count])
  88. return;
  89. _showMore[index] = 1;
  90. UIButton* more = [UIFactory createButtonWithImage:@"menu_normal"
  91. highlight:@"menu_press"
  92. target:delegate
  93. selector:onSelected];
  94. more.frame = CGRectMake(self.frame.size.width/[items count]-25, 17, 10, 10);
  95. UIButton* btn = [self.arrayBtns objectAtIndex:index];
  96. [btn addSubview:more];
  97. }
  98. @end