iOS使用[核心動畫]和[定時器]兩種方式實現【呼吸燈動畫】(仿蘑菇街價格標籤)

最近公司需求作個相似小紅書的標籤呼吸燈動畫,通過一段時間研究使用兩種方式實現了該效果…php

呼吸燈效果圖

第一種方式使用定時器加 UIView動畫,核心方法以下

 
  1. -(void)begigFlashAnimation {
  2. // 縮放 + 透明度動畫
  3. self.flashView.transform = CGAffineTransformMakeScale(0.1, 0.1);
  4. [UIView animateWithDuration:3 animations:^{
  5. self.flashView.transform = CGAffineTransformMakeScale(1,1);
  6. self.flashView.alpha = 1.0;
  7. [UIView beginAnimations:@"flash" context:nil];
  8. [UIView setAnimationDuration:2];
  9. [UIView setAnimationCurve:UIViewAnimationCurveLinear];
  10. self.flashView.alpha = 0;
  11. [UIView commitAnimations];
  12. }];
  13. }

第二種方式使用核心動畫的動畫組,核心方法以下

 
  1. - (CAAnimationGroup *)groups {
  2. if (!_groups) {
  3. // 縮放動畫
  4. CABasicAnimation * scaleAnim = [CABasicAnimation animation];
  5. scaleAnim.keyPath = @"transform.scale";
  6. scaleAnim.fromValue = @0.1;
  7. scaleAnim.toValue = @1;
  8. scaleAnim.duration = 2;
  9. // 透明度動畫
  10. CABasicAnimation *opacityAnim=[CABasicAnimation animationWithKeyPath:@"opacity"];
  11. opacityAnim.fromValue= @1;
  12. opacityAnim.toValue= @0.1;
  13. opacityAnim.duration= 2;
  14. // 建立動畫組
  15. _groups =[CAAnimationGroup animation];
  16. _groups.animations = @[scaleAnim,opacityAnim];
  17. _groups.removedOnCompletion = NO;
  18. _groups.fillMode = kCAFillModeForwards;
  19. _groups.duration = 2;
  20. _groups.repeatCount = FLT_MAX;
  21. }
  22. return _groups;
  23. }

對比兩種方法,第一種方法須要使用定時器,第二個則不須要,不知道這樣是否第二個性能性對來講會好點呢?git

DEMO:https://github.com/Caiflower/XXTwinkleViewgithub

原文地址:http://bbs.520it.com/forum.php?mod=viewthread&tid=2492佈局

 

性能

以前寫了篇關於呼吸燈動畫的,有幾個朋友問我應用場景,恰好最近有用到,藉此來實際應用下,先看看效果圖;
字體

看了上面圖片相信能想到一些實際的應用場景了吧
這裏我已經將此控件簡單封裝了下,動畫

你能夠這麼用

 
  1. // 建立 並設置標題,顯示位置
  2. self.markView = [XXMarkTwinkleView markViewWithTitle:@"韓式波波頭" showInRight: YES];
  3. // 寬度不用傳,內部自適應了,若是對字體沒有太大要求,高度其實也能夠在內部固定
  4. self.markView.frame = CGRectMake(230, 320, 0, 30);
  5. // 設置文字顏色
  6. self.markView.textColor = [UIColor redColor];
  7. [self.view addSubview:self.markView];

也能夠這麼用

 
  1. // 快讀建立一個呼吸燈view
  2. XXTwinkleView *twinkleView = [[XXTwinkleView alloc]initWithColor:[UIColor redColor] edgeColor:[UIColor whiteColor] circleWidth:8 edgeWidth:2];
  3. // 根據呼吸燈view建立 標籤
  4. XXMarkTwinkleView *markView1 = [[XXMarkTwinkleView alloc]initWithTwinkleView:self.twinkleView showInRight:NO];
  5. // 設置標題
  6. markView1.title = @"波波頭";
  7. // 寬度自適應不須要傳寬度
  8. markView1.frame = CGRectMake(120, 360, 0, 30);
  9. [self.view addSubview:markView1];

並無啥難點就作了個自適應寬度,只須要設置呼吸燈控件的位置,內部會根據標籤顯示在左邊仍是右邊,後臺返回呼吸燈控件的位置,咱們根據呼吸燈的位置來建立標籤,因此外面設置frame中的x,y應該是呼吸燈控件的中心點,因此這裏須要注意的是,如何在內部修改控件的位置,具體方法以下

 
  1. - (void)layoutSubviews {
  2. [super layoutSubviews];
  3. // 下移一半
  4. CGRect tmpBounds = self.bounds;
  5. tmpBounds.origin.y -= self.cf_height * 0.5;
  6. self.bounds = tmpBounds;
  7. // 根據標籤顯示的位置,佈局子控件
  8. if (self.isShowInRight) {
  9. self.twinkleView.frame = CGRectMake(-kTwinkleWidth * 0.5, -kTwinkleWidth * 0.5 , kTwinkleWidth, kTwinkleWidth);
  10. self.tagLable.frame = CGRectMake(self.twinkleView.cf_maxX + kContentMargin, -self.cf_height * 0.5 , self.tagLable.cf_width + kLabelAdditionalWidth, self.cf_height);
  11. // 設置寬度
  12. self.cf_width = self.tagLable.cf_maxX;
  13. }else {
  14.  
  15. self.tagLable.frame = CGRectMake(0, -self.cf_height * 0.5 , self.tagLable.cf_width + kLabelAdditionalWidth, self.cf_height);
  16. self.twinkleView.frame = CGRectMake(self.tagLable.cf_maxX + kContentMargin, -kTwinkleWidth * 0.5 , kTwinkleWidth, kTwinkleWidth);
  17. // 計算寬度
  18. CGFloat width = self.twinkleView.cf_minX + kTwinkleWidth * 0.5;
  19. // 修改x值
  20. self.cf_x = self.cf_x - width;
  21. // 設置寬度
  22. self.cf_width = width ;
  23. }
  24. // 設置圓角半徑
  25. self.tagLable.layer.cornerRadius = self.cf_height * 0.5;
  26. }

具體效果請看 https://github.com/Caiflower/XXTwinkleViewspa

 

原文地址: http://bbs.520it.com/forum.php?mod=viewthread&tid=2516.net

相關文章
相關標籤/搜索