-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMJAnimationCirculation.m
More file actions
75 lines (69 loc) · 2.57 KB
/
MJAnimationCirculation.m
File metadata and controls
75 lines (69 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// AnimationCirculation.m
//
//
// Created by 刘鹏 on 2018/4/10.
// Copyright © 2018年 Musjoy. All rights reserved.
//
#import "MJAnimationCirculation.h"
@implementation MJAnimationCirculation
/**
实例化连续循环动画管理
@param view 需要动画的视图(开始能看到)
@param viewCopy 视图的另一份copy(开始看不到)
@param duration 单次循环耗时
@param direction 运动方向
@return 实例
*/
- (instancetype)initWithView:(UIView *)view copyView:(UIView *)viewCopy duration:(NSTimeInterval)duration direction:(MJAnimationCirculationDirection)direction
{
if (view == nil || viewCopy == nil) {
return nil;
}
self = [super initWithViewArray:@[view, viewCopy] withAnimation:[self translationAnimationWithDuration:duration]];
if (self) {
// 配置每次循环前的准备工作
[self configBeforeEachStart:^(NSArray<UIView *> *arrViews, CAAnimation *animation) {
// 当完全超出边界后,调整位置
CGFloat width = arrViews.firstObject.bounds.size.width;
if (direction == MJAnimationCirculationDirection_Left) {
// 向左
for (UIView *aView in arrViews) {
if (aView.frame.origin.x <= -width) {
CGRect frame = aView.frame;
frame.origin.x = width;
aView.frame = frame;
}
}
} else {
// 向右
for (UIView *aView in arrViews) {
if (aView.frame.origin.x >= width) {
CGRect frame = aView.frame;
frame.origin.x = -width;
aView.frame = frame;
}
}
}
// 计算需要移动的偏移量
CABasicAnimation *aAnimation = (CABasicAnimation *)animation;
if (direction == MJAnimationCirculationDirection_Left) {
// 向左
aAnimation.byValue = @(-width);
} else {
// 向右
aAnimation.byValue = @(width);
}
}];
}
return self;
}
- (CAAnimation *)translationAnimationWithDuration:(NSTimeInterval)duration
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.repeatCount = CGFLOAT_MAX;
animation.duration = duration;
return animation;
}
@end