UIButton

来源:互联网 发布:喷绘用什么软件做 编辑:程序博客网 时间:2024/06/14 03:36
#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
0 0
原创粉丝点击