#import "ViewController.h"
@interface ViewController ()
{
UIImageView *imageViewTwo;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
UILabel *label=[[UILabel alloc] initWithFrame:CGRectZero];
label.text=@"";
CGRect rect=[label.text boundingRectWithSize:CGSizeMake(300,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];
label.frame=CGRectMake(0, 20, 300, rect.size.height);
label.numberOfLines=0;
label.backgroundColor=[UIColor cyanColor];
[self.view addSubview:label];
*/
//UIButton
//凡是继承于UIControl的控件都具有相应事件的能力
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(10, 100, 100, 100);
[ button setTitle:@"按钮" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//button.backgroundColor=[UIColor lightGrayColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
button.backgroundColor=[UIColor lightGrayColor];
//添加事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
/*--------------------------------------------------------------------*/
//UIButton *button2=[UIButton buttonWithType:UIButtonTypeSystem];//系统的颜色
UIButton *button2=[UIButton buttonWithType:UIButtonTypeCustom];//是他与背景相关联
button2.frame=CGRectMake(130, 100, 100, 100);
[button2 setTitle:@"自然状态" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button2 setTitle:@"选中状态" forState:UIControlStateSelected];
[button2 setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
//button2.selected=NO;//选中状态
button2.backgroundColor=[UIColor lightGrayColor];
//添加事件
[button2 addTarget:self action:@selector(buttonSeletedAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
UIButton *button3=[UIButton buttonWithType:UIButtonTypeCustom];
button3.frame=CGRectMake(250, 100, 100, 100);
[button3 setTitle:@"自然状态" forState:UIControlStateNormal];
[button3 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[button3 setTitle:@"编辑状态" forState:UIControlStateDisabled];
[button3 setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
button3.enabled=NO;
[self.view addSubview:button3];
/*--------------------------------------------*/
UIButton *aButton=[UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame=CGRectMake(10, 220, 100, 100);
//自然状态
[aButton setImage:[UIImage imageNamed:@"Button/Off"] forState:UIControlStateNormal];
//高亮状态
[aButton setImage:[UIImage imageNamed:@"Button/On"] forState:UIControlStateHighlighted];
[aButton addTarget:self action:@selector(aButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
/*-------------------------------------------*/
UIButton *bButton=[UIButton buttonWithType:UIButtonTypeCustom];
bButton.frame=CGRectMake(130, 220, 100, 100);
[bButton setImage:[UIImage imageNamed:@"Button/Off"] forState:UIControlStateNormal];
//被选中状态
[bButton setImage:[UIImage imageNamed:@"Button/On"] forState:UIControlStateSelected];
[bButton addTarget:self action:@selector(bButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bButton];
//应用按照说话
UIButton *cButton=[UIButton buttonWithType:UIButtonTypeCustom];
cButton.frame=CGRectMake(Screen_width/2-40, 400, 80, 100);
[cButton setImage:[UIImage imageNamed:@"Button/speakPhone"] forState:UIControlStateNormal];
//按下时响应的事件
[cButton addTarget:self action:@selector(cButtonDown:) forControlEvents:UIControlEventTouchDown];
//松开手时响应的事件
[cButton addTarget:self action:@selector(cButtonUp:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cButton];
UIImageView *imageViewOne=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Button/speak_two"]];
imageViewOne.frame=CGRectMake(Screen_width/2+40, 400-30, 30, 30);
[self.view addSubview:imageViewOne];
imageViewTwo=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Button/speak_one"]];
imageViewTwo.frame=CGRectMake(Screen_width/2+40, 400-30, 30, 30);
imageViewTwo.animationImages=@[[UIImage imageNamed:@"Button/speak_one"],[UIImage imageNamed:@"Button/speak_two"]];
imageViewTwo.animationDuration=0.5;
imageViewTwo.hidden=YES;
[self.view addSubview:imageViewTwo];
}
#pragma mark-------方法实现---------
-(void)buttonAction:(UIButton *)sender
{
NSLog(@"按钮相应事件");
}
-(void)buttonSeletedAction:(UIButton *)sender
{
sender.selected=!sender.selected;
NSLog(@"按钮2响应事件");
}
-(void)aButtonAction:(UIButton *)sender
{
NSLog(@"aButton响应事件");
}
-(void)bButtonAction:(UIButton *)sender
{
sender.selected=!sender.selected;
NSLog(@"bButton响应事件");
}
-(void)cButtonDown:(UIButton *)sender
{
imageViewTwo.hidden=NO;
[imageViewTwo startAnimating];
NSLog(@"cButtonDown");
}
-(void)cButtonUp:(UIButton *)sender
{
imageViewTwo.hidden=YES;
[imageViewTwo stopAnimating];
NSLog(@"cButtonUp");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end