從點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
和背部的運動。
順便說一下,使用UIViewAnimationOptionCurveEaseInOut
與UIViewAnimationOptionAutoreverse
和UIViewAnimationOptionRepeat
一起使用會爲您帶來更平滑的效果,動畫會反轉並重復。
來源
2013-02-06 13:49:12
Rob
是否要使用keyframeanimation?關鍵幀動畫具有自動反向和自動重複的屬性。 – Exploring