From 29bb728666f02bd019cddb61f05c68527ab1726f Mon Sep 17 00:00:00 2001 From: chatwyn Date: Sat, 30 Jan 2016 13:56:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SXWaveAnimate/SXWaveView.h | 2 + SXWaveAnimate/SXWaveView.m | 73 ++++++++++++------- SXWaveAnimate/images/SXWaveCell.xib | 6 +- .../SXWaveAnimate/SXViewController.m | 2 +- 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/SXWaveAnimate/SXWaveView.h b/SXWaveAnimate/SXWaveView.h index b27445f..35496b6 100644 --- a/SXWaveAnimate/SXWaveView.h +++ b/SXWaveAnimate/SXWaveView.h @@ -36,5 +36,7 @@ - (void)setPrecent:(int)precent description:(NSString *)description textColor:(UIColor *)tcolor bgColor:(UIColor *)bColor alpha:(CGFloat)alpha clips:(BOOL)clips; +- (void)setPrecent:(int)precent description:(NSString *)description textColor:(UIColor *)tcolor bgColor:(UIColor *)bColor alpha:(CGFloat)alpha clips:(BOOL)clips endless:(BOOL)endless; + @end diff --git a/SXWaveAnimate/SXWaveView.m b/SXWaveAnimate/SXWaveView.m index 6952688..d5acf0f 100644 --- a/SXWaveAnimate/SXWaveView.m +++ b/SXWaveAnimate/SXWaveView.m @@ -29,14 +29,14 @@ - (instancetype)initWithFrame:(CGRect)frame UIImageView *rotateImg = [[UIImageView alloc]initWithFrame:frame]; rotateImg.contentMode = UIViewContentModeScaleAspectFit; rotateImg.image = [UIImage imageNamed:@"fb_rotation.png"]; + UIView * leftView = [[UIView alloc]init]; - [bgView addSubview:leftView]; leftView.width = bgView.width - W/12.5; leftView.height = bgView.height - W/12.5; leftView.centerx = bgView.centerx; leftView.centery = bgView.centery; - + leftView.top = W/25; [bgView addSubview:rotateImg]; @@ -52,7 +52,7 @@ - (instancetype)initWithFrame:(CGRect)frame avgScoreLbl.text = @"56%"; avgScoreLbl.textAlignment = NSTextAlignmentCenter; - + UILabel *descriptionLbl = [[UILabel alloc]init]; descriptionLbl.width = W / 2; @@ -86,7 +86,7 @@ - (instancetype)initWithFrame:(CGRect)frame self.bigImg.top = W; self.bigImg.left = -5*W; - + } return self; } @@ -128,6 +128,11 @@ - (void)setPrecent:(int)precent description:(NSString *)description textColor:(U } +- (void)setPrecent:(int)precent description:(NSString *)description textColor:(UIColor *)tcolor bgColor:(UIColor *)bColor alpha:(CGFloat)alpha clips:(BOOL)clips endless:(BOOL)endless{ + [self setPrecent:precent description:description textColor:tcolor bgColor:bColor alpha:alpha clips:clips]; + self.endless = endless; +} + - (void)setClips:(BOOL)clips { _clips = clips; @@ -178,45 +183,59 @@ - (void)addAnimateWithType:(int)type transformRoate.repeatCount = self.isEndless == YES ? MAXFLOAT : 2; [self.rotateImg.layer addAnimation:transformRoate forKey:viewRotationKey]; - if (type == 0) { - CGFloat avgScore = self.precent; - [UIView animateWithDuration:4.0 animations:^{ - self.bigImg.top = W - ((avgScore/100.0) * W); + CGFloat avgScore = self.precent; + if (self.endless) { +// self.leftView.clipsToBounds = false; + } + __weak typeof(self)weakSelf = self; + void(^beginAnimation)(NSTimeInterval)= ^(NSTimeInterval animationTime) { + + [UIView animateWithDuration:animationTime animations:^{ + weakSelf.bigImg.top = W - ((avgScore/100.0) * W); if (avgScore == 100) { - self.bigImg.top = -20; + weakSelf.bigImg.top = -20; } self.bigImg.left = 0; + } completion:^(BOOL finished) { + + if (weakSelf.endless) { + CAKeyframeAnimation * moveAction = [CAKeyframeAnimation animationWithKeyPath:@"position.x"]; + + moveAction.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:self.bigImg.layer.position.x],[NSNumber numberWithFloat:-2*W],nil]; + moveAction.autoreverses = true; + moveAction.duration = 4; + moveAction.repeatCount = MAXFLOAT; + [weakSelf.bigImg.layer addAnimation:moveAction forKey:viewMoveKey]; + } + }]; + }; + + if (type == 0) { + + beginAnimation(4.0); }else if (type == 1){ - CGFloat avgScore = self.precent; + self.bigImg.top = -20; - self.bigImg.left = -5*W; - [UIView animateWithDuration:4.0 animations:^{ - self.bigImg.top = W - ((avgScore/100.0) * W); - if (avgScore == 100) { - self.bigImg.top = -20; - } - self.bigImg.left = 0; - }]; + beginAnimation(4.0); + }else if (type == 2){ - CGFloat avgScore = self.precent; [UIView animateWithDuration:2.0 animations:^{ self.bigImg.top = -20; self.bigImg.left = -3*W; } completion:^(BOOL finished) { - [UIView animateWithDuration:2.0 animations:^{ - self.bigImg.top = W - ((avgScore/100.0) * W); - if (avgScore == 100) { - self.bigImg.top = -20; - } - self.bigImg.left = 0; - }]; + beginAnimation(2.0); }]; } + + + + } -(void)dealloc{ - [self.bigImg.layer removeAnimationForKey:viewRotationKey]; + + [self.rotateImg.layer removeAnimationForKey:viewRotationKey]; [self.bigImg.layer removeAnimationForKey:viewMoveKey]; } diff --git a/SXWaveAnimate/images/SXWaveCell.xib b/SXWaveAnimate/images/SXWaveCell.xib index 7495389..7628e4d 100644 --- a/SXWaveAnimate/images/SXWaveCell.xib +++ b/SXWaveAnimate/images/SXWaveCell.xib @@ -1,8 +1,8 @@ - + - + @@ -11,7 +11,7 @@ - + diff --git a/SXWaveAnimateDemo/SXWaveAnimate/SXViewController.m b/SXWaveAnimateDemo/SXWaveAnimate/SXViewController.m index 12b6f26..124174e 100644 --- a/SXWaveAnimateDemo/SXWaveAnimate/SXViewController.m +++ b/SXWaveAnimateDemo/SXWaveAnimate/SXViewController.m @@ -47,7 +47,7 @@ - (void)viewDidLoad { SXWaveView *animateView1 = [[SXWaveView alloc]initWithFrame:CGRectMake(0+MARGIN, 100+MARGIN,SIDES, SIDES)]; [self.view addSubview:animateView1]; self.animateView1 = animateView1; - [self.animateView1 setPrecent:self.precent description:@"董铂然" textColor:[UIColor orangeColor] bgColor:COLOR(31, 187, 170, 1) alpha:1 clips:NO]; + [self.animateView1 setPrecent:self.precent description:@"董铂然" textColor:[UIColor orangeColor] bgColor:COLOR(31, 187, 170, 1) alpha:1 clips:NO endless:true]; // ------步骤3 SXWaveView *animateView2 = [[SXWaveView alloc]initWithFrame:CGRectMake(0+MARGIN*2+SIDES, 100+MARGIN,SIDES, SIDES)]; From 67fcf0cb9e4d02399591ad344ba450631142cb30 Mon Sep 17 00:00:00 2001 From: ChatWyn Date: Sat, 30 Jan 2016 13:58:43 +0800 Subject: [PATCH 2/2] Update SXWaveView.m --- SXWaveAnimate/SXWaveView.m | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SXWaveAnimate/SXWaveView.m b/SXWaveAnimate/SXWaveView.m index d5acf0f..f54e77a 100644 --- a/SXWaveAnimate/SXWaveView.m +++ b/SXWaveAnimate/SXWaveView.m @@ -184,9 +184,7 @@ - (void)addAnimateWithType:(int)type [self.rotateImg.layer addAnimation:transformRoate forKey:viewRotationKey]; CGFloat avgScore = self.precent; - if (self.endless) { -// self.leftView.clipsToBounds = false; - } + __weak typeof(self)weakSelf = self; void(^beginAnimation)(NSTimeInterval)= ^(NSTimeInterval animationTime) {