這兩種動(dòng)畫實(shí)現(xiàn)起來都很簡單,蘋果已經(jīng)為我們封裝好了,并且提供了多種好看實(shí)用的效果
彈簧動(dòng)畫
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建一個(gè)氣泡
UIButton * bubble = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:bubble];
bubble.layer.cornerRadius = 50;
bubble.backgroundColor = [UIColor cyanColor];
[bubble addTarget:self action:@selector(bubbleAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)bubbleAction:(UIButton *)button {
// 先縮小
button.transform = CGAffineTransformMakeScale(0.7, 0.7);
// 彈簧動(dòng)畫,參數(shù)分別為:時(shí)長襟衰,延時(shí),彈性(越小彈性越大)禁荸,初始速度
[UIView animateWithDuration: 0.7 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.3 options:0 animations:^{
// 放大
button.transform = CGAffineTransformMakeScale(1, 1);
} completion:nil];
}
完事
視圖過渡動(dòng)畫
視圖過渡動(dòng)畫有兩種右蒲,一種是單個(gè)視圖的過渡動(dòng)畫,另一種是兩個(gè)UIView之間的過渡
過渡類型:
- UIViewAnimationOptionTransitionFlipFromTop由上向下翻轉(zhuǎn)動(dòng)畫
- UIViewAnimationOptionTransitionFlipFromBottom由下向上翻轉(zhuǎn)動(dòng)畫
- UIViewAnimationOptionTransitionCrossDissolve漸現(xiàn)動(dòng)畫
- UIViewAnimationOptionTransitionCurlDown由上到下翻頁動(dòng)畫
- UIViewAnimationOptionTransitionCurlUp由下到上翻頁動(dòng)畫
- UIViewAnimationOptionTransitionFlipFromRight由左向右翻轉(zhuǎn)
- UIViewAnimationOptionTransitionFlipFromLeft由右向左翻轉(zhuǎn)
速度
- UIViewAnimationOptionCurveEaseInOut 緩入緩出赶熟,開始和結(jié)束時(shí)減速
- UIViewAnimationOptionCurveEaseIn 緩入瑰妄,開始時(shí)候減速
- UIViewAnimationOptionCurveEaseOut 緩出,結(jié)束時(shí)減速
- UIViewAnimationOptionCurveLinear 線性映砖,勻速運(yùn)動(dòng)
過渡視圖和速度在動(dòng)畫的options參數(shù)中使用间坐。但是不要使用多種過渡類型,真是要難看死了我的天哪邑退。
options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionCurveEaseInOut
Swift寫法(感謝熊格智障的評(píng)論):
options: [.TransitionCrossDissolve,.CurveEaseInOut]
1. 單個(gè)視圖的過渡
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建一個(gè)按鈕
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
[self.view addSubview:button];
button.backgroundColor = [UIColor orangeColor];
button.layer.cornerRadius = 3;
[button setTitle:@"??" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonAction:(UIButton *)button {
// transition動(dòng)畫:指定一個(gè)view竹宋,單獨(dú)為它設(shè)置transition的動(dòng)畫
// option設(shè)置動(dòng)畫類型,這里使用翻頁動(dòng)畫以及淡出淡入
[UIView transitionWithView:button duration:0.7 options:UIViewAnimationOptionTransitionCurlDown | UIViewAnimationOptionCurveEaseOut animations:^{
[button setTitle:@"??" forState:UIControlStateNormal];
} completion:nil];
}
再來一個(gè)實(shí)用的imageView圖片漸變效果:
@interface ViewController ()
@property (nonatomic, retain) UIImageView * imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];
[self.view addSubview:_imageView];
_imageView.image = [UIImage imageNamed:@"1.jpg"];
// 1s后換圖片
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage) userInfo:nil repeats:NO];
}
- (void)changeImage {
// 使用漸變效果就夠了
[UIView transitionWithView:_imageView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
_imageView.image = [UIImage imageNamed:@"2.jpg"];
} completion:nil];
}
2. 兩個(gè)視圖的過渡動(dòng)畫
方法:
UIView
的transitionFromView: toView: duration: options:
方法
官方解釋:toView added to fromView.superview, fromView removed from its superview
實(shí)際效果:動(dòng)畫的作用范圍為fromView
的父視圖的frame
地技。動(dòng)畫結(jié)束后fromView
會(huì)被刪除蜈七,toView
會(huì)被添加到fromView
的父視圖
@interface ViewController ()
@property (nonatomic, retain) UIView * view1;
@property (nonatomic, retain) UIView * view2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建占用半個(gè)屏幕的view,作為過渡view的父視圖莫矗,測試動(dòng)畫的作用范圍
UIView * bigView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 300)];
[self.view addSubview:bigView];
bigView.backgroundColor = [UIColor orangeColor];
self.view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
[bigView addSubview:_view1];
_view1.backgroundColor = [UIColor redColor];
self.view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
// 不要加到視圖上飒硅!看效果
// [self.view addSubview:_view2];
_view2.backgroundColor = [UIColor blackColor];
// 1s后執(zhí)行
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(animate) userInfo:nil repeats:NO];
}
- (void)animate {
[UIView transitionFromView:_view1 toView:_view2 duration:1 options:UIViewAnimationOptionTransitionCurlDown completion:^(BOOL finished) {
}];
}
@end