http://www.cnblogs.com/XYQ-208910/p/4761646.html
一蠢棱、UIView常見屬性
(1)@property(nonatomic,readonly)UIView *superview; //獲取自己的父控件對象
(2)@property(nonatomic,readonly,copy)UIView *subviews;//獲取自己的所有子控件對象
(3)@property(nonatomic)NSInteger tag;//控件的ID標(biāo)識,父控件可以通過tag來找到對應(yīng)的子控件,默認(rèn)為0
(4)@property(nonatomic) CGRect frame;//控件所在矩形框的位置和尺寸(以父控件的左上角為坐標(biāo)原點)
(5)@property(nonatomic) CGRect bounds;//控件所在矩形框的位置和尺寸(以自己左上角為坐標(biāo)原點,所以bounds的x\y永遠(yuǎn)為0)
(6)@property(nonatomic) CGPoint center;//控件中點的位置(以父控件的左上角為坐標(biāo)原點)
(7)@property(nonatomic) CGAffineTransform transform;//控件的形變屬性(可以設(shè)置旋轉(zhuǎn)角度甩栈、比例縮放泻仙、平移等屬性)
a.旋轉(zhuǎn):CGAffineTransform?CGAffineTransformScale(CGAffineTransform t,??CGFloat sx, CGFloat sy)
sx和sy為坐標(biāo)放縮倍數(shù)
b.放縮:CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,CGFloat angle)
angle:旋轉(zhuǎn)度數(shù)
c.反轉(zhuǎn):CGAffineTransform CGAffineTransformInvert(CGAffineTransform t)
d. 兩個矩陣仿射為一個新的矩陣:
CGAffineTransformCGAffineTransformConcat(CGAffineTransform?t1,CGAffineTransform t2)
e.判斷兩個矩陣是否相等:
bool?CGAffineTransformEqualToTransform(CGAffineTransform?t1,?CGAffineTransform t2)
創(chuàng)建一個仿射矩陣
CGAffineTransformMake?直接賦值來創(chuàng)建
CGAffineTransformMakeRotation?設(shè)置角度來生成矩陣
結(jié)果就是
CGAffineTransformMakeScale??設(shè)置縮放,及改變a量没、d的值
CGAffineTransformMakeTranslation??設(shè)置偏移
改變已經(jīng)存在的放射矩陣
CGAffineTransformTranslate??原始的基礎(chǔ)上加上偏移
CGAffineTransformScale加上縮放
CGAffineTransformRotate加上旋轉(zhuǎn)
CGAffineTransformInvert?反向的仿射矩陣比如(x玉转,y)通過矩陣t得到了(x',y')那么通過這個函數(shù)生成的t'作用與(x',y')就能得到原始的(x,y)
CGAffineTransformConcat?通過兩個已經(jīng)存在的放射矩陣生成一個新的矩陣t' =?t1 *?t2
應(yīng)用仿射矩陣
CGPointApplyAffineTransform?得到新的點
CGSizeApplyAffineTransform??得到新的size
CGRectApplyAffineTransform??得到新的rect
視圖view的組件圖框frame的類型為CGRect結(jié)構(gòu)體
struct CGRect
{
CGPoint origin;
CGSize size;
};
struct CGPonit
{
CGFloat x;
CGFloat y;
};
struct CGSize
{
CGFloat width;
CGFloat height;
};
視圖view的組件形變控件transform的類型為CGAffineTransform結(jié)構(gòu)體,就是一個矩陣(可以伸縮、平移殴蹄、旋轉(zhuǎn)等)
struct CGAffineTransform {
CGFloat a, b, c, d;
CGFloat tx, ty;
};
二究抓、UIButton的初始化
–最普通的初始化方法
UIButton *btn = [[UIButton alloc] initWithFrame:rect];
–快速初始化
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
–type參數(shù)用來指定按鈕的類型,一共有6種選擇:
?UIButtonTypeCustom:無類型袭灯,按鈕的內(nèi)容需要自定義
?UIButtonTypeRoundedRect:圓矩形邊框
?UIButtonTypeDetailDisclosure:
?UIButtonTypeInfoLight:
?UIButtonTypeInfoDark:
?UIButtonTypeContactAdd:
1#import"ViewController.h"
2typedefenum
3{
4buttonTypeLeft=1,
5buttonTypeRight,
6buttonTypeUp,7buttonTypeDown,8buttonTypeRotateL,9buttonTypeRotateR,10buttonTypeScaleUp,11buttonTypeScaleDown,1213}buttonType;14@interfaceViewController ()
15@property (weak, nonatomic) IBOutlet UIView *viewMan;
1617@end
1819@implementationViewController
20//移動方向
21- (IBAction)buttonDirectionClicked:(UIButton *)sender22{23//父視圖的長度和高度24NSInteger screenWidth =self.view.frame.size.width;25NSInteger screenHeight =self.view.frame.size.height;2627//子視圖28CGRect rect =self.viewMan.frame;29//每次移動距離30NSInteger offset =5;31//父控件通過tag標(biāo)識獲取視圖中子控件(組件)32switch(sender.tag)33{34casebuttonTypeDown:35rect.origin.y +=offset;36if(rect.origin.y >=screenHeight)37{38rect.origin.y = -rect.size.height;39}40break;41casebuttonTypeUp:42rect.origin.y -=offset;43if(rect.origin.y <= -rect.size.height)44{45rect.origin.y =screenHeight;46}47break;48casebuttonTypeLeft:49rect.origin.x -=offset;50if(rect.origin.x <= -rect.size.width)51{52rect.origin.x =screenWidth;53}54break;55casebuttonTypeRight:56rect.origin.x +=offset;57if(rect.origin.x >=screenWidth)58{59rect.origin.x = -rect.size.width;60}61break;62}63self.viewMan.frame =rect;64}6566//旋轉(zhuǎn)67- (IBAction)buttonRotateClicked:(UIButton *)sender68{69//子視圖這個控件的坐標(biāo)矩陣70CGAffineTransform form =self.viewMan.transform;71switch(sender.tag) {72casebuttonTypeRotateL:73form = CGAffineTransformRotate(form, -M_2_PI/2);
74break;
75casebuttonTypeRotateR:
76form = CGAffineTransformRotate(form, M_2_PI/2);77break;78}
79self.viewMan.transform =form;80}
8182//放縮
83- (IBAction)buttonScaleClicked:(UIButton *)sender
84{85//子視圖這個控件的坐標(biāo)矩陣
86CGAffineTransform form =self.viewMan.transform;
87CGFloat scaleFator =0.0f;
8889switch(sender.tag) {
90casebuttonTypeScaleUp刺下;
91scaleFator =1.2;
92break;
93casebuttonTypeScaleDown:
94scaleFator =0.8;
95break;
96}
97form =CGAffineTransformScale(form, scaleFator, scaleFator);
98self.viewMan.transform =form;
99}
100
101- (void)viewDidLoad {
102[super viewDidLoad];
103//Do any additional setup after loading the view, typically from a nib.
104}
105
106- (void)didReceiveMemoryWarning {
107[super didReceiveMemoryWarning];
108//Dispose of any resources that can be recreated.
109}
110
111@end