LambohiniViewController.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // LambohiniViewController.m
  3. // animatedemo
  4. //
  5. // Created by 7yword on 16/7/13.
  6. // Copyright © 2016年 7yworld. All rights reserved.
  7. //
  8. #import "LambohiniViewController.h"
  9. @interface LambohiniViewController ()
  10. @property (weak, nonatomic) IBOutlet UIView *car_body;
  11. @property (weak, nonatomic) IBOutlet UIImageView *front;
  12. @property (weak, nonatomic) IBOutlet UIImageView *body;
  13. @end
  14. @implementation LambohiniViewController
  15. - (void)viewDidLoad
  16. {
  17. [super viewDidLoad];
  18. self.view.backgroundColor = kClearColor;
  19. _senderNameLabel.textColor = kTextColorSenderName;
  20. _senderNameLabel.text = _senderNameStr;
  21. [_car_body bringSubviewToFront:_body];
  22. [self do_rotation:_front];
  23. [self do_move:_car_body];
  24. }
  25. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
  26. {
  27. NSString * value = [anim valueForKey:@"moveAnimation"];
  28. if ([value isEqualToString:@"moveAnimation"])
  29. {
  30. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  31. CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
  32. moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height + 50)];
  33. moveAnimation.duration = 3.0f;
  34. moveAnimation.removedOnCompletion = NO;
  35. moveAnimation.fillMode = kCAFillModeForwards;
  36. moveAnimation.repeatCount = 1;
  37. moveAnimation.delegate = self;
  38. [moveAnimation setValue:@"moveAnimation1" forKey:@"moveAnimation1"];
  39. [_car_body.layer addAnimation:moveAnimation forKey:@"moveAnimation1"];
  40. });
  41. }
  42. else if([[anim valueForKey:@"moveAnimation1"] isEqualToString:@"moveAnimation1"])
  43. {
  44. [_car_body setHidden:YES];
  45. if (_delegate && [_delegate respondsToSelector:@selector(lambohiniAnimationFinished)])
  46. {
  47. [_delegate lambohiniAnimationFinished];
  48. [self.view removeFromSuperview];
  49. }
  50. }
  51. }
  52. #pragma mark - 平移动画
  53. - (void)do_move:(UIView *)view
  54. {
  55. CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
  56. moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake([[UIScreen mainScreen] bounds].size.width / 2.0f, [[UIScreen mainScreen] bounds].size.height / 2.0f)];
  57. moveAnimation.duration = 3.0f;
  58. moveAnimation.removedOnCompletion = NO;
  59. moveAnimation.repeatCount = 1;
  60. moveAnimation.delegate = self;
  61. moveAnimation.fillMode = kCAFillModeForwards;
  62. [moveAnimation setValue:@"moveAnimation" forKey:@"moveAnimation"];
  63. [view.layer addAnimation:moveAnimation forKey:@"moveAnimation"];
  64. }
  65. #pragma mark - 旋转动画
  66. - (void)do_rotation:(UIView *)view
  67. {
  68. CABasicAnimation* rotationAnimation;
  69. rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  70. rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 1.0];
  71. rotationAnimation.duration = 0.3f;
  72. rotationAnimation.cumulative = YES;
  73. rotationAnimation.repeatCount = INT_MAX;
  74. [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
  75. }
  76. @end