自由布局,UIButton样式,下阴影,按钮事件

http://gekie.iteye.com/blog/1133427


自由布局,UIButton样式,下阴影,按钮事件。

导航栏rightBarButtonItem增加UISegmentedControl控件。

 

 

 

 

Objective-c代码   收藏代码
  1. #import <QuartzCore/QuartzCore.h>  
  2. #import "LeagueController.h"  
  3. #import "WaitDialog.h"  
  4. #import "JSONParser.h"  
  5. #import "FunUtil.h"  
  6.   
  7. @implementation LeagueController  
  8. @synthesize selectDate;  
  9. @synthesize flag;  
  10. @synthesize topToolBar;  
  11. @synthesize mainView;  
  12. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {  
  13.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  14.     if (self) {  
  15.           
  16.     }  
  17.     return self;  
  18. }  
  19. -(id)initWithDate:(CFGregorianDate) date{  
  20.     self = [super initWithNibName:nil bundle:nil];  
  21.     if(self){  
  22.         self.selectDate = date;  
  23.         self.title = @"联赛选择";  
  24.       
  25.         NSArray *buttonNames = [NSArray arrayWithObjects:@"所有", @"一级", @"二级", nil];  
  26.         topToolBar = [[UISegmentedControl alloc] initWithItems:buttonNames];  
  27.         [topToolBar setFrame:CGRectMake(1112030)];  
  28.         topToolBar.selectedSegmentIndex = 0;  
  29.         topToolBar.segmentedControlStyle = UISegmentedControlStyleBar;  
  30.         [topToolBar addTarget:self action:@selector(selectFlag:) forControlEvents:UIControlEventValueChanged];  
  31.         UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:topToolBar];  
  32.         self.navigationItem.rightBarButtonItem = segButton;   
  33.           
  34.         self.mainView = [[UIScrollView alloc]initWithFrame:CGRectMake(00, self.view.frame.size.width, self.view.frame.size.height)];  
  35.         [self.view addSubview:mainView];  
  36.         [segButton release];  
  37.         self.flag = 0;  
  38.     }  
  39.     return self;  
  40. }  
  41. -(void)removeAllView{  
  42.     for(UIView *subView in [self.mainView subviews]){  
  43.         [subView removeFromSuperview];  
  44.     }  
  45. }  
  46. - (void)viewDidAppear:(BOOL)animated{  
  47.     [super viewDidAppear:animated];  
  48.     self.mainView.frame = CGRectMake(00, self.view.frame.size.width, self.view.frame.size.height);  
  49.     [[WaitDialog sharedWaitDialog] setLoadingLabel:@"加截数据..."];  
  50.     [NSThread detachNewThreadSelector:@selector(fetchData) toTarget:self withObject:nil];  
  51. }  
  52. -(void)selectFlag:(UISegmentedControl *)Seg{  
  53.     int index = Seg.selectedSegmentIndex;  
  54.     self.flag = index;  
  55.     [NSThread detachNewThreadSelector:@selector(fetchData) toTarget:self withObject:nil];  
  56. }  
  57. -(void) fetchData{  
  58.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  59.     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;  
  60.     NSString *date = [NSString stringWithFormat:@"%d-%d-%d",selectDate.year,selectDate.month,selectDate.day];  
  61.     NSString *dataURL =@"";  
  62.     NSArray *data = [JSONParser loadData:dataURL isAllValues:NO valueForKey:@"list"];  
  63.     [self performSelectorOnMainThread:@selector(makeView:) withObject:data waitUntilDone:NO];  
  64.     [pool release];  
  65. }  
  66. -(void)makeView:(NSArray*)data{  
  67.     [self removeAllView];  
  68.     [[WaitDialog sharedWaitDialog]endShowLoading];  
  69.     if(data == nil || [data count]==0){  
  70.         UILabel *nullData = [[UILabel alloc]initWithFrame:CGRectMake(1010, self.view.frame.size.width-2040)];  
  71.         nullData.text = @"暂时没有数据";  
  72.         nullData.backgroundColor = [UIColor clearColor];  
  73.         nullData.font = [UIFont boldSystemFontOfSize:20];  
  74.         [self.mainView addSubview:nullData];  
  75.     }else{  
  76.         int count = [data count];  
  77.         int i = 0;  
  78.         int x = 10;  
  79.         int y = 10;  
  80.         int cols = 3;  
  81.         int width = self.view.frame.size.width/cols -20;  
  82.         int height = 30;  
  83.         int contentHeight = (height+y*2)*(count/cols);  
  84.           
  85.         int c = count/cols+1;  
  86.         for(i = 0;i<c;i++){  
  87.             NSArray *league = [data objectAtIndex:i];  
  88.             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
  89.             btn.layer.cornerRadius = 5;  
  90.             btn.layer.shadowOffset =  CGSizeMake(35);  
  91.             btn.layer.shadowOpacity = 0.8;  
  92.             btn.layer.shadowColor =  [UIColor blackColor].CGColor;  
  93.             btn.frame = CGRectMake(x, y, width, height);  
  94.             btn.backgroundColor =  [FunUtil colorWithHexString:[league valueForKey:@"color"]];  
  95.             [btn setTitleColor:[UIColor whiteColor ]forState:UIControlStateNormal];  
  96.             [btn setTitle:[league valueForKey:@"cnName"] forState:UIControlStateNormal];  
  97.             [btn setTag:[[league valueForKey:@"lid"] intValue]];  
  98.             [btn addTarget:self action:@selector(selectLeague:) forControlEvents:UIControlEventTouchUpInside];  
  99.             [btn addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];  
  100.             [self.mainView addSubview:btn];  
  101.             y+=height+10;  
  102.         }  
  103.           
  104.         contentHeight = (height+10)*c+10;  
  105.         CGSize newSize = CGSizeMake(self.view.frame.size.width, contentHeight);  
  106.         [self.mainView setContentSize:newSize];  
  107.     }  
  108. }  
  109. -(void)selectLeague:(UIButton*)sender{  
  110.     [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];  
  111.     sender.layer.shadowOffset =  CGSizeMake(35);  
  112.     sender.layer.shadowOpacity = 0.8;  
  113.     sender.layer.shadowColor =  [UIColor blackColor].CGColor;  
  114. }  
  115. -(void)buttonDown:(UIButton*)sender{  
  116.     [sender setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];  
  117.     sender.layer.shadowOffset =  CGSizeMake(0,0);  
  118.     sender.layer.shadowOpacity = 1;  
  119.     sender.layer.shadowColor =  [UIColor blackColor].CGColor;  
  120. }  
  121. #pragma mark TableViewDataSource Methods  
  122.   
  123. - (void)dealloc {  
  124.     [topToolBar release];  
  125.     [mainView release];  
  126.     [super dealloc];  
  127. }  
  128.   
  129.   
  130. @end  

 


猜你喜欢

转载自blog.csdn.net/microchenhong/article/details/6636042