1、UIButton:
1.1、
#pragma mark 通過代碼建立UIButton
-(void)makeUIButtonInSource{
UIButton *btn = [[UIButton alloc]init]; //初始化UIButton
btn.frame = CGRectMake(0, 0, 100, 100); //為UIButton指定所在的父控件的位置
[btn setTitle:@"點我啊" forState:UIControlStateNormal]; //為UIButton 設定在正常狀态下的title(text)
[btn setTitle"@"摸我幹啥" forState:UIControlStateHighlighted]; //為UIButton 設定在點選狀态下的title(text)
//設定正常狀态下的UIButton的title顔色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//設定被點選狀态下的UIButton的title的顔色
[btn setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
//設定正常狀态下的UIButton的背景圖檔
[btn setBackgroundImage:[UIImage imageNamed:@"btn_01.png"] forState:(UIControlStateNormal)];
//設定被點選狀态下的UIButton的背景圖檔
[btn setBackgroundImage:[UIImage imageNamed:@"btn_02.png"] forState:UIControlStateHighlighted];
//為UIButton 添加點選事件方法-------通過addTarget的方式
[btn addTarget: self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//将UIButton添加到它的父控件上
[self.view addSubview:btn];
}
#pragma mark 按鈕點選事件
-(void)onClick:(UIButton *)btn{
NSLog(@"摸我幹啥!");
}
1.2、
#pragma mark view屬性
-(void)onClick:(UIButton *)btn{
NSLog(@"摸我幹啥!");
[self setAnimationForImageButton:^{
//移動按鈕
CGRect tempFram = _imageButton.frame;
tempFram.origin.x -= 100;
tempFram.origin.y -= 100;
_imageButton.frame =tempFram;
// CGPoint tempCenter = _imageButton.center;
// tempCenter.x +=100;
// tempCenter.y +=100;
// _imageButton.center = tempCenter;
//放大縮小按鈕
// CGRect tempFrame = _imageButton.frame;
// tempFrame.size.height -= 10;
// tempFrame.size.width -= 10;
// _imageButton.frame = tempFrame;
//大于1為放大,小于為縮小
// _imageButton.transform = CGAffineTransformScale(_imageButton.transform, 1.2, 1.2);
//旋轉圖檔
// CGFloat angle = -M_PI_4; //正數為逆時針,負數為順時針
// _imageButton.transform = CGAffineTransformRotate(_imageButton.transform, angle);
//将縮放,旋轉後的圖檔恢複到初始狀态
// _imageButton.transform = CGAffineTransformIdentity;
}];
}
#pragma mark 通過block的方式可以将相同的代碼抽取出來進行重構
// void (^block)() =^(){};
-(void)setAnimationForImageButton:(void (^)()) block{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:20];
block();
[UIView commitAnimations];
}