Water wave animation of iOS animation

Keywords: iOS github

  • In order to provide a good interactive effect for users, many app s usually add animation effect to achieve the goal. A good animation effect will often play the role of masterstroke.
  • Today, we will realize the water wave animation in IOS. First on the renderings:

  • It's a little complicated to see many effects of realizing water wave on the Internet. Today, simple animation is used to realize water wave effect
  • Main code
    - (void)addWaterware {
        
        //  Underline a static water wave effect
        CGFloat y = 50;
        CGFloat p = 15;//Set amplitude
        
        CGFloat w = self.frame.size.width;
        CGFloat h = self.frame.size.height;
        
        CGFloat h1 = h;
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        
        [path moveToPoint:CGPointMake(-w, y)];
        
        [path addCurveToPoint:CGPointMake(0, y) controlPoint1:CGPointMake(-w*2/3.f, y - p) controlPoint2:CGPointMake(-w/3.f, y+p)];
        [path addCurveToPoint:CGPointMake(w, y) controlPoint1:CGPointMake(w/3.f, y - p) controlPoint2:CGPointMake(w*2/3.f, y+p)];
        
        [path addLineToPoint:CGPointMake(w, h1)];
        [path addLineToPoint:CGPointMake(-w, h1)];
        [path addLineToPoint:CGPointMake(-w, y)];
       
        CAShapeLayer * layer = [CAShapeLayer new];
        layer.path = path.CGPath;
        layer.fillColor = [UIColor redColor].CGColor;
        layer.strokeColor = [UIColor orangeColor].CGColor;
        [self.layer addSublayer:layer];
        
        
        
        // Animate horizontal movement
        CGPoint start = layer.position;
        CGPoint end = layer.position;
        end.x =  end.x + w;
        
        CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        moveAnimation.fromValue = [NSValue valueWithCGPoint:start];
        moveAnimation.toValue = [NSValue valueWithCGPoint:end];
        moveAnimation.autoreverses = NO;
        moveAnimation.repeatCount = MAXFLOAT;
        moveAnimation.duration = 1.2;//Setting rate
        moveAnimation.removedOnCompletion = NO;
        
        [layer addAnimation:moveAnimation forKey:@"move"];
    }

demo download address: https://github.com/xiaoke2015/SGWaterwave

 


Posted by Stinger51 on Sat, 15 Feb 2020 08:47:17 -0800