2014年8月18日 星期一

用UIView animateWithDuration來做出二個Frame之間的連續動作

這個類別方法你可以把他想像成是「展示二個frame設定之間的連續動作」,因此,你只能一次設定一個frame之間的轉變,如果有多的,它也只會做最後的一步,要做多重的轉變動畫,就要在completion中多次呼叫這個方法,我的範例如下:

-(void) _setScaleWithView:(UIView *)view alpha:(float) alpha xScale:(float) xScale yScale:(float) yScale{
    
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width + xScale , view.frame.size.height + yScale);
    view.alpha = alpha;
}

-(void) _scalingView:(UIView *)view{
    CGRect rect = view.frame;
    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:(void (^)(void)) ^{
                         [self _setScaleWithView:view alpha:0.5 xScale:-2 yScale:-2];
                     }
                     completion:^(BOOL finished){
                         [UIView animateWithDuration:0.2 animations:^{
                             [self _setScaleWithView:view alpha:1.0 xScale:2*2 yScale:2*2];
                         } completion:^(BOOL finished) {
                             [UIView animateWithDuration:0.5 animations:^{
                                 [self _setScaleWithView:view alpha:0.5 xScale:-2*2 yScale:-2*2];
                                 } completion:^(BOOL finished) {
                                 [UIView animateWithDuration:0.2 animations:^{
                                     [self _setScaleWithView:view alpha:1.0 xScale:2*2 yScale:2*2];
                                 } completion:^(BOOL finished) {
                                     [UIView animateWithDuration:0.5 animations:^{
                                         [self _setScaleWithView:view alpha:0.5 xScale:-2*2 yScale:-2*2];
                                     } completion:^(BOOL finished) {
                                         [UIView animateWithDuration:0.2 animations:^{
                                             [self _setScaleWithView:view alpha:1.0 xScale:2 yScale:2];
                                         } completion:^(BOOL finished) {
                                             view.frame = rect;
                                             view.alpha = 1;
                                         }];
                                     }];
                                 }];
                                 
                             }];
                         }];
                     }];

}

沒有留言:

張貼留言