2012年6月3日 星期日

UIView基本瞭解 (一)

UIView 
只有一個superview  -->  (UIView *)superview
有許多subviews --> (NSArray *)subviews

UIWindow 
繼承自UIView
一個IOS Application只有一個UIWindow

UIView的視覺座標變數(起始座標0,0定義在左上方)
1. CGFloat 只是普通的float,為了方便使用在圖型運算上使用
2. CGPoint 使用兩個CGFloat來定義X,Y位置
   CGPoint p = CGPointMake(34.5, 22.0);
   p.x += 20;  // move right by 20 points
   p.y += 10;  // 向下移動10 points
3. CGSize 使用兩個CGFloat來定義長寬
   CGSize s = CGSizeMake(100.0, 200.0);
   s.height += 50;  // make the size 50 points taller 
   s.width +=20;  // 寬度放大20 points
4. CGRect  使用一個CGPoint及一個CGSize來定義圖型的位置與大小
   CGRect aRect = CGRectMake(45.0, 75.5, 300, 500);
   aRect.size.height += 45;// make the rectangle 45 points taller
   aRect.origin.x += 30; // 物件向右移動30 points


UIView 重要的 property  //The geometry of a view is defined by  these properties
@property
bounds   //  your view’s internal drawing space’s origin and size
@property center   //The center of the view in your superview’s coordinate space
@property frame  // a rectangle in your superview’s coordinate   
        space which entirely contains your view’s bounds.size

啟動繪圖例
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    return self;
}

//做一個自訂的畫圓的method
- (void)drawCircleAtPoint:(CGPoint)p withRadius:(CGFloat)radius inContext:(CGContextRef)context
{
    UIGraphicsPushContext(context);
    CGContextBeginPath(context);
    CGContextAddArc(context, p.x, p.y, radius, 0, 2*M_PI, YES); // 360 degree (0 to 2pi) arc
    CGContextStrokePath(context);
    UIGraphicsPopContext();
}

- (void)drawRect:(CGRect)rect
{
//取得現在圖層的資料
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    CGPoint midPoint; // center of our bounds in our coordinate system
    midPoint.x = self.bounds.origin.x + self.bounds.size.width/2;
    midPoint.y = self.bounds.origin.y + self.bounds.size.height/2;
   
    CGFloat size = self.bounds.size.width / 2;
    if (self.bounds.size.height < self.bounds.size.width) size = self.bounds.size.height / 2;
    size *= self.scale; // scale is percentage of full view size
   
    CGContextSetLineWidth(context, 5.0);
    [[UIColor blueColor] setStroke];
   
    [self drawCircleAtPoint:midPoint withRadius:size inContext:context]; // head

}


其中

1. - (id)initWithFrame:(CGRect)aRect
Parameters
aRect
The frame rectangle for the view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This method uses the frame rectangle to set thecenter and bounds properties accordingly.

2. CGContextRef UIGraphicsGetCurrentContext ( void );
Returns the current graphics context.


3. void UIGraphicsPushContext ( CGContextRef context );
 You can use this function to save the previous graphics state and make the specified context the current context.
 
4.  void UIGraphicsPopContext ( void ); 
配合UIGraphicsPushContext 

5. void CGContextBeginPath ( CGContextRef c );
 A graphics context can have only a single path in use at any time. If the specified context already contains a current path when you call this function, Quartz discards the old path and any data associated with it.
 
6.  void CGContextStrokePath ( CGContextRef c);
Quartz uses the line width and stroke color of the graphics state to paint the path. As a side effect when you call this function, Quartz clears the current path.
 
7. void CGContextAddArc (
   CGContextRef c,
   CGFloat x,
   CGFloat y,
   CGFloat radius,
   CGFloat startAngle,
   CGFloat endAngle,
   int clockwise
);
Adds an arc of a circle to the current path, possibly preceded by a straight line segment
Parameters
c
A graphics context.
x
The x-value, in user space coordinates, for the center of the arc.
y
The y-value, in user space coordinates, for the center of the arc.
radius
The radius of the arc, in user space coordinates.
startAngle
The angle to the starting point of the arc, measured in radians from the positive x-axis.
endAngle
The angle to the end point of the arc, measured in radians from the positive x-axis.
clockwise
Specify 1 to create a clockwise arc or 0 to create a counterclockwise arc.
 
 
View B’s bounds = ((0,0),(200,250))
View B’s frame = ((140,65),(320,320))
View B’s center = (300,225)


 
  











































沒有留言: