ios之UIView執行基礎動畫Animation使用參數配置

來源:互聯網
上載者:User

標籤:uiview執行基礎動畫animatio

在ios開發中一般用到的基礎動畫有以下幾種,所有的動畫參數配置大致相同,但是有時候在開發過程中很少這樣配置一般使用代碼塊比較方便,而且代碼也比較簡單以下是常用基礎動畫類型的一個配置

#pragma mark -- Action methods- (void)transitionAnimation// 轉場動畫{    //設定動畫名稱,方便代理方法判斷是哪個動畫    [UIView beginAnimations:@"TransitionAnimation" context:NULL];    //設定動畫時間長度    [UIView setAnimationDuration:3.0];    //設定動畫的變化規律 --有以下4中枚舉值    //    UIViewAnimationCurveEaseInOut,     開始和結束減速    // slow at beginning and end//    UIViewAnimationCurveEaseIn,            // slow at beginning//    UIViewAnimationCurveEaseOut,           // slow at end//    UIViewAnimationCurveLinear           勻速        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    // 對View設定轉場動畫方向 有以下枚舉方向//    typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {//        UIViewAnimationTransitionNone,//        UIViewAnimationTransitionFlipFromLeft,//        UIViewAnimationTransitionFlipFromRight,//        UIViewAnimationTransitionCurlUp,//        UIViewAnimationTransitionCurlDown,//    };    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_view cache:NO];    // 對View設定初始狀態 並對其他進行配置(這裡只是讓View變成之前的參數而已,如果不需要就可以去掉這段代碼)    [_view setTransform:CGAffineTransformIdentity];    [_view setBackgroundColor:[UIColor blackColor]];    [_view setAlpha:1];    [_view setCenter:CGPointMake(50, 50)];    // 設定代理    [UIView setAnimationDelegate:self];    // 動畫結束執行代理方法(這裡走得時代理方法也可以走其他動畫方法就可以形成動畫組)    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];    //動畫結束    [UIView commitAnimations];}- (void)changeAlphaAnimation //改變透明度動畫{    [UIView beginAnimations:@"ChangeAlphaAnimation" context:NULL];    [UIView setAnimationDuration:2.0];    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    [_view setAlpha:0.2];    [UIView setAnimationDelegate:self];    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];    [UIView commitAnimations];}- (void)changeColorAnimation // 改變顏色動畫{    [UIView beginAnimations:@"ChangeColorAnimation" context:NULL];    [UIView setAnimationDuration:2.0];    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    [_view setBackgroundColor:[UIColor redColor]];    [UIView setAnimationDelegate:self];    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];    [UIView commitAnimations];}- (void)rotationAnimation // 旋轉動畫{    [UIView beginAnimations:@"RotationAnimation" context:NULL];    [UIView setAnimationDuration:2.0];    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    [_view setTransform:CGAffineTransformRotate(_view.transform, M_PI_4)];    [UIView setAnimationDelegate:self];    [UIView setAnimationDidStopSelector:@selector(changeColorAnimation)];    [UIView commitAnimations];}- (void)scareAnimation //放大縮小動畫{    [UIView beginAnimations:@"ScareAnimation" context:NULL];    [UIView setAnimationDuration:2.0];    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    [UIView setAnimationDelegate:self];    [_view setTransform:CGAffineTransformScale(_view.transform, 2, 2)];    [UIView setAnimationDidStopSelector:@selector(rotationAnimation)];    [UIView commitAnimations];}- (void)positionAnimation //位移動畫{    [UIView beginAnimations:@"PositionAnition" context:NULL];    [UIView setAnimationDuration:2.0];    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    [UIView setAnimationDelegate:self];    _view.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));    [UIView setAnimationDidStopSelector:@selector(scareAnimation)];    [UIView commitAnimations];}// 代理方法,檢測動畫介紹然後進行其他動作 還有其他兩個方法- (void)animationDidStop:(NSString *)animationId finished:(NSNumber *)finished context:(void *)context{    // 判斷是哪個動畫  然後執行相應操作    if ([animationId isEqualToString:@"ChangeColorAnimation"]) {        [self changeAlphaAnimation];    }    if ([animationId isEqualToString:@"ChangeAlphaAnimation"]) {        [self transitionAnimation];    }}


下面我們來看下代碼塊的用法,代碼塊的話用起來很方便並且可以執行回調,在APP點擊菜動態單切換兩個View或是其他動畫

<pre name="code" class="objc">[UIView animateWithDuration:0.5 animations:^{        [UIView setAnimationDelay:0.8];//配置動畫時延        _currentView.center = CGPointMake(X,Y);//可以對多個view進行我們想要的動畫配置        newView.center = CGPointMake(X,Y);       } completion:^(BOOL finished) {        //執行完後走這裡的代碼塊    }];






ios之UIView執行基礎動畫Animation使用參數配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.