Skip to content

Instantly share code, notes, and snippets.

@jparishy
Created July 29, 2018 01:32
Show Gist options
  • Save jparishy/9d852c2478304ae20e1c6fcc85b3fe22 to your computer and use it in GitHub Desktop.
Save jparishy/9d852c2478304ae20e1c6fcc85b3fe22 to your computer and use it in GitHub Desktop.
//
// ViewController.m
// Path
//
// Created by Julius Parishy on 7/28/18.
// Copyright © 2018 JP. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *containerView;
@property (nonatomic, assign) CGFloat angleOffset;
@property (nonatomic, weak) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) CADisplayLink *displayLink;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupCurveLayer];
self.angleOffset = 0;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updatePath)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setupCurveLayer
{
if (self.shapeLayer)
{
[self.shapeLayer removeFromSuperlayer];
}
CAShapeLayer *layer = [CAShapeLayer layer];
layer.fillColor = [[UIColor purpleColor] CGColor];
[self.containerView.layer addSublayer:layer];
self.shapeLayer = layer;
}
- (void)updatePath
{
self.angleOffset += (M_PI / 2.0 / 20.0);
CGFloat width = CGRectGetWidth(self.containerView.bounds);
CGFloat midY = CGRectGetMidY(self.containerView.bounds);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, midY)];
NSInteger steps = 300;
NSInteger numCurves = 5;
CGFloat amplitude = midY;
for (NSInteger i = 0; i < steps; ++i)
{
CGFloat stepAngle = (M_PI / (steps / numCurves)) * i;
CGFloat delta = sinf(self.angleOffset + stepAngle);
CGPoint point = CGPointZero;
point.x = (width / steps) * i;
point.y = midY + (delta * amplitude);
[path addLineToPoint:point];
}
[path addLineToPoint:CGPointMake(width, midY)];
self.shapeLayer.frame = self.containerView.bounds;
self.shapeLayer.path = [path CGPath];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment