//当为1时,按钮可以点击,当为2时按钮不可点击
//标红的地方需要同时设置
// ButtonDemo
//
// Created by apple on 16/5/25.
// Copyright © 2016年 apple. All rights reserved.
//
#import "ViewController.h"
#define kScreenBounds ([[UIScreen mainScreen] bounds])
#define kScreenwidth (kScreenBounds.size.width)
#define kScreenheight (kScreenBounds.size.height)
#define mBlueColor [UIColor colorWithRed:50.0/255.0 green:162.0/255.0 blue:248.0/255.0 alpha:1.0]
#define mGrayColor [UIColor colorWithRed:165/255.0 green:165/255.0 blue:165/255.0 alpha:1.0]
@interface ViewController ()
@property (nonatomic,strong)UIButton *button;
@property (nonatomic,strong)UIButton *button1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.button1 = [[UIButton alloc]initWithFrame:CGRectMake(kScreenwidth/2-50, kScreenheight/2-100, 100, 50)];
[self.button1 setBackgroundColor:mBlueColor];
[self.button1 setTitle:@"测试" forState:UIControlStateNormal];
[self.button1 addTarget:self action:@selector(two) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button1];
self.button = [[UIButton alloc]initWithFrame:CGRectMake(kScreenwidth/2-50, kScreenheight/2, 100, 50)];
[self.button setBackgroundColor:mBlueColor];
[self.button setBackgroundImage:[self createImageWithColor:mGrayColor] forState:UIControlStateDisabled];
self.button.enabled = NO;
[self.button setTitle:@"1" forState:UIControlStateNormal];
[self.button setTitle:@"2" forState:UIControlStateDisabled];
[self.button addTarget:self action:@selector(one) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void)one{
}
- (void)two{
self.button.enabled = !self.button.enabled;
if (self.button.enabled) {
[self.button setBackgroundColor:mBlueColor];
}else{
[self.button setBackgroundColor:mGrayColor];
}
}
/** 用颜色生成一张图片 */
- (UIImage *)createImageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end