@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *moveorigin;
- (IBAction)move:(UIButton *)sender;
- (IBAction)change:(UIButton *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)move:(UIButton *)sender {
//为每个按钮设置不同的tag值,然后在这个方法中就可以根据sender.tag来判断用户当前点击的是哪个按钮
//1、获取原始的frame
CGRect Origin = self.moveorigin.frame;
//2、修改frame
switch (sender.tag) {
case 10:
//左
Origin.origin.x -= 10;
break;
case 20:
//上
Origin.origin.y -= 10;
break;
case 30:
//右
Origin.origin.x += 10;
break;
case 40:
//下
Origin.origin.y += 10;
break;
}
//通过center移动位置
//center表示的是控件中心点的坐标
//frame获取的x和y表示的是元素左上角的坐标
//CGPoint centerPoint = self.moveorigin.center;
self.moveorigin.frame = Origin;
}
- (IBAction)change:(UIButton *)sender {
CGRect Size = self.moveorigin.frame;
switch (sender.tag) {
case 100:
Size.size.width += 10;
Size.size.height += 10;
break;
case 200:
Size.size.height -= 10;
Size.size.width -= 10;
break;
}
//通过bounds修改大小
//bounds虽然是CGRect类型,但是x,y的值始终是0,所以只能通过bounds修改大小
//1、获取原始大小
CGRect Bounds = self.moveorigin.bounds;
//把结构体转为字符串输出
// NSLog(@"%@",NSStringFromCGRect(Bounds));
// self.moveorigin.frame = Size;
//通过动画实现
//开启动画
// [UIView beginAnimations:nil context:nil];
//设置动画执行时间
// [UIView setAnimationDuration:5];
//--------------- 要制定动画的代码------------
// self.moveorigin.bounds = Bounds;
// self.moveorigin.frame = Size;
// 提交动画
// [UIView commitAnimations];
// 通过block方式来实现动画
[UIView animateWithDuration:1.0 animations:^{
//执行动画的代码
self.moveorigin.frame = Size;
}];
}
@end
iOS按钮的使用和动画的实现
最新推荐文章于 2023-02-03 15:35:16 发布