iOS - 视图抖动效果
程序员文章站
2024-03-26 08:03:24
...
实现视图抖动效果的思路
一、首先,如何让视图发生位移
UIView中定义了transform属性,其类型为CGAffineTransform,默认是CGAffineTransformIdentity
transform 改变位置/尺寸/旋转角度,且是在原有transform的基础上改变
①创建“基于控件初始位置”的形变
CGAffineTransformMakeTranslation(平移)
CGAffineTransformMakeScale(缩放)
CGAffineTransformMakeRotation(旋转)
②创建“基于transform参数”的形变
CGAffineTransformTranslate
CGAffineTransformScale
CGAffineTransformRotate
补充:
在OC中,所有跟角度相关的数值,都是弧度值,180° = M_PI
正数表示顺时针旋转
负数表示逆时针旋转
二、如何让视图的位移形成反复动画
在UIViewAnimationWithBlocks中提供了一系列的视图动画
我们最常用的也就是下面三种:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
其中UIViewAnimationOptions枚举值如下:
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionLayoutSubviews = 1 << 0, // 提交动画的时候布局子控件,表示子控件将和父控件一同动画
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // 动画时允许用户交互
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // 从当前状态开始动画,而不是初始状态
UIViewAnimationOptionRepeat = 1 << 3, // 动画无限重复
UIViewAnimationOptionAutoreverse = 1 << 4, // 在动画可以重复的前提下,可执行动画回路
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // 忽略外层动画嵌套的执行时间
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // 忽略外层动画嵌套的时间变化曲线
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // 用显隐的方式替代添加移除图层的动画效果
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9, // 忽略嵌套继承的选项
///时间函数曲线相关
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // 时间曲线函数,由慢到快 default
UIViewAnimationOptionCurveEaseIn = 1 << 16, // 时间曲线函数,由慢到特别快
UIViewAnimationOptionCurveEaseOut = 2 << 16, //时间曲线函数,由快到慢
UIViewAnimationOptionCurveLinear = 3 << 16, //时间曲线函数,匀速
///转场动画相关的
UIViewAnimationOptionTransitionNone = 0 << 20, //无转场动画 default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20, //转场从左翻转
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20, //转场从右翻转
UIViewAnimationOptionTransitionCurlUp = 3 << 20, //上卷转场
UIViewAnimationOptionTransitionCurlDown = 4 << 20, //下卷转场
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20, //转场交叉消失
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20, //转场从上翻转
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20, //转场从下翻转
UIViewAnimationOptionPreferredFramesPerSecondDefault = 0 << 24,
UIViewAnimationOptionPreferredFramesPerSecond60 = 3 << 24,
UIViewAnimationOptionPreferredFramesPerSecond30 = 7 << 24,
} NS_ENUM_AVAILABLE_IOS(4_0);
补充:关于最后一组转场动画它一般是用在这个方法中的:
[UIView transitionFromView: toView: duration: options: completion:^(BOOL finished) {}];
三、基础了解完毕,上代码
+ (void)shakeView:(UIView*)viewToShake {
// 偏移值
CGFloat t = 3.0;
// 左摇
CGAffineTransform translateLeft =CGAffineTransformTranslate(CGAffineTransformIdentity,-t,0.0);
// 右晃
CGAffineTransform translateRight =CGAffineTransformTranslate(CGAffineTransformIdentity, t,0.0);
// 执行动画 重复动画且执行动画回路
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
// 设置重复次数 repeatCount为float
[UIView setAnimationRepeatCount:2.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished){
if(finished){
// 从视图当前状态回到初始状态
[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform =CGAffineTransformIdentity;
} completion:NULL];
}
}];
}
转载于:https://www.jianshu.com/p/f81ed56d7a15
上一篇: JMS的一个简单同步实现
下一篇: 06 Java方法重载