iOS開發中經常使用的動畫方式一共有兩種,UIView動畫效果,還有核心動畫。固然,還有另一種叫作隱式動畫,後續隨手會介紹隱式動畫。這裏主要介紹UIView動畫效果和簡單的核心動畫的使用。c#
1. 直接使用UIView的動畫動畫
[UIView beginAnimations:nil context:nil];url
/*spa
使用set方法設置動畫的屬性代理
*/orm
//設置動畫的時間ip
[UIView setAnimationDuration:2.0];ci
//動畫的代理方法中,還能夠監聽動畫的完成開發
[UIView setAnimationDelegate:self];animation
[UIView setAnimationDidStopSelector:@selector(stop)];
[UIView setAnimationRepeatCount:5];
//動畫效果
self.imageView.transform = CGAffineTransformMakeTranslation(100, 100);
//完成編輯動畫
[UIView commitAnimations];
2. UIView動畫的代碼塊方式
[UIView animateWithDuration:2.0 animations:^{
self.imageView.center = CGPointMake(200, 200);
} completion:^(BOOL finished) {
NSLog(@"動畫完成");
}];
3.UIView自帶專場動畫
[UIView transitionWithView:self.imageView duration:2.0 options:UIViewAnimationOptionTransitionCurlUp animations:^{
self.imageView.image = [UIImage imageNamed:@"5"];
} completion:^(BOOL finished) {
NSLog(@"動畫完成3");
}];
4.核心動畫(核心動畫是添加到圖層上面的動畫效果)
//這是一個組動畫的應用效果
//核心動畫只是一個假象,動畫執行完以後相應的屬性仍是沒變,要想保持動畫以後的屬性,應該在動畫結束後的代碼上設置,有兩種方式,一種是直接在動畫代碼結束後的位置設置,一種是在代理方法中,有一個動畫結束以後的方法
CAAnimationGroup *group = [CAAnimationGroup animation];
//設置代理
// group.delegate = self;
//1.幀動畫
CAKeyframeAnimation *key = [CAKeyframeAnimation animation];
key.keyPath = @"position";
//建立path
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.width));
/*
//若是要現實的話須要將path添加到圖形上下文,和渲染,這裏只須要一個路徑,因此不用渲染
CGContextAddPath(<#CGContextRef context#>, <#CGPathRef path#>);
CGContextStrokePath(<#CGContextRef c#>);
*/
//使用path的缺點:不能指定起始路徑和結束路徑,(2016.1.5)
key.path = path;
//旋轉動畫
CABasicAnimation *basic = [CABasicAnimation animation];
basic.keyPath = @"transform.rotation";
basic.byValue = @(M_PI_4);
self.imageView.image = [UIImage imageNamed:@"5"];
//轉場動畫
CATransition *cirani = [CATransition animation];
//屬性
cirani.type = kCATransitionMoveIn;
cirani.subtype = kCATransitionFromRight;
//添加到組動畫
group.animations = @[key,basic,cirani];
group.duration = 3;
//添加到view
[self.imageView.layer addAnimation:group forKey:nil];
}
// CAAnimation的代理方法,CAAnimation動畫執行完成以後的方法
- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
self.imageView.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
self.imageView.layer.position = CGPointMake(self.view.bounds.size.width, self.view.bounds.size.width * 0.5 + 100);
NSLog(@"動畫完成4");
}