1.点击按钮,弹出View,再次点击,View回去
创建一个Button,并给button设置一个Bool值,在button的点击事件里写:
- (
void
)viewBtnAction:(
UIButton
*)btn{
if ( self . viewbtn . selected ) {
[ UIView animateWithDuration : 0.5 animations :^{
self . myView . frame = CGRectMake ( 0 , 677 , 375 , 50 );
NSLog ( @"===== %@" , NSStringFromCGRect ( self . myView . frame ));
}];
} else {
//view 动画
// 参数 1: 动画时长
[ UIView animateWithDuration : 0.5 animations :^{
if ( self . viewbtn . selected ) {
[ UIView animateWithDuration : 0.5 animations :^{
self . myView . frame = CGRectMake ( 0 , 677 , 375 , 50 );
NSLog ( @"===== %@" , NSStringFromCGRect ( self . myView . frame ));
}];
} else {
//view 动画
// 参数 1: 动画时长
[ UIView animateWithDuration : 0.5 animations :^{
self.myView.frame = CGRectMake(0, 577, 375, 50);
NSLog(@"===== %@",NSStringFromCGRect(self.myView.frame));
}];
}
2.点击按钮.弹出View,过几秒,View自动回去
创建一个Button,在button的点击事件里写:
- (
void
)viewBtnAction:(
UIButton
*)btn{
[
UIView
animateWithDuration
:
0.5
animations
:^{
self
.
myView
.
frame
=
CGRectMake
(
0
,
677
,
375
,
50
);
NSLog ( @"===== %@" , NSStringFromCGRect ( self . myView . frame ));
}];
NSLog ( @"===== %@" , NSStringFromCGRect ( self . myView . frame ));
}];
//delay
延迟几秒执行
[
UIView
animateWithDuration
:
0.5
delay
:
0.5
options
:
UIViewAnimationOptionLayoutSubviews
animations
:^{
self . myView . frame = CGRectMake ( 0 , 667 , 375 , 50 );
} completion :^( BOOL finished) {
}];
self . myView . frame = CGRectMake ( 0 , 667 , 375 , 50 );
} completion :^( BOOL finished) {
}];
}
3.键盘上的自定义动画框
//
监听键盘弹起
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
- (
void
)keyboardWillShow:(
NSNotification
*)notif{
// 得到键盘 frame, 通过键盘 frame 改变自定义工具栏高度
CGRect frame = [[[notif userInfo ] objectForKey : UIKeyboardFrameEndUserInfoKey ] CGRectValue ];
NSLog ( @"frame == %@" , NSStringFromCGRect (frame));
//view 动画
// 参数 1: 动画时长
[ UIView animateWithDuration : 0.5 animations :^{
self . myView . frame = CGRectMake ( 0 , 627 - frame. size . height , 375 , 50 );
NSLog ( @"===== %@" , NSStringFromCGRect ( self . myView . frame ));
}];
// 得到键盘 frame, 通过键盘 frame 改变自定义工具栏高度
CGRect frame = [[[notif userInfo ] objectForKey : UIKeyboardFrameEndUserInfoKey ] CGRectValue ];
NSLog ( @"frame == %@" , NSStringFromCGRect (frame));
//view 动画
// 参数 1: 动画时长
[ UIView animateWithDuration : 0.5 animations :^{
self . myView . frame = CGRectMake ( 0 , 627 - frame. size . height , 375 , 50 );
NSLog ( @"===== %@" , NSStringFromCGRect ( self . myView . frame ));
}];
}
4.
Transform
动画
创建一个View,和一个按钮,在按钮的点击事件里写
- (
void
)TransformBtnAction:(
UIButton
*)btn{
//transform 是 View 的一个属性 , 是用来改变 View 的形态的 , 通过设置 transform 属性值可以实现 View 的形态变化 , 比如旋转 , 缩放等
//rotate: 旋转的意思 M_PI 是 180 度
// self.myView.transform = CGAffineTransformRotate(self.myView.transform, M_PI_4);
// 每次缩放原来的 0.9 倍
// self.myView.transform = CGAffineTransformScale(self.myView.transform, 0.9, 0.9);
// 移动 正数代表往右下方移动 , 负数代表往左上方移动
//transform 是 View 的一个属性 , 是用来改变 View 的形态的 , 通过设置 transform 属性值可以实现 View 的形态变化 , 比如旋转 , 缩放等
//rotate: 旋转的意思 M_PI 是 180 度
// self.myView.transform = CGAffineTransformRotate(self.myView.transform, M_PI_4);
// 每次缩放原来的 0.9 倍
// self.myView.transform = CGAffineTransformScale(self.myView.transform, 0.9, 0.9);
// 移动 正数代表往右下方移动 , 负数代表往左上方移动
self.myView.transform = CGAffineTransformTranslate(self.myView.transform, 5, 5);
}
5.layer动画
创建一个Button控制,点击旋转,再次点击停止,再点击又继续;在View上添加一张图片,并给图片设置轻拍,可以点击图片控制旋不旋转,并设置Bool值;
在Button的点击事件里写:
- (
void
)layerBtnAction:(
UIButton
*)btn{
//layer 层 , 每个 View 视图都有一个 layer 层 , 使用来设置 View 上的内容 , 比如背景颜色 ,frame, 文字等内容 , 而 View 只用来负责显示 layer 层
// 我们可以通过改变 layer 层的内容
// 改变 view z 轴值
CABasicAnimation *animation = [ CABasicAnimation animationWithKeyPath : @"transform.rotation.z" ];
// 旋转起始值
animation. fromValue = [ NSNumber numberWithInt : 0 ];
// 最终旋转的角度
animation. toValue = [ NSNumber numberWithInt : M_PI * 2 ];
// 旋转时间
animation. duration = 10 ;
// 重复次数 ,NSIntegerMax 为无限旋转
animation. repeatCount = NSIntegerMax ;
// 旋转结束后是否要逆向返回原位置
animation. autoreverses = NO ;
// 是否按照结束位置继续旋转
animation. cumulative = YES ;
[ self . imgV . layer addAnimation :animation forKey : @"basicAnimation" ];
//layer 层 , 每个 View 视图都有一个 layer 层 , 使用来设置 View 上的内容 , 比如背景颜色 ,frame, 文字等内容 , 而 View 只用来负责显示 layer 层
// 我们可以通过改变 layer 层的内容
// 改变 view z 轴值
CABasicAnimation *animation = [ CABasicAnimation animationWithKeyPath : @"transform.rotation.z" ];
// 旋转起始值
animation. fromValue = [ NSNumber numberWithInt : 0 ];
// 最终旋转的角度
animation. toValue = [ NSNumber numberWithInt : M_PI * 2 ];
// 旋转时间
animation. duration = 10 ;
// 重复次数 ,NSIntegerMax 为无限旋转
animation. repeatCount = NSIntegerMax ;
// 旋转结束后是否要逆向返回原位置
animation. autoreverses = NO ;
// 是否按照结束位置继续旋转
animation. cumulative = YES ;
[ self . imgV . layer addAnimation :animation forKey : @"basicAnimation" ];
}
在轻拍事件里写:
- (
void
)tapImg:(
UITapGestureRecognizer
*)tap{
if (self.isSelected) {
//
获得上次停止的时间的偏移量
CFTimeInterval stopTime = self . imgV . layer . timeOffset ;
self . imgV . layer . beginTime = 0 ;
// 设置速度
self . imgV . layer . speed = 1.0 ;
// 设置偏移量为 0
self . imgV . layer . timeOffset = 0 ;
// 设置开始时间
self . imgV . layer . beginTime = [ self . imgV . layer convertTime : CACurrentMediaTime () fromLayer : nil ] - stopTime;
} else {
// 每一个 view 的 layer 层系统设置记录了一个时间的属性 , 通过改变 view 动画时间来控制动画效果
// 获得当前旋转的时间点
CFTimeInterval stopTime = [ self . imgV . layer convertTime : CACurrentMediaTime () fromLayer : nil ];
// 设置播放速度变成 0, 即停止
self . imgV . layer . speed = 0 ;
self . imgV . layer . timeOffset = stopTime;
}
self . isSelected = ! self . isSelected ;
CFTimeInterval stopTime = self . imgV . layer . timeOffset ;
self . imgV . layer . beginTime = 0 ;
// 设置速度
self . imgV . layer . speed = 1.0 ;
// 设置偏移量为 0
self . imgV . layer . timeOffset = 0 ;
// 设置开始时间
self . imgV . layer . beginTime = [ self . imgV . layer convertTime : CACurrentMediaTime () fromLayer : nil ] - stopTime;
} else {
// 每一个 view 的 layer 层系统设置记录了一个时间的属性 , 通过改变 view 动画时间来控制动画效果
// 获得当前旋转的时间点
CFTimeInterval stopTime = [ self . imgV . layer convertTime : CACurrentMediaTime () fromLayer : nil ];
// 设置播放速度变成 0, 即停止
self . imgV . layer . speed = 0 ;
self . imgV . layer . timeOffset = stopTime;
}
self . isSelected = ! self . isSelected ;
}
6.长按晃动
在一个View上添加长按手势
UILongPressGestureRecognizer
*longView = [[
UILongPressGestureRecognizer
alloc
]
initWithTarget
:
self
action
:
@selector
(longView:)];
[self.myView addGestureRecognizer:longView];
- (
void
)longView:(
UILongPressGestureRecognizer
*)longTap{
// 旋转
CAKeyframeAnimation *keyAnimation = [ CAKeyframeAnimation animationWithKeyPath : @"transform.rotation" ];
float top = M_PI / 18 ;
float bom = - M_PI / 18 ;
keyAnimation. values = @[@( top ) , @(0) , @( bom ) , @(0) , @( top )] ;
keyAnimation. repeatCount = NSIntegerMax ;
keyAnimation. duration = 0.3 ;
[ self . myView . layer addAnimation :keyAnimation forKey : @"key" ];
dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW , ( int64_t )( 4 * NSEC_PER_SEC )), dispatch_get_main_queue (), ^{
// 停止动画
[ self . myView . layer removeAnimationForKey : @"key" ];
});
// 旋转
CAKeyframeAnimation *keyAnimation = [ CAKeyframeAnimation animationWithKeyPath : @"transform.rotation" ];
float top = M_PI / 18 ;
float bom = - M_PI / 18 ;
keyAnimation. values = @[@( top ) , @(0) , @( bom ) , @(0) , @( top )] ;
keyAnimation. repeatCount = NSIntegerMax ;
keyAnimation. duration = 0.3 ;
[ self . myView . layer addAnimation :keyAnimation forKey : @"key" ];
dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW , ( int64_t )( 4 * NSEC_PER_SEC )), dispatch_get_main_queue (), ^{
// 停止动画
[ self . myView . layer removeAnimationForKey : @"key" ];
});
}
7.关键帧动画:
创建一个按钮,在按钮上放一张没颜色图片,点击按钮,变成另一张带颜色图片,在按钮的点击事件里写:
- (
void
)zanBtnAction:(
UIButton
*)btn{
// 关键帧动画
CAKeyframeAnimation *keyAnimation = [ CAKeyframeAnimation animationWithKeyPath : @"transform.scale" ];
// 设置关键帧的值 ,values 是数组类型
keyAnimation. values = @[@(0.1) , @(1.0) , @(1.5)] ;
// NSNumber *num = [NSNumber numberWithFloat:0.1];
// keyAnimation.values = [NSArray arrayWithObjects:(nonnull id), ..., nil];
keyAnimation. duration = 0.3 ;
[ self . zanBtn . layer addAnimation :keyAnimation forKey : @"key" ];
if ( self . zanBtn . isSelected == NO ) {
[ self . zanBtn setBackgroundImage :[ UIImage imageNamed : @"zan4.jpg" ] forState : UIControlStateNormal ];
} else {
[ self . zanBtn setBackgroundImage :[ UIImage imageNamed : @"zan2.jpg" ] forState : UIControlStateNormal ];
}
self . zanBtn . selected = ! self . zanBtn . selected ;
// 关键帧动画
CAKeyframeAnimation *keyAnimation = [ CAKeyframeAnimation animationWithKeyPath : @"transform.scale" ];
// 设置关键帧的值 ,values 是数组类型
keyAnimation. values = @[@(0.1) , @(1.0) , @(1.5)] ;
// NSNumber *num = [NSNumber numberWithFloat:0.1];
// keyAnimation.values = [NSArray arrayWithObjects:(nonnull id), ..., nil];
keyAnimation. duration = 0.3 ;
[ self . zanBtn . layer addAnimation :keyAnimation forKey : @"key" ];
if ( self . zanBtn . isSelected == NO ) {
[ self . zanBtn setBackgroundImage :[ UIImage imageNamed : @"zan4.jpg" ] forState : UIControlStateNormal ];
} else {
[ self . zanBtn setBackgroundImage :[ UIImage imageNamed : @"zan2.jpg" ] forState : UIControlStateNormal ];
}
self . zanBtn . selected = ! self . zanBtn . selected ;
}