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];
}
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];
}