1.核心类
UIView:视图,屏幕上能看的见摸得着的东西都是UIView,如按钮,文本标签,表格等。
UIViewController:内部默认有个UIView视图,负责管理UIView的生命周期装配数据到UIView上显示,处理UIView事件,以及UIView之间的切换。
2.每一个应用程序都有属于自己的UIWindow 。分为状态栏和下面的view。UIWindow继承UIView
3.UIView的属性:
(1) frame:它是以父控件的左上角为原点。控制控件的位置和大小。
(2) bounds:它控制控件的大小,以自己本身的左上角为原点。默认一般为(0,0,weith,height).
(3) center:控制控件的位置(中心点);
(4) tag:给控件一个标识,通过获取tag值,可以知道是哪个控件。NSInteger类型。
(5) tramsform:形变,即控件的旋转,放大/缩小,平移
上代码: (这里的控件是直接拖得,按钮的点击事件也是直接拖的)
#import "ViewController.h"
@interface ViewController ()
//需要改变的控件
@property (weak, nonatomic) IBOutlet UIButton *btn;
@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.
}
//自定义一个block,减少添加动画时的重复代码。
-(void)clickWithBlock:(void (^)())animationBolck{
// 这个动画方法4.0以上的可以用
[UIView animateWithDuration:.5 animations:^{
animationBolck();
}];
//这种动画慢慢的被面上的方法取代了
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:1.0];
// animationBolck();
// [UIView commitAnimations];
}
//控制控件的上下左右移动,
- (IBAction)move:(UIButton *)sender {
[self clickWithBlock:^{
//获取被点击按钮的tag
NSInteger tag = sender.tag;
//获取原本的位置
CGRect frame = _btn.frame ;
switch (tag) {
case 1:
frame.origin.y -=10;
break;
case 2:
frame.origin.x -=5;
break;
case 3:
frame.origin.y +=10;
break;
case 4:
frame.origin.x +=5;
break;
}
//最后把改变后的位置赋值给控件
_btn.frame = frame;
}];
}
//控件的旋转
- (IBAction)rotain:(UIButton *)sender {
[self clickWithBlock:^{
//因为左右旋转只是一个正负符号问题,通过三目运算符来控制
int a = sender.tag == 11? -1:1;
_btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4*a);
}];
}
//控件的放大缩小
- (IBAction)scale:(UIButton *)sender {
[self clickWithBlock:^{
CGFloat a = (sender.tag == 13? 1.2:0.8);
_btn.transform = CGAffineTransformScale(_btn.transform, a,a );
}];
}
@end
同时,放大/缩小还可以通过改变bounds/frame的值来实现.比如放大:
CGRect btnBounds = _btn.bounds;
btnBounds.size.height +=20;
btnBounds.size.weight +=20;
_btn.bounds = btnBounds;
通过bounds放大是以自己的中心点为原点,而frame是以自己左上角的为原点进行缩放的。