UIButton
2015-08-08 16:04
459 查看
#import "AppDelegate.h" @interface AppDelegate () @property(nonatomic, assign)BOOL isSelected; @property(nonatomic, assign)BOOL isClick; @end @implementation AppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; [_window release]; // UIButton提供的外观控制API:标题,背景图片,前景图片 // UIButton: UIControl: UIView // 用button自己的便利构造器的方式来创建对象 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; // 指定button的位置和大小 button.frame = CGRectMake(140, 280, 100, 40); // 设置背景颜色 button.backgroundColor = [UIColor blueColor]; [self.window addSubview:button]; // button不用release // 给button设置标题 [button setTitle:@"确认" forState:UIControlStateNormal]; // 获取指定状态下的标题 NSString *normalTitle = [button titleForState:UIControlStateNormal]; NSLog(@"%@", normalTitle); // 标题的字体大小 button.titleLabel.font = [UIFont systemFontOfSize:24]; // // 设置圆角 // button.layer.cornerRadius = 10; // // 设置边框 // button.layer.borderWidth = 1; button.tag = 999; // 点击方法:button最重要的方法 [button addTarget:self action:@selector(changePic:) forControlEvents:UIControlEventTouchUpInside]; // 移除按钮的点击事件,有什么用? UIButton *buttont = [UIButton buttonWithType:UIButtonTypeSystem]; buttont.frame = CGRectMake(140, 360, 100, 40); buttont.backgroundColor = [UIColor blueColor]; [self.window addSubview:buttont]; [buttont setTitle:@"确认" forState:UIControlStateNormal]; [buttont removeTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside]; // 重新创建一个button UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; button1.frame = CGRectMake(90, 100, 200, 150); button1.backgroundColor = [UIColor redColor]; button1.titleLabel.font = [UIFont systemFontOfSize:36]; button1.layer.cornerRadius = 30; button1.layer.borderWidth = 10; // 改变字体颜色 // 这样设置只会改变文本所占格的背景颜色.不会改变字体颜色 // button1.titleLabel.backgroundColor = [UIColor orangeColor]; // 改变按钮标题颜色 [button1 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; // 获取按钮标题颜色 UIColor *normalTitleColor = [button1 titleColorForState:UIControlStateNormal]; NSLog(@"%@", normalTitleColor); // 设置标题阴影颜色 没看见效果 怎样设置标题阴影? [button1 setTitleShadowColor:[UIColor greenColor] forState:UIControlStateNormal]; // 怎么改变边框颜色? [button1 setTitle:@"点击即送" forState:UIControlStateNormal]; [self.window addSubview:button1]; [button1 addTarget:self action:@selector(knock:) forControlEvents:UIControlEventTouchUpInside]; button1.tag = 1000; UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom]; button2.frame = CGRectMake(140, 320, 100, 40); // button2.layer.cornerRadius = 10; // button2.layer.borderWidth = 3; [button2 setTitle:@"拒绝" forState:UIControlStateNormal]; button2.titleLabel.font = [UIFont systemFontOfSize:24]; [self.window addSubview:button2]; [button2 addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside]; button2.tag = 1001; // 给按钮设置背景图片 // 只需要写入图片的名字就可以 [button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal]; [button2 setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; // 给按钮设置按钮图片 [button setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal]; // 获取按钮图片 // 怎样通过定义一个新图片查找它的图片名字来进行判断? UIImage *normalImage = [button imageForState:UIControlStateNormal]; NSLog(@"%@", normalImage); // 打印出的是地址 [button2 setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal]; // 如果想使用setImage设置图片的话,button的类型要调整成custom // setImage方法不会把图片放大或缩小,所以要注意视图尺寸大小 // setBackgroundImage则会改变图片大小适应背景 self.isSelected = YES; self.isClick = NO; return YES; } - (void)login:(UIButton *)button { NSLog(@"移除按钮"); } - (void)knock:(UIButton *)button { // 等号后边本来是一个UIView型 通过前边加的括号转化为UIButton button = (UIButton *)[self.window viewWithTag:1000]; // 按钮的点击方法会传过来一个相应类型的button,谁触发了这个点击方法,相应的button就是谁的对象 // 判断当前按钮的标题 NSLog(@"%@", button.currentTitle); // 打印标题 if (([button.currentTitle isEqual: @"点击即送"])) { [button setTitle:@"屠龙宝刀" forState:UIControlStateNormal]; } else { [button setTitle:@"点击即送" forState:UIControlStateNormal]; } NSLog(@"%ld", (long)button.tag); // 这个long系统给加的 可以不加 } // 为什么有的时候按钮点击后图片开关和文字没有同时改变导致错位? // 下边这两个方法有问题 有可能是不能将两个变换写在一个方法里,总之每次开始运行的时候 需要点两遍才变 会导致两个变换不统一 // 更换按钮的背景图片 - (void)changePic:(UIButton *)button { button = (UIButton *)[self.window viewWithTag:999]; NSLog(@"%@", button.currentTitle); if ([button.currentTitle isEqual:@"确认"]) { [button setTitle:@"取消" forState:UIControlStateNormal]; } else { [button setTitle:@"确认" forState:UIControlStateNormal]; } NSLog(@"%ld", (long)button.tag); // 判断 if (self.isSelected) { [button setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; } else { [button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal]; } self.isSelected = !self.isSelected; if (self.isClick) { [button setImage:[UIImage imageNamed:@"BtnOn"] forState:UIControlStateNormal]; } else { [button setImage:[UIImage imageNamed:@"BtnOff"] forState:UIControlStateNormal]; } self.isClick = !self.isClick; } - (void)changeImage:(UIButton *)button { button = (UIButton *)[self.window viewWithTag:1001]; NSLog(@"%@", button.currentTitle); if ([button.currentTitle isEqual:@"拒绝"]) { [button setTitle:@"取消" forState:UIControlStateNormal]; } else { [button setTitle:@"拒绝" forState:UIControlStateNormal]; } NSLog(@"%ld", (long)button.tag); if (self.isClick) { [button setImage:[UIImage imageNamed:@"BtnOn"] forState:UIControlStateNormal]; } else { [button setImage:[UIImage imageNamed:@"BtnOff"] forState:UIControlStateNormal]; } self.isClick = !self.isClick; // 判断 if (self.isSelected) { [button setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; } else { [button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal]; } self.isSelected = !self.isSelected; } @end
相关文章推荐
- android stduio 引入 .so
- UISegmentedControl方法与属性的总结
- UI08_UItableView(关于cell滑动条消失,隐藏cell线,解决cell的线不对应问题)
- 周赛-Equidistant String
- 周赛-Equidistant String 分类: 比赛 2015-08-08 15:44 6人阅读 评论(0) 收藏
- iOS基础-UIKit框架-触摸事件-事件传递
- Xib中UIScrollView添加约束步骤
- UILabel与UIFont的用法和属性的一些总结
- 蓝牙协议(bluetooth spec)
- UIActivityIndicatorView控件的属性和方法
- UIProgress控件的属性和方法
- UIPageControll 的属性和用法
- Ultra-QuickSort
- IOS中UIAlertView(警告框)常用方法总结
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
- IOS中UIActionSheet使用方法详解
- 手机开发之H5+规范的原生UI警示框
- UITextField
- 2015多校第四场1003 hdu 5329 Question for the Leader
- Android编译failed to find Build Tools revision 23.0.0 rc2解决