快速連結

2013年7月10日

讓你的View動起來──新版本

在4.0之前,讓View動作的語法如下:

    UIButton *button = [[UIButton alloc] init];
    CGRect frame = button.frame;
    frame.origin.x = 500; // new x coordinate
    frame.origin.y = 500; // new y coordinate

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration: 0.25];
    button.frame = frame;
    button.alpha = 0;
    [UIView commitAnimations];

新版的語法有三句:

[UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#>];

[UIView animateWithDuration:<#(NSTimeInterval)#>
                     animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>]; 

[UIView animateWithDuration:<#(NSTimeInterval)#> delay:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>];

duration是動畫時間長度。
delay是延遲時間。
animations是動作要做些甚麼,具體要怎麼作等等看範例!
completion是結束後的動作,也可以繼續做動畫或是呼叫function。
options是選項設定,可以去官網列出的列表。

範例:

  UIButton *button = [[UIButton alloc] init];
  CGRect frame = button.frame;
  frame.origin.x = 500; // new x coordinate
  frame.origin.y = 500; // new y coordinate
  [UIView animateWithDuration:0.5
     delay: 1.0
     options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseInOut)
     animations:^(void){
        button.frame = frame;
        button.alpha = 0;
     }completion:^(BOOL finished){
         [button removeFromSuperview];
     }
  ];

沒有留言:

張貼留言

歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT