//static int distantce = 100;
@interface ViewController ()
- (IBAction)startAnimation:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIView *testView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)startAnimation:(id)sender {
//开始动画
[UIView beginAnimations:@"moveqq" context:nil];
//延迟2秒执行
// [UIView setAnimationDelay:2];
//设置动画时间
[UIView setAnimationDuration:1];
//设置动画代理
[UIView setAnimationDelegate:self];
//将要开始动画
[UIView setAnimationWillStartSelector:@selector(willStartAnimation)];
//已经结束动画
[UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
//设置动画的速度变化
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//设置重复次数
// [UIView setAnimationRepeatCount:3];
//动画的反转
// [UIView setAnimationRepeatAutoreverses:YES];
//设置UIView的位置变化,大小变化,透明度
// self.imageView.alpha = 0;
// self.testView.backgroundColor = [UIColor redColor];
//
// distantce += 20;
//
// self.testView.frame = CGRectMake(distantce, 0, 100, 100);
//拉伸
// self.testView.transform = CGAffineTransformMakeScale(1.5, 1.5);
//平移
// self.testView.transform = CGAffineTransformMakeTranslation(100, 100);
// self.testView.transform = CGAffineTransformTranslate(self.testView.transform, 20, 0);
//旋转
// self.testView.transform = CGAffineTransformMakeRotation(M_PI_2);
// self.testView.transform = CGAffineTransformRotate(self.testView.transform, M_PI_4/2);
//提交动画
[UIView commitAnimations];
}
- (void)willStartAnimation {
NSLog(@"开始动画");
}
- (void)didStopAnimation {
NSLog(@"结束动画");
}
基础动画
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)doAction:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)doAction:(id)sender {
//Duration动画时间 animations后的状态
// [UIView animateWithDuration:1 animations:^{
// self.imageView.alpha = 0;
// }];
//completion动画完成之后的代码块
// [UIView animateWithDuration:1 animations:^{
// self.imageView.alpha = 0;
// } completion:^(BOOL finished) {
//
// [UIView animateWithDuration:1 animations:^{
//
// self.imageView.alpha = 1;
// self.imageView.frame = CGRectMake(200, 100, 100, 100);
//
// } completion:^(BOOL finished) {
// [UIView animateWithDuration:1 animations:^{
// self.imageView.frame = CGRectMake(200, 100, 100, 100);
//
// }];
// }];
// }];
// [UIView animateWithDuration:1 animations:^{
//
// self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_2);
//
// } completion:^(BOOL finished) {
//
// [UIView animateWithDuration:1 animations:^{
//
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, -M_PI_2);
//
// //CGAffineTransformIdentity恢复最初状态
// self.imageView.transform = CGAffineTransformIdentity;
//
// }];
//
// }];
// [UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionCurveEaseOut animations:^{
// self.imageView.backgroundColor = [UIColor grayColor];
// } completion:^(BOOL finished) {
//
// }];
//Damping 弹力 0 ~ 1 四星
//SpringVelocity 弹簧初速度
// [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
//
// self.imageView.frame = CGRectMake(200, 100, 100, 100);
//
// } completion:^(BOOL finished) {
//
// }];
//关键帧动画 五星
// [UIView animateKeyframesWithDuration:3.0 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
//
// [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1/6.0 animations:^{
// self.imageView.backgroundColor = [UIColor redColor];
// }];
//
// [UIView addKeyframeWithRelativeStartTime:1/6.0 relativeDuration:3/6.0 animations:^{
// self.imageView.backgroundColor = [UIColor blueColor];
// }];
//
// [UIView addKeyframeWithRelativeStartTime:4/6.0 relativeDuration:2/6.0 animations:^{
// self.imageView.backgroundColor = [UIColor orangeColor];
// }];
//
//
// } completion:^(BOOL finished) {
//
// }];
}
转场动画
@property (nonatomic ,strong) UIImageView * firstView, * secondView;
@property (nonatomic, strong) UIView * contentView;
- (IBAction)startAnimation:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.contentView];
[self.contentView addSubview:self.firstView];
[self.contentView addSubview:self.secondView];
}
- (UIImageView *)firstView {
if (!_firstView) {
_firstView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"baby.jpg"]];
_firstView.frame = CGRectMake(0, 0, 200, 200);
}
return _firstView;
}
- (UIImageView *)secondView {
if (!_secondView) {
_secondView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"baby1"]];
_secondView.frame = CGRectMake(0, 0, 200, 200);
}
return _secondView;
}
- (UIView *)contentView {
if (!_contentView) {
_contentView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
}
return _contentView;
}
- (IBAction)startAnimation:(id)sender {
// [self animation1];
//UIView block转场动画 推荐这个方法
// [UIView transitionFromView:self.secondView toView:self.firstView duration:2 options:UIViewAnimationOptionTransitionCurlUp completion:^(BOOL finished) {
//
// }];
//param1 父视图 这个方法常用于视图控制器的动画
[UIView transitionWithView:self.contentView duration:2 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[self.firstView removeFromSuperview];
[self.contentView addSubview:self.secondView];
} completion:^(BOOL finished) {
}];
}
- (void)animation1 {
[UIView beginAnimations:@"transition" context:nil];
[UIView setAnimationDuration:2];
//设置转场动画
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.contentView cache:YES];
[self.contentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];
}
动画练习 简单封装
#import <UIKit/UIKit.h>
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface FootView : UIView
@end
#import "FootView.h"
static CGFloat kMargin = 10;
@interface FootView ()
@property (nonatomic, strong) UIButton * collectBtn;
@property (nonatomic, strong) UIButton * shareBtn;
@end
@implementation FootView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.collectBtn];
[self addSubview:self.shareBtn];
self.backgroundColor = [UIColor greenColor];
}
return self;
}
#pragma mark - getters and setters
- (UIButton *)collectBtn {
if (!_collectBtn) {
_collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_collectBtn.backgroundColor = [UIColor yellowColor];
[_collectBtn setTitle:@"收藏" forState:UIControlStateNormal];
[_collectBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
_collectBtn.frame = CGRectMake(kMargin, kMargin, (SCREEN_WIDTH - 3 * kMargin)/2, 60 - 2 * kMargin);
}
return _collectBtn;
}
- (UIButton *)shareBtn {
if (!_shareBtn) {
_shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_shareBtn.backgroundColor = [UIColor yellowColor];
[_shareBtn setTitle:@"收藏" forState:UIControlStateNormal];
[_shareBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
_shareBtn.frame = CGRectMake(2 * kMargin + (SCREEN_WIDTH - 3 * kMargin)/2, kMargin, (SCREEN_WIDTH - 3 * kMargin)/2, 60 - 2 * kMargin);
}
return _shareBtn;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
#import "ViewController.h"
#import "FootView.h"
@interface ViewController ()
@property (nonatomic, strong) FootView * footView;
- (IBAction)doAction:(id)sender;
@end
@implementation ViewController
- (FootView *)footView {
if (!_footView) {
_footView = [[FootView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 60, SCREEN_WIDTH, 60)];
}
return _footView;
}
- (IBAction)doAction:(id)sender {
UIButton * btn = (UIButton *)sender;
btn.enabled = NO;
btn.selected = !btn.selected;
__block CGRect rect = self.footView.frame;
if (btn.selected) {
[UIView animateWithDuration:2 animations:^{
rect.origin.y += 60;
self.footView.frame = rect;
} completion:^(BOOL finished) {
btn.enabled = YES;
}];
} else {
[UIView animateWithDuration:2 animations:^{
rect.origin.y -= 60;
self.footView.frame = rect;
} completion:^(BOOL finished) {
btn.enabled = YES;
}];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.footView];
}