2015年11月17日 星期二

iOS筆記:Creating Views Programatically

數據類型:

CGFloat: 浮點值的基本類型
CGPoint: 表示一個二維坐標系中的點
CGSize: 表示一個矩形的寬度和高度
CGRect: 表示一個矩形的位置和大小

typedef float CGFloat;// 32-bit
typedef double CGFloat;// 64-bit

struct CGPoint {
    CGFloat x;
    CGFloat y;
};
typedef struct CGPoint CGPoint;

struct CGSize {
    CGFloat width;
    CGFloat height;
};
typedef struct CGSize CGSize;

struct CGRect {
    CGPoint origin;
    CGSize size;
};
typedef struct CGRect CGRect;

Views Frame and Bounds

frame:描述當前視圖在其父視圖中的位置和大小。
bounds:描述當前視圖在其自身坐標系統中的位置和大小。
center:描述當前視圖的中心點在其父視圖中的位置。

enter image description here

ios以左上角爲坐標原點(0,0),以原點向右側爲X軸正方向,原點下側爲Y軸正方向
enter image description here

ios採用CGPoint來表示點在坐標系上X、Y位置。我們可以通過CGPointMake(x,y)來創建一個坐標點:CGPoint point = CGPointMake(80,40).

同時,ios採用CGSize來表示視圖的寬度和高度,即視圖的大小。我們可以通過CGSizeMake(width,height)來創建一個矩形的大小
CGGeometry類定義幾何元素的結構和操作幾何元素的函數。

CGRect則是結合了CGPoint和CGSize,用來表示矩形的位置和大小。它的origin表示矩形右上角所在位置(CGPoint),size表示矩形的大小(CGSize)。

frame和bounds屬性,都是用來描述視圖的大小(CGSize)和位置(CGPoint)的,兩者都用CGRect表示。不同的是,frame描述的是在其父視圖中的CGRect,而bounds描述的是在其自身視圖中的CGRect,也就是說,兩者所在的坐標系是不同的。如上圖所示,View B是View A的子視圖,那麽,View B的frame屬性爲origin(200,100),size(200,250),而View B的bounds屬性爲origin(0,0),size(200,250)。

frame和bounds是UIView中的兩個屬性(property)。

frame指的是:該view在父view坐標系統中的位置和大小。(參照點是父親的坐標系統)
bounds指的是:該view在本身坐標系統中 的位置和大小。(參照點是本身坐標系統)


Adding a UIView programmatically

ex.

CGRect myViewsFram = CGRectMake(20, 250, 200, 60);
UIView *myView = [[UIView alloc] initWithFrame:myViewsFram];
myView.backgroundColor = [UIColor redColor];
[self.view addSubview:myView];

UIButton *anotherButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    anotherButton.frame = CGRectMake(40, 40, 200, 100);
    anotherButton.backgroundColor = [UIColor greenColor];
    [anotherButton setTitle:@"Press ME" forState:UIControlStateNormal];
    [self.view addSubview:anotherButton];

    //[myView removeFromSuperview];

Set up Target Action

- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    [anotherButton addTarget:self action:@selector(didPressButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didPressButton:(UIButton *)button
{
    NSLog(@"this is the test form Target/Action");
}

範例:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.mylabel = [[UILabel alloc] init];
    self.mylabel.frame = CGRectMake(20, 60, 280, 22);
    [self.view addSubview:self.mylabel];

    self.mylabel.textColor = [UIColor blueColor];
    self.mylabel.text = @"Placeholder";

    self.myTextfield = [[UITextField alloc] init];
    self.myTextfield.frame = CGRectMake(20 , 110, 280, 30);
    self.myTextfield.placeholder = @"Type text here";
    self.myTextfield.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.myTextfield];

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [myButton addTarget:self action:@selector(myButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [myButton setTitle:@"Press me!" forState:UIControlStateNormal];
    myButton.frame = CGRectMake(20, 160, 200, 30);
    [self.view addSubview:myButton];

}

- (void)myButtonPressed: (UIButton *)sender
{
    self.mylabel.text = self.myTextfield.text;
    [self.myTextfield resignFirstResponder];
}