自由布局,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
自由布局,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
相关文章推荐
- 自由布局,UIButton样式,下阴影,按钮事件
- Android相对布局+圆角按钮+Shape样式
- 如何在html中直接给某个按钮置灰(样式、去除绑定事件), querySelector与jquery selector的不同之处
- 圆形按钮样式(包含属性触发器、事件触发器)
- iOS-UIButton-文字位置,字体大小,边角样式,button种类,点击事件,内容位置
- 修改ActionBar的样式(overflow按钮图片修改,AcitonBar背景颜色,去掉ActionBar下面的阴影)
- listView布局中按钮点击事件放到activity或者fragment中响应
- java实现简单的页面布局和按钮单击事件(学习中)
- wpf 如何对window样式中的close按钮添加关闭事件
- 四种方式写按钮点击事件和Android 中常用的布局
- GUI_窗体添加按钮并对按钮添加事件案例、布局
- 【Android】利用Java代码布局,按钮添加点击事件
- RecyclerView onItemClick 按钮和布局都有单击事件时的处理方式
- html中 按钮样式 怎么把按钮的边框阴影去掉
- UIButton的UIButtonTypeRoundedRect样式导致按钮图象色彩发生改变
- Android 布局研究,按钮,TextView添加阴影效果,直接连接
- 22 Notification样式设置内部按钮点击事件
- Swift - 自由调整图标按钮中的图标和文字位置(扩展UIButton)
- 用block改写UIButton点击事件和UIAlerView的按钮点击代理方法
- 搜索条UISearchBar自定义样式 去掉上下阴影下划线 修改取消按钮的文字、颜色 - Swift