IOS動畫分為屬性動畫和過渡動畫。ios4.0之前
內容和設置主要放在方括號中既:如下
[UIView beginAnimations:@move context:@aa];
中間部分設置動畫內容和屬性
[UIView commitAnimations];
詳見代碼如下
[UIView beginAnimations:@move context:@aa]; [UIView setAnimationDuration:1];//設置動畫時間 CGPoint point = self.View1.center;// [UIView setAnimationDelegate:self];//設置代理 [UIView setAnimationWillStartSelector:@selector(startAnimation:)];//動畫開始之前 [UIView setAnimationDidStopSelector:@selector(stopAnition:)];//動畫結束之後 // [UIView setAnimationRepeatAutoreverses:YES]; self.View1.center = CGPointMake(point.x+1, point.y); // self.View1.backgroundColor = [UIColor orangeColor]; [UIView commitAnimations];
The following properties of the UIView class are animatable:
@property frame
@property bounds
@property center
@property transform
@property alpha
@property backgroundColor
@property contentStretch
[UIView beginAnimations:@move context:@text]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.View3 cache:YES]; [UIView commitAnimations];
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
Animating Views with Block Objects
//屬性動畫
+ animateWithDuration:delay:options:animations:completion:
+ animateWithDuration:animations:completion:
+ animateWithDuration:animations:
//過渡動畫
+ transitionWithView:duration:options:animations:completion:
+ transitionFromView:toView:duration:options:completion:
//關鍵幀動畫
+ animateKeyframesWithDuration:delay:options:animations:completion:
+ addKeyframeWithRelativeStartTime:relativeDuration:animations:
//系統動畫
+ performSystemAnimation:onViews:options:animations:completion:
//未知,還為測試
+ animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:
+ performWithoutAnimation:
代碼如下:具體過程。
//CABasicAnimation 動畫 CABasicAnimation* baseAnimation = [CABasicAnimation animationWithKeyPath:@center]; baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 0)]; baseAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(400, 100)] ; baseAnimation.duration = 3; [self.View2.layer addAnimation:baseAnimation forKey:@baseAnimation]; // CAKeyframeAnimation 動畫 CAKeyframeAnimation* keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@position]; keyAnimation.values =[NSArray arrayWithObjects: [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5, self.View2.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x-5, self.View2.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5, self.View2.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)], nil]; keyAnimation.keyTimes = @[[NSNumber numberWithFloat:.1], [NSNumber numberWithFloat:.2], [NSNumber numberWithFloat:.3], [NSNumber numberWithFloat:.4], [NSNumber numberWithFloat:.5], [NSNumber numberWithFloat:1]];//設置時間 ,百分比 keyAnimation.calculationMode = kCAAnimationDiscrete; [self.View2.layer addAnimation:keyAnimation forKey:@position]; //CATransition 動畫 CATransition* transiton = [CATransition animation]; transiton.startProgress = 0; transiton.endProgress = 1.0; transiton.duration = 3; transiton.type = kCATransitionReveal;//根據不同值顯示不同動畫 transiton.subtype = kCATransitionFromRight; [self.view.layer addAnimation:transiton forKey:@transtion];