2013-02-06 46 views
31

如何使這個複雜的動畫重複和自動反向? 有沒有什麼辦法可以添加選項UIViewAnimationOptionAutoreverse | UIViewAnimationOption重複到這個動畫序列?如何使UIView動畫序列重複和自動反向

[UIView animateWithDuration:1.0f animations:^{ 

     someView.frame = someFrame1; 

    } completion:^(BOOL finished) { 

     [UIView animateWithDuration:0.5f animations:^{ 

      someView.frame = someFrame2; 

     } completion:nil]; 

    }]; 
+0

是否要使用keyframeanimation?關鍵幀動畫具有自動反向和自動重複的屬性。 – Exploring

回答

99

從點1動畫2到3比2比1和重複,你可以使用animateKeyframesWithDuration中的iOS 7及更高版本:

someView.frame = frame1; 
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{ 
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{ 
     someView.frame = frame2; 
    }]; 
    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{ 
     someView.frame = frame3; 
    }]; 
} completion:nil]; 

如果使用自動佈局,您可以動畫約束常數的變化:

[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{ 
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{ 
     topConstraint.constant = 200; 
     leftConstraint.constant = 200; 
     [self.view layoutIfNeeded]; 
    }]; 
    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{ 
     topConstraint.constant = 100; 
     leftConstraint.constant = 300; 
     [self.view layoutIfNeeded]; 
    }]; 
} completion:nil]; 

或者,具有自動佈局方法是停用的約束,然後你可以使用動畫值frame或者你有什麼。


在早期版本的iOS,您可以使用CAKeyframeAnimation,例如沿路徑動畫:

UIBezierPath *path = [UIBezierPath bezierPath]; 
[path moveToPoint:CGPointMake(100.0, 100.0)]; 
[path addLineToPoint:CGPointMake(200.0, 200.0)]; 
[path addLineToPoint:CGPointMake(100.0, 300.0)]; 

CAKeyframeAnimation *animatePosition = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
animatePosition.path = [path CGPath]; 
animatePosition.duration = 1.0; 
animatePosition.autoreverses = YES; 
animatePosition.repeatCount = HUGE_VALF; 
[self.someView.layer addAnimation:animatePosition forKey:@"position"]; 

你可以用你想要但是多點做到這一點。如果您想沿着彎曲路徑(例如圓形或貝塞爾曲線)進行動畫製作,這也是一項有用的技巧。


,只留下兩個點之間的動畫,你可以使用animateWithDuration:delay:options:animations:completion:,如:

[UIView animateWithDuration:0.5 
         delay:0.0 
        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        // do whatever animation you want, e.g., 

        someView.frame = someFrame1; 
       } 
       completion:NULL]; 

此動畫化從起始幀到someFrame1和背部的運動。

順便說一下,使用UIViewAnimationOptionCurveEaseInOutUIViewAnimationOptionAutoreverseUIViewAnimationOptionRepeat一起使用會爲您帶來更平滑的效果,動畫會反轉並重復。

+0

在問這個問題之前,我嘗試用選項Autoreverse和Repeat從動畫塊的問題中粘貼2個塊。這是行不通的 –

+0

框架不一樣 –

+1

1到2到3到2到1到2到3到2到1到2 ....... –

-1
UIImageView *pin = [UIImageView new]; 
    [pin setFrame:CGRectMake(115, 160, 30, 60)]; 
    [pin setBackgroundColor:[UIColor redColor]]; 
    [holderView addSubview:pin]; 

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat 
        animations:^{ 
         // [pin setTransform:CGAffineTransformMakeTranslation(0, 20)]; 
         [pin setFrame:CGRectMake(115, 200, 60, 100)]; 
        }completion:^(BOOL finished){ 

        }]; 
+0

(插入你自己的動畫) –