iOS Dev (28) 三種基本的 2D 放射變換 – iPhone手機開發技術文章 iPhone軟體開發教學課程

iOS Dev (28) 三種基本的 2D 放射變換

  • 基礎

    我們要用到的是:

    CGAffineTransform
    

    的一些函數(是 C 形式的函數)。

    縮放 scale 的定義:

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

    旋轉 roate 的定義:

    CGAffineTransform CGAffineTransformRotate (
       CGAffineTransform t,
       CGFloat angle
    );
    

    平移 translate 的定義:

    CGAffineTransform CGAffineTransformTranslate (
       CGAffineTransform t,
       CGFloat tx,
       CGFloat ty
    );
    

    示例

    建立一個 Empty Project,直接該丫的 application 方法。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(60, 100, 200, 200)];
        view1.tag = 101;
        view1.backgroundColor = [UIColor redColor];
        [self.window addSubview:view1];
    
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
        view2.tag = 102;
        view2.backgroundColor = [UIColor yellowColor];
        [view1 addSubview:view2];
    
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(110, 400, 100, 44)];
        btn.backgroundColor = [UIColor cyanColor];
        [btn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
        [self.window addSubview:btn];
    
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    - (void)onClick
    {
        UIView *view2 = [[self.window viewWithTag:101] viewWithTag:102];
        view2.transform = CGAffineTransformScale(view2.transform, 0.8, 0.8);
        view2.transform = CGAffineTransformRotate(view2.transform, 0.2);
        view2.transform = CGAffineTransformTranslate(view2.transform, 50, 50);
    }
    

    轉載請註明來自:https://blog.csdn.net/prevention

發佈留言