1. 程式人生 > >IOS開發(90)之動畫檢視的旋轉

IOS開發(90)之動畫檢視的旋轉

1 前言

今天我們來學習如何建立一個旋轉仿射變換並使用 UIView 類的動畫方法來執行旋轉動作。

2 程式碼例項

ZYViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage *xcodeImage = [UIImage imageNamed:@"Xcode.png"];
    self.xcodeImageView = [[UIImageView alloc] initWithImage:xcodeImage];
    //設定圖片的Frame
    [self.xcodeImageView setFrame:CGRectMake(0.0f,0.0f, 100.0f, 100.0f)];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.xcodeImageView];
}

- (void) viewDidAppear:(BOOL)paramAnimated{ [super viewDidAppear:paramAnimated];
    self.xcodeImageView.center = self.view.center;
    /* Begin the animation */
    [UIView beginAnimations:@"clockwiseAnimation" context:NULL];
    /* Make the animation 5 seconds long */
    [UIView setAnimationDuration:5.0f];
    [UIView setAnimationDelegate:self];
    //停止動畫時候呼叫clockwiseRotationStopped方法
    [UIView setAnimationDidStopSelector:@selector(clockwiseRotationStopped:finished:context:)];
    //順時針旋轉90度
    self.xcodeImageView.transform = CGAffineTransformMakeRotation((90.0f * M_PI) / 180.0f);
    /* Commit the animation */
    [UIView commitAnimations];
}

- (void)clockwiseRotationStopped:(NSString *)paramAnimationID finished:(NSNumber *)paramFinished
                         context:(void *)paramContext{
    [UIView beginAnimations:@"counterclockwiseAnimation"context:NULL];
    /* 5 seconds long */
    [UIView setAnimationDuration:5.0f];
    /* 回到原始旋轉 */
    self.xcodeImageView.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
}

執行結果


旋轉90度


復原


3 結語

以上是所有內容,希望對大家有所幫助。