您的位置:首页 > 产品设计 > UI/UE

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

2011-07-26 21:38 204 查看
/article/3983301.html

自由布局,UIButton样式,下阴影,按钮事件。导航栏rightBarButtonItem增加UISegmentedControl控件。

Objective-c代码

#import <QuartzCore/QuartzCore.h>
#import "LeagueController.h"
#import "WaitDialog.h"
#import "JSONParser.h"
#import "FunUtil.h"

@implementation LeagueController
@synthesize selectDate;
@synthesize flag;
@synthesize topToolBar;
@synthesize mainView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}
return self;
}
-(id)initWithDate:(CFGregorianDate) date{
self = [super initWithNibName:nil bundle:nil];
if(self){
self.selectDate = date;
self.title = @"联赛选择";

NSArray *buttonNames = [NSArray arrayWithObjects:@"所有", @"一级", @"二级", nil];
topToolBar = [[UISegmentedControl alloc] initWithItems:buttonNames];
[topToolBar setFrame:CGRectMake(1, 1, 120, 30)];
topToolBar.selectedSegmentIndex = 0;
topToolBar.segmentedControlStyle = UISegmentedControlStyleBar;
[topToolBar addTarget:self action:@selector(selectFlag:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:topToolBar];
self.navigationItem.rightBarButtonItem = segButton;

self.mainView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:mainView];
[segButton release];
self.flag = 0;
}
return self;
}
-(void)removeAllView{
for(UIView *subView in [self.mainView subviews]){
[subView removeFromSuperview];
}
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.mainView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[[WaitDialog sharedWaitDialog] setLoadingLabel:@"加截数据..."];
[NSThread detachNewThreadSelector:@selector(fetchData) toTarget:self withObject:nil];
}
-(void)selectFlag:(UISegmentedControl *)Seg{
int index = Seg.selectedSegmentIndex;
self.flag = index;
[NSThread detachNewThreadSelector:@selector(fetchData) toTarget:self withObject:nil];
}
-(void) fetchData{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *date = [NSString stringWithFormat:@"%d-%d-%d",selectDate.year,selectDate.month,selectDate.day];
NSString *dataURL =@"";
NSArray *data = [JSONParser loadData:dataURL isAllValues:NO valueForKey:@"list"];
[self performSelectorOnMainThread:@selector(makeView:) withObject:data waitUntilDone:NO];
[pool release];
}
-(void)makeView:(NSArray*)data{
[self removeAllView];
[[WaitDialog sharedWaitDialog]endShowLoading];
if(data == nil || [data count]==0){
UILabel *nullData = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 40)];
nullData.text = @"暂时没有数据";
nullData.backgroundColor = [UIColor clearColor];
nullData.font = [UIFont boldSystemFontOfSize:20];
[self.mainView addSubview:nullData];
}else{
int count = [data count];
int i = 0;
int x = 10;
int y = 10;
int cols = 3;
int width = self.view.frame.size.width/cols -20;
int height = 30;
int contentHeight = (height+y*2)*(count/cols);

int c = count/cols+1;
for(i = 0;i<c;i++){
NSArray *league = [data objectAtIndex:i];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.layer.cornerRadius = 5;
btn.layer.shadowOffset = CGSizeMake(3, 5);
btn.layer.shadowOpacity = 0.8;
btn.layer.shadowColor = [UIColor blackColor].CGColor;
btn.frame = CGRectMake(x, y, width, height);
btn.backgroundColor = [FunUtil colorWithHexString:[league valueForKey:@"color"]];
[btn setTitleColor:[UIColor whiteColor ]forState:UIControlStateNormal];
[btn setTitle:[league valueForKey:@"cnName"] forState:UIControlStateNormal];
[btn setTag:[[league valueForKey:@"lid"] intValue]];
[btn addTarget:self action:@selector(selectLeague:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
[self.mainView addSubview:btn];
y+=height+10;
}

contentHeight = (height+10)*c+10;
CGSize newSize = CGSizeMake(self.view.frame.size.width, contentHeight);
[self.mainView setContentSize:newSize];
}
}
-(void)selectLeague:(UIButton*)sender{
[sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
sender.layer.shadowOffset = CGSizeMake(3, 5);
sender.layer.shadowOpacity = 0.8;
sender.layer.shadowColor = [UIColor blackColor].CGColor;
}
-(void)buttonDown:(UIButton*)sender{
[sender setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
sender.layer.shadowOffset = CGSizeMake(0,0);
sender.layer.shadowOpacity = 1;
sender.layer.shadowColor = [UIColor blackColor].CGColor;
}
#pragma mark TableViewDataSource Methods

- (void)dealloc {
[topToolBar release];
[mainView release];
[super dealloc];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐