2013年4月22日 星期一

UIView Animation

資料來源

Using in pre-iOS4
//@property (nonatomic,weak) IBOutlet UIImageView *basketTop;
    //@property (nonatomic,weak) IBOutlet UIImageView *basketBottom;  
    CGRect basketTopFrame = self.basketTop.frame;
    basketTopFrame.origin.y = -basketTopFrame.size.height;
 
    CGRect basketBottomFrame = self.basketBottom.frame;
    basketBottomFrame.origin.y = self.view.bounds.size.height;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
 
    self.basketTop.frame = basketTopFrame;
    self.basketBottom.frame = basketBottomFrame;
    [UIView commitAnimations];

Alternate method for iOS 4+
[UIView animateWithDuration:0.5
                          delay:1.0
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.basketTop.frame = basketTopFrame;
                         self.basketBottom.frame = basketBottomFrame;
                     }
                     completion:^(BOOL finished){
                         NSLog(@"Done!");
                     }];

附件是我把教學來源網站範例操作一遍
Touch Event有別於touchesBegan ,改用UITapGestureRecognizer
增加了幾個教學末端建議的練習上去(點擊開啓Backet, 蟲子消失之後關閉Basket)

圖片以及音效檔案請自行去教學網站下載, 不然檔案肥大

幾個在try的時候試出來值得提的東西

UITapGestureRecognizer
  • 一個View可以有多個Gesture, 但一個Gesture只對應一個View
UIViewAnimation
  • 無需設定UIViewAnimationOptionAllowUserInteraction, 也沒這選項給你設定
  • 若要使用AnimationDidStopSelector, 需要添加delegate
    UIViewAnimationWithBlocks
    • 如果options不添加UIViewAnimationOptionAllowUserInteraction, 在跑動畫時點擊bug會沒反應
    • 記得要對[self.bug setUserInteractionEnabled:YES] ,或是直接去IB設定Bug的屬性把User Interaction Enable勾選

    沒有留言:

    張貼留言