剪辑视图实例学习

【实例学习】
给出的图片是地球仪,正方形的图。
如果不剪辑,就是以正方视图出现。
剪辑后效果,就是以圆形视图出现。

涉及到主要代码如下:
- (void) drawRect: (CGRect) aRect
{
CGRect bounds
= CGRectMake(0.0f, 0.0f, SIDELENGTH, SIDELENGTH);

// Create a new path
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path
= CGPathCreateMutable();

// Add circle to path
CGPathAddEllipseInRect(path, NULL, bounds);//这句话就是剪辑作用
CGContextAddPath(context, path);

// Clip to the circle and draw the logo
CGContextClip(context);
[LOGO drawInRect:bounds];
CFRelease(path);
}

// Detect whether the touch "hits" the view
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGPoint pt;
float HALFSIDE = SIDELENGTH / 2.0f;

// normalize with centered origin
pt.x = (point.x - HALFSIDE) / HALFSIDE;
pt.y
= (point.y - HALFSIDE) / HALFSIDE;

// x^2 + y^2 = radius
float xsquared = pt.x * pt.x;
float ysquared = pt.y * pt.y;

// If the radius < 1, the point is within the clipped circle
if ((xsquared + ysquared) < 1.0) return YES;
return NO;
}


先看下各个结构和类
CGContextRef结构
    typedef struct CGContext * CGContextRef;//这里可以看出CGContext是一个结构体

    An opaque type that represents a Quartz 2D drawing environment.

    CGContext主要用于2D绘图环境,上下文数据结构;

普通方法:
CGContextAddPath
    void CGContextAddPath ( CGContextRef context, CGPathRef path );

    Adds a previously created Quartz path object to the current path in a graphics context.

CGContextClip
    void CGContextClip ( CGContextRef c );

    Modifies the current clipping path, using the nonzero winding number rule.

UIKit函数参考
    The UIKit framework defines a number of functions, many of them used in graphics and drawing operations.

    UIKit框架定义大量的函数,大部分函数都用于图像和绘画操作。

普通方法:
UIGraphicsGetCurrentContext
    Returns the current graphics context.

CGMutablePathRef结构
    typedef struct CGPath *CGMutablePathRef;

    An opaque type that represents a mutable graphics path.

CGPath
    A graphics path is a mathematical description of a series of shapes or lines.

    CGPathRef defines an opaque type that represents an immutable graphics path. CGMutablePathRef defines an opaque type that represents a mutable graphics path. Neither CGPathRef nor CGMutablePathRef define functions to draw a path. To draw a Quartz path to a graphics context, you add the path to the graphics context by calling CGContextAddPath and then call one of the context’s drawing functions—see CGContext Reference.

    头文件有这么句话:
    typedef struct CGPath *CGMutablePathRef;
    typedef const struct CGPath *CGPathRef;

    这里看得出CGPath也是结构体

普通方法:
CGPathCreateMutable
    Creates a mutable graphics path.

    Return Value
    A new mutable path. You are responsible for releasing this object.

    需要负责释放返回的资源。如代码:
    CGMutablePathRef path = CGPathCreateMutable();
    。。。。。。
    CFRelease(path);

CGPathAddEllipseInRect
    Adds to a path an ellipse that fits inside a rectangle.

    void CGPathAddEllipseInRect (
    CGMutablePathRef path,
    const CGAffineTransform *m,
    CGRect rect
    );
    Parameters
    path
    The path to modify.
    m
    An affine transform to apply to the ellipse, or NULL if you don’t want to transform the ellipse.
    rect
    A rectangle to enclose the ellipse.

CFType实际上使用typedef重新声明的

    看这个头文件描述,CFBase.h
    typedef const void * CFTypeRef;

所以此文件下的函数属于普通函数
CFRelease
void CFRelease ( CFTypeRef cf );

Releases a Core Foundation object.
cf: A CFType object to release.

/******/

备注:
方法-成员方法
普通方法-类似C语言函数
类方法-类似C++类函数

/**************************/

例子有这么一段代码,loadView函数中:
    // Add a randomize button
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                               initWithTitle:@"Randomize"
                                               style:UIBarButtonItemStylePlain
                                               target:self
                                               action:@selector(randomize)] autorelease];

本例学习中,仍对图形的操作很迷糊,也许是自己对图形有一定的偏移思维。但不阻碍我对其调用的理解。

这里有两点:
1、阅读资料,首先分清楚这是什么类型的函数。可以阅读部分头文件来看,直接从sdk资料分析有什么很迷惑。
2、上面loadView的部分代码很适用,至少一种方式来展示导航功能。

再看段代码:
// These calls which extend UIView are partly not needed any more in
// recent versions of the SDK, which is why parts are commented out
@interface UIView (Extended)
-(void) setOrigin: (CGPoint) aPoint;
@end

@interface DragClipView : UIView
{
    CGPoint startLocation;
}
@end

至于为什么@interface UIView (Extended)以及setOrigin暂时未知原因,后续学习中解答
解答在此,http://www.cnblogs.com/GoGoagg/archive/2011/05/25/2056676.html
posted @ 2011-05-20 14:34  西就东城  阅读(1887)  评论(1编辑  收藏  举报