UIView的动画相关API

2016-12-05  本文已影响19人  thinkq

只记录自己不熟悉的,具体见View Programming Guide for iOS - Animations篇

视图属性动画

Tables Are
setAnimationStartDate: setAnimationDelay: 使用这两个方法指明什么时候开始执行,如果指明的开始时间是过去的则动画立即执行
setAnimationDuration: 使用这个方法指明动画时间
setAnimationCurve: 使用此方法设置动画的时间曲线。 这控制动画是在某些时间线性执行还是改变速度。
setAnimationRepeatCount: setAnimationRepeatAutoreverses: 使用这些方法设置动画重复次数以及在动画结束时是否反转
setAnimationDelegate: setAnimationWillStartSelect: setAnimationDidStopSelect: 使用这些方法设置代理以及在动画开始前和结束后执行相应代码
setAnimationBeginFromCurrentState: 使用这个方法去立即结束所有之前的动画并且在当前状态下开始新的动画。如果你将参数传为NO则新的动画将在之前的动画结束之后再执行

动画代理方法:

// This method begins the first animation.

- (IBAction)showHideView:(id)sender

{
    [UIView beginAnimations:@"ShowHideView" context:nil];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

    [UIView setAnimationDuration:1.0];

    [UIView setAnimationDelegate:self];

    [UIView setAnimationDidStopSelector:@selector(showHideDidStop:finished:context:)];

    // Make the animatable changes.

    thirdView.alpha = 0.0;
    // Commit the changes and perform the animation.

    [UIView commitAnimations];

}

// Called at the end of the preceding animation.
- (void)showHideDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

{
    [UIView beginAnimations:@"ShowHideView2" context:nil];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    [UIView setAnimationDuration:1.0];

    [UIView setAnimationDelay:1.0];

    thirdView.alpha = 1.0;

    [UIView commitAnimations];
}

动画代理方法应该如下所示:

- (void)animationWillStart:(NSString *)animationID context:(void *)context;
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;

animationID和context是在动画块开始方法中beginAnimations:context: 传递的参数,

创建视图过渡动画

视图过渡动画可以隐藏在视图层级中和添加,删除,隐藏和展示的相关的突然改变。可以通过使用视图过渡动画实现以下改变:

改变子视图(iOS 4以后鼓励使用):

- (IBAction)displayNewPage:(id)sender
{
    [UIView transitionWithView:self.view
        duration:1.0
        options:UIViewAnimationOptionTransitionCurlUp
        animations:^{
            currentTextView.hidden = YES;
            swapTextView.hidden = NO;
        }
        completion:^(BOOL finished){
            // Save the old text and then swap the views.
            [self saveNotes:temp];
            UIView*    temp = currentTextView;
            currentTextView = swapTextView;
            swapTextView = temp;
        }];
}

使用begin/commit方法改变子视图

    [UIView beginAnimations:@"ToggleSiblings" context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [UIView setAnimationDuration:1.0];

    // Make your changes 

    [UIView commitAnimations];

替换视图

- (IBAction)toggleMainViews:(id)sender {

    [UIView transitionFromView:(displayingPrimary ? primaryView : secondaryView)
        toView:(displayingPrimary ? secondaryView : primaryView)
        duration:1.0
        options:(displayingPrimary ? UIViewAnimationOptionTransitionFlipFromRight :

                    UIViewAnimationOptionTransitionFlipFromLeft)
        completion:^(BOOL finished) {
            if (finished) {
                displayingPrimary = !displayingPrimary;
            }
    }];
}
上一篇下一篇

猜你喜欢

热点阅读