Skip to content

Commit

Permalink
implement test animation
Browse files Browse the repository at this point in the history
  • Loading branch information
DHUsesAll committed Jul 29, 2015
1 parent 85aa939 commit 1b2f56f
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ - (void)setWebContent:(NSString *)webContent
if ([webContent isEqualToString:_webContent]) {
return;
}

// 加载webView
_webContent = webContent;
[self.webView loadHTMLString:webContent baseURL:[NSURL URLWithString:@""]];
}
Expand Down
2 changes: 1 addition & 1 deletion DeepBreathing/模块/知识/KnowledgeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (void)initializeAppearance
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"knowledgeData"]) {

// 调用viewModel的方法来处理所有数据
[self.viewModel prepareWithData:self.model.knowledgeData];

if (![self.viewModel isRequestSuccess]) {
Expand Down
23 changes: 17 additions & 6 deletions DeepBreathing/模块/评估/AssessmentDetailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ @interface AssessmentDetailViewController ()
// 显示当前第几题的label
@property (nonatomic, strong) UILabel * quizIndexLabel;

// 锚点
@property (nonatomic, assign) CGPoint anchorPoint;

// 所有quizView的容器
@property (nonatomic, strong) UIView * containerView;

// 容器的锚点
@property (nonatomic, assign) CGPoint anchorPoint;

@end

@implementation AssessmentDetailViewController
Expand Down Expand Up @@ -56,6 +56,7 @@ - (void)initializeAppearance
[self.view.layer addSublayer:lineLayer];
self.lineLayer = lineLayer;

// 在小球下方的曲线
UIBezierPath * linePath = ({

UIBezierPath * path = [UIBezierPath bezierPath];
Expand All @@ -72,6 +73,7 @@ - (void)initializeAppearance
[self.view addSubview:self.ballView];
[self.view addSubview:self.containerView];

// 一个左划手势,一个右滑手势
UISwipeGestureRecognizer * leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
leftSwipe.direction =UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipe];
Expand Down Expand Up @@ -178,23 +180,32 @@ - (UIView *)containerView
- (void)onSwipe:(UISwipeGestureRecognizer *)gesture
{
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) {
if (_quizIndex == 4) {
return;
}
_quizIndex++;
} else if (gesture.direction == UISwipeGestureRecognizerDirectionRight) {
if (_quizIndex==0) {
return;
}
_quizIndex--;
}

[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.86 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{

[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.78 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
// 通过旋转整个containerView来旋转所有的问题的视图
self.containerView.transform = CGAffineTransformMakeRotation(_quizIndex*M_PI/6);

} completion:^(BOOL finished) {

}];

// 小球动画
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
// 向上
self.ballView.frame = CGRectOffset(self.ballView.frame, 0, -100*[DHConvenienceAutoLayout iPhone5VerticalMutiplier]);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.86 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseOut animations:^{
[UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:1 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseOut animations:^{
// 向下
self.ballView.frame = CGRectOffset(self.ballView.frame, 0, 100*[DHConvenienceAutoLayout iPhone5VerticalMutiplier]);
} completion:^(BOOL finished) {

Expand Down
3 changes: 2 additions & 1 deletion DeepBreathing/模块/评估/AssessmentViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ @implementation AssessmentViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeAppearance];

}

- (void)initializeAppearance
Expand Down Expand Up @@ -46,7 +47,7 @@ - (void)initializeAppearance
[testButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[testButton.titleLabel setFont:[UIFont systemFontOfSize:26*[DHConvenienceAutoLayout iPhone5VerticalMutiplier]]];
testButton.titleLabel.numberOfLines = 0;


[self.view addSubview:testButton];
}
Expand Down
1 change: 1 addition & 0 deletions DeepBreathing/模块/首页/HomePageQuestionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

// 4个问题加载到该view上
@property (nonatomic, strong, readonly) UIView * questionContainerView;
@property (nonatomic, copy) void (^noBlock)();

// 单例
+ (HomePageQuestionManager *)defaultManager;
Expand Down
224 changes: 223 additions & 1 deletion DeepBreathing/模块/首页/HomePageQuestionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ @interface HomePageQuestionManager ()
@property (nonatomic, strong) QuestionBaseView * question3View;
@property (nonatomic, strong) QuestionBaseView * question4View;

@property (nonatomic, strong) UIButton * noButton;
@property (nonatomic, strong) UIButton * yesButton;

@property (nonatomic, assign) NSInteger currentQuestionIndex;

- (void)questionAnimationForIndex:(NSInteger)index;
Expand Down Expand Up @@ -81,6 +84,8 @@ + (CGPathRef)pathForEndding
- (void)didTransitionToQuestion
{
[self questionAnimationForIndex:0];
[self.questionContainerView addSubview:self.yesButton];
[self.questionContainerView addSubview:self.noButton];
}

#pragma mark - getter
Expand All @@ -89,6 +94,7 @@ - (UIView *)questionContainerView
if (!_questionContainerView) {
_questionContainerView = ({
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, QUESTION_WIDTH, QUESTION_HEIGTH)];
[view addSubview:self.question2View];
[view addSubview:self.question1View];
view;
});
Expand Down Expand Up @@ -132,6 +138,67 @@ - (QuestionBaseView *)question1View
return _question1View;
}

- (QuestionBaseView *)question2View
{
if (!_question2View) {
_question2View = ({
QuestionBaseView * view = [[QuestionBaseView alloc] initWithFrame:CGRectMake(0, 0, QUESTION_WIDTH, QUESTION_HEIGTH)];
view.hidden = YES;
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = [DHConvenienceAutoLayout frameWithLayoutOption:DHAutoLayoutOptionScale|DHAutoLayoutOptionPosition iPhone5Frame:CGRectMake(0, 0, 320, 568) adjustWidth:![DHFoundationTool iPhone4]];
gradientLayer.colors = @[(__bridge id)RGB_COLOR(246, 139, 65).CGColor,(__bridge id)RGB_COLOR(249, 210, 102).CGColor];

gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(0, 1);
[view.layer addSublayer:gradientLayer];

view;
});
}

return _question2View;
}


- (UIButton *)noButton
{
if (!_noButton) {
_noButton = ({

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.hidden = YES;
[button setCenter:[DHConvenienceAutoLayout centerWithiPhone5Center:CGPointMake(70, 500)]];
[button setBounds:[DHConvenienceAutoLayout frameWithLayoutOption:DHAutoLayoutOptionScale iPhone5Frame:CGRectMake(0, 0, 90, 90) adjustWidth:YES]];
[button setImage:IMAGE_WITH_NAME(@"错.png") forState:UIControlStateNormal];
[button setImage:IMAGE_WITH_NAME(@"错_选中.png") forState:UIControlStateSelected];
[button addTarget:self action:@selector(onNo:) forControlEvents:UIControlEventTouchUpInside];
button;

});
}

return _noButton;
}

- (UIButton *)yesButton
{
if (!_yesButton) {
_yesButton = ({

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.hidden = YES;
[button setCenter:[DHConvenienceAutoLayout centerWithiPhone5Center:CGPointMake(250, 500)]];
[button setBounds:[DHConvenienceAutoLayout frameWithLayoutOption:DHAutoLayoutOptionScale iPhone5Frame:CGRectMake(0, 0, 90, 90) adjustWidth:YES]];
[button setImage:IMAGE_WITH_NAME(@"对.png") forState:UIControlStateNormal];
[button setImage:IMAGE_WITH_NAME(@"对_选中.png") forState:UIControlStateSelected];
[button addTarget:self action:@selector(onYes:) forControlEvents:UIControlEventTouchUpInside];
button;

});
}

return _yesButton;
}

#pragma mark - 私有方法

Expand All @@ -148,7 +215,7 @@ - (void)questionAnimationForIndex:(NSInteger)index
layer = (CAShapeLayer *)layer.mask;
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.fromValue = (__bridge id)[HomePageQuestionManager pathForStarting];

animation.delegate = self;
layer.path = [HomePageQuestionManager pathForEndding];
animation.duration = 0.65;

Expand All @@ -157,4 +224,159 @@ - (void)questionAnimationForIndex:(NSInteger)index
}
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
self.yesButton.hidden = NO;
self.noButton.hidden = NO;
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration = 0.56;
animation.fromValue = [NSValue valueWithCGPoint:[DHConvenienceAutoLayout centerWithiPhone5Center:CGPointMake(-90, 500)]];
[self.noButton.layer addAnimation:animation forKey:@"noButton"];

animation.fromValue = [NSValue valueWithCGPoint:[DHConvenienceAutoLayout centerWithiPhone5Center:CGPointMake(410, 500)]];
[self.yesButton.layer addAnimation:animation forKey:@"yesButton"];
}

- (void)drawClockFace
{
// 设个容器Layer用来装整个时钟
CALayer * containerLayer = [CALayer layer];
containerLayer.frame = [DHConvenienceAutoLayout frameWithLayoutOption:DHAutoLayoutOptionScale iPhone5Frame:CGRectMake(0, 0, 200, 200) adjustWidth:YES];
containerLayer.position = [DHConvenienceAutoLayout centerWithiPhone5Center:CGPointMake(160, 120)];
[self.question2View.layer addSublayer:containerLayer];

// 表盘
CALayer * clockFaceLayer = [CALayer layer];
clockFaceLayer.frame = containerLayer.bounds;
clockFaceLayer.contents = (__bridge id)IMAGE_WITH_NAME(@"表盘.png").CGImage;
[containerLayer addSublayer:clockFaceLayer];

// 各个指针的初始化,主要是设置anchorPoint
// center和表盘中心对其,然后设置锚点
CALayer * hourHand = [self handWithSize:CGSizeMake(75, 95) image:IMAGE_WITH_NAME(@"时钟.png")];

hourHand.anchorPoint = CGPointMake(0.9, 0.9);
[containerLayer addSublayer:hourHand];

CALayer * minutesHand = [self handWithSize:CGSizeMake(130, 10) image:IMAGE_WITH_NAME(@"分钟.png")];
minutesHand.anchorPoint = CGPointMake(0.02, 0.5);
[containerLayer addSublayer:minutesHand];

CALayer * secondHand = [self handWithSize:CGSizeMake(11, 186) image:IMAGE_WITH_NAME(@"秒针.png")];

secondHand.anchorPoint = CGPointMake(0.5, 0.22);
[containerLayer addSublayer:secondHand];

CGFloat duration = 1.5;

// 表盘缩放动画
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.fromValue = @3;
animation.duration = duration;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[clockFaceLayer addAnimation:animation forKey:@"clockFace"];

// 添加动画
[hourHand addAnimation:[self animationGroupFromPosition:CGPointMake(0, 100)] forKey:@"hourHand"];
[minutesHand addAnimation:[self animationGroupFromPosition:CGPointMake(300, 100)] forKey:@"minutesHand"];
[secondHand addAnimation:[self animationGroupFromPosition:CGPointMake(160, 400)] forKey:@"sectionHand"];
}

- (CAAnimationGroup *)animationGroupFromPosition:(CGPoint)position
{
// 统一持续时间
CGFloat duration = 1.5;
// 统一曲线,淡入淡出
CAMediaTimingFunction * timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

// 改变anchorPoint
// 因为旋转的时候anchorPoint是0.5,0.5
// 动画结束后anchorPoint需要改变
CABasicAnimation * anchorPointAnimation = [CABasicAnimation animationWithKeyPath:@"anchorPoint"];
anchorPointAnimation.duration = duration;
anchorPointAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0.5, 0.5)];
anchorPointAnimation.timingFunction = timingFunction;

// 位置动画,改变各个指针的位置
CABasicAnimation * positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
positionAnimation.duration = duration;
positionAnimation.fromValue = [NSValue valueWithCGPoint:[DHConvenienceAutoLayout centerWithiPhone5Center:position]];
positionAnimation.timingFunction = timingFunction;

// 旋转动画
CABasicAnimation * rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotateAnimation.fromValue = @0;
rotateAnimation.toValue = @(4*M_PI);
rotateAnimation.duration = duration;
rotateAnimation.timingFunction = timingFunction;

// 缩放动画
// 从2倍缩放到0.7倍
CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = @2;
scaleAnimation.duration = duration;
scaleAnimation.timingFunction = timingFunction;

// 动画组,返回
CAAnimationGroup * animationGroup = [CAAnimationGroup animation];
animationGroup.duration = duration;
animationGroup.animations = @[positionAnimation, scaleAnimation,rotateAnimation,anchorPointAnimation];
return animationGroup;
}




- (CALayer *)handWithSize:(CGSize)size image:(UIImage *)image
{
CALayer * layer = [CALayer layer];
[layer setBounds:[DHConvenienceAutoLayout frameWithLayoutOption:DHAutoLayoutOptionScale iPhone5Size:size iPhone5Center:CGPointZero adjustWidth:YES]];
layer.position = [DHConvenienceAutoLayout centerWithiPhone5Center:CGPointMake(100, 100)];
layer.transform = CATransform3DMakeScale(0.7, 0.7, 1);
layer.contents = (__bridge id)image.CGImage;


return layer;
}


#pragma mark - button action

- (void)onNo:(UIButton *)sender
{
sender.selected = YES;
// [self.question1View removeFromSuperview];
// [self.question2View removeFromSuperview];
// self.question1View = nil;
// self.question2View = nil;
// if (self.noBlock) {
// self.noBlock();
// }
}

- (void)onYes:(UIButton *)sender
{
sender.selected = YES;
[UIView animateWithDuration:0.3 animations:^{
// 将question1移除
self.question1View.center = CGPointMake(-160*[DHConvenienceAutoLayout iPhone5VerticalMutiplier], self.question1View.center.y);
}];

self.question2View.hidden = NO;
// 取到gradientLayer
NSArray * sublayers = self.question2View.layer.sublayers;
CAGradientLayer * layer = sublayers.lastObject;
// 改变渐变色的动画
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"colors"];
animation.beginTime = 0.3 + CACurrentMediaTime();
animation.fillMode = kCAFillModeBackwards;
animation.duration = 3;
animation.fromValue = layer.colors;
layer.colors = @[(__bridge id)RGB_COLOR(80, 80, 80).CGColor,(__bridge id)RGB_COLOR(10, 10, 255).CGColor];
[layer addAnimation:animation forKey:@"gradient"];

[self drawClockFace];
}


@end
5 changes: 5 additions & 0 deletions DeepBreathing/模块/首页/HomePageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ - (CAShapeLayer *)maskLayer
#pragma mark - button action
- (void)fillIn
{
[HomePageQuestionManager defaultManager].noBlock = ^(){
_resultView.hidden = NO;
};
// 从resultView切换到questionView
UIView * containerView = [[HomePageQuestionManager defaultManager] questionContainerView];
containerView.hidden = NO;
[UIView animateWithDuration:1.2 animations:^{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_resultView cache:NO];
} completion:^(BOOL finished) {
Expand Down
5 changes: 5 additions & 0 deletions DeepBreathing/模块/首页/QuestionBaseView.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ @interface QuestionBaseView ()

@implementation QuestionBaseView

- (void)dealloc
{
NSLog(@"%@ dealloc",self);
}


#pragma mark - getter
- (UISwipeGestureRecognizer *)swipeGesture
Expand Down

0 comments on commit 1b2f56f

Please sign in to comment.