自定义button
来源:互联网 发布:心理学 网络教育 编辑:程序博客网 时间:2024/04/29 19:02
我们应该建立自己的代码库,建立自己的工厂
苹果公司给我们提供了强大的利器
但是我们不应该以简简单单的实现基本功能就满足了
大牛的成长之路,都是自己慢慢深入研究
我们要有成长为大牛的目标
今天给大家写个自定义button 对于初学者来说很重要
我们要理解苹果公司提供的类库
我们写的程序应该尽量贴近原生
这样我们的程序才会更易理解 效率等方便也会有所提高
上代码了:代码不长,但是是一种思想 要好好理解
//// MyButton.h// 自定义Button#import <UIKit/UIKit.h>@interface MyButton : UIControl/** * 自定义的返回自己的类型 * * @param title <#title description#> * @param frame <#frame description#> * @param target <#target description#> * @param selector <#selector description#> * * @return <#return value description#> */+(MyButton *)createButtonWithTitle:(NSString *)title andFrame:(CGRect)frame andTarget:(id)target andSelector:(SEL)selector;@end
//// MyButton.m// 自定义Button//#import "MyButton.h"@implementation MyButton+(MyButton *)createButtonWithTitle:(NSString *)title andFrame:(CGRect)frame andTarget:(id)target andSelector:(SEL)selector{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; //UIButton *btn = [[UIButton alloc]init]; btn.frame = frame; btn.layer.cornerRadius =10.0f; btn.layer.masksToBounds = YES; btn.layer.borderWidth = 2.0; btn.backgroundColor = [UIColor colorWithRed:0.18f green:0.64f blue:0.87f alpha:1.00f]; [btn setTitle:title forState:UIControlStateNormal]; [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; return btn;}@end
//// ViewController.h// 自定义Button#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end
//// ViewController.m// 自定义Button//#import "ViewController.h"#import "MyButton.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; MyButton *btn = [MyButton createButtonWithTitle:@"Happy" andFrame:CGRectMake(10, 30, 100, 50) andTarget:self andSelector:@selector(btnClick)]; self.view.backgroundColor = [UIColor orangeColor]; [self.view addSubview:btn]; // Do any additional setup after loading the view, typically from a nib.}-(void)btnClick{ NSLog(@"按钮被点击了!");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0
- 自定义button
- 自定义BUTTON!
- 自定义button
- 自定义Button
- 自定义button
- 自定义button
- 自定义Button
- 自定义Button
- 自定义Button
- 自定义button
- 自定义checkbox,自定义button
- Button控件+自定义Button控件
- button自定义事件
- 自定义外观的Button
- 自定义的button
- Android自定义Button组件
- 自定义Button样式
- 自定义Button组件
- 【洛谷】P1421 小玉买文具
- [iOS备忘录]使用蓝牙耳机headset录音
- Android Studio App设置Activity背景图片
- SVN与GIT
- error C2440: “=”: 无法从“const char [11]”转换为“LPCWSTR”
- 自定义button
- 项目EasyUi和JS中遇到的问题总汇
- Linux包含hash_map和hash_set的not declared问题
- eclipse +maven+web应用 +tomcat jar包加载
- 【NOIP2001P/codevs1014】 装箱问题 解题报告
- 【洛谷】P1425 小鱼的游泳时间
- 第一章:机器学习基础
- Struts2学习笔记——02
- 机器学习资源