旋转视图例子学习

旋转效果,你想想在图片浏览的时候,照的时候是横起的,看肯定要旋转下才能更合适观看

这个例子实现的旋转不是这样,但类似。

仿射转换支持更改某个对象的几何特征,方法是将该对象从一个视图坐标系映射到另一个视图坐标系。
iPhoneSDK完全支持标准的仿射2D转换,可以缩放、转换、旋转和倾斜视图。

单词:Affine,adj.仿射的,几何学的,姻亲的;n.姻亲

例子中使用了一个Timer来定时旋转。

NSTimer类

类方法:
    + scheduledTimerWithTimeInterval:invocation:repeats:
    + scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
    + timerWithTimeInterval:invocation:repeats:
    + timerWithTimeInterval:target:selector:userInfo:repeats:

代码中使用+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

看看Timer做了什么
- (void) handleTimer: (NSTimer *) timer
{
// Rotate each iteration by 1% of PI
float angle = theta * (PI / 100);
CGAffineTransform transform
= CGAffineTransformMakeRotation(angle);
theta
= (theta + 1) % 200;

// For fun, scale by the absolute value of the cosine
float degree = cos(angle);
if (degree < 0.0) degree *= -1.0f;
degree
+= 0.5f;
CGAffineTransform scaled
= CGAffineTransformScale(transform, degree, degree);

// Apply the affine transform
[[self.view viewWithTag:ROTATE_VIEW_TAG] setTransform:scaled];
}


CGAffineTransform结构

    struct CGAffineTransform {
    CGFloat a;
    CGFloat b;
    CGFloat c;
    CGFloat d;
    CGFloat tx;
    CGFloat ty; };
    typedef struct CGAffineTransform CGAffineTransform;

    A structure for holding an affine transformation matrix.

普通方法:
CGAffineTransformMakeRotation
    CGAffineTransform CGAffineTransformMakeRotation ( CGFloat angle );

    Returns an affine transformation matrix constructed from a rotation value you provide.

    将角度值转为几何结构值

CGAffineTransformScale
    CGAffineTransform CGAffineTransformScale ( CGAffineTransform t, CGFloat sx, CGFloat sy );

    Returns an affine transformation matrix constructed by scaling an existing affine transform.

UIView类

属性:
transform
    Specifies the transform applied to the receiver, relative to the center of its bounds.

    @property(nonatomic) CGAffineTransform transform
   
好了,主要实现就是这样的。

注意一下代码的使用:
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
HelloController
*hello = [[HelloController alloc] init];
UINavigationController
*nav = [[UINavigationController alloc] initWithRootViewController:hello];
[window addSubview:nav.view];
[window makeKeyAndVisible];


其中UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:hello];就意味着我可用普通试图控制器来编写其视图逻辑,其导航逻辑应该由Nav来控制,后续实践此想法。

posted @ 2011-05-20 14:58  西就东城  阅读(1456)  评论(0编辑  收藏  举报