A Free iOS Instagram Story Button

Oscar de la Hera
AR Tips And Tricks
Published in
2 min readJul 12, 2018

Copy the class file below and instantiate it in your view controller using the following line

self.view.addSubview(instagramCameraButton());

Save this class into a Swift File:

class instagramCameraButton: UIButton {private let CameraButtonDimension:CGFloat = screenWidth*0.2;private let whiteCircle:UIView = UIView();private var whiteCircleSize:CGFloat {return self.CameraButtonDimension*3/4;}private let videoCameraProgressArc:CAShapeLayer = CAShapeLayer();private var videoCameraProgressArcMargin:CGFloat {return self.CameraButtonDimension/20;}private let circleGrowthTimeInterval:TimeInterval = 0.3;private var videoCameraTimerStartTime:Date = Date();private let maximumTimeForAPhoto:TimeInterval = 0.2;init() {// Initialize the view to fit the responsive design presented in the deck and sketch file// Instatiate Camera Circlesuper.init(frame: CGRect(x: (screenWidth - self.CameraButtonDimension)/2, y: (screenHeight*0.9-self.CameraButtonDimension), width: self.CameraButtonDimension, height: self.CameraButtonDimension));self.backgroundColor = UIColor.white.withAlphaComponent(0.6);self.layer.cornerRadius = CameraButtonDimension/2;// Instatiate Camera Circleself.whiteCircle.backgroundColor = UIColor.white;self.whiteCircle.layer.cornerRadius = whiteCircleSize/2;self.whiteCircle.frame = CGRect(x: (self.frame.width-whiteCircleSize)/2, y: (self.frame.width-whiteCircleSize)/2, width: whiteCircleSize, height: whiteCircleSize);self.addSubview(self.whiteCircle);// Create the video camera progress arcself.videoCameraProgressArc.opacity = 0;self.videoCameraProgressArc.path = UIBezierPath(ovalIn: CGRect(x: self.videoCameraProgressArcMargin, y: self.videoCameraProgressArcMargin, width: self.frame.width - self.videoCameraProgressArcMargin*2, height: self.frame.width - self.videoCameraProgressArcMargin*2)).cgPath;self.videoCameraProgressArc.lineWidth = 5.0;self.videoCameraProgressArc.strokeColor = UIColor.black.cgColor;self.videoCameraProgressArc.fillColor = UIColor.clear.cgColor;// Rotate 90 degrees anti clockwiseself.videoCameraProgressArc.transform = CATransform3DMakeRotation(CGFloat(-Double.pi/2), 0,0,1);self.videoCameraProgressArc.position = CGPoint(x: 0, y: self.frame.height);self.layer.addSublayer(self.videoCameraProgressArc);}override init(frame: CGRect) {super.init(frame: frame);}required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder);}// BUTTON FUNCTIONALITYvar count:Int = 0;override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {//        print("START");self.count = 0;self.videoCameraTimerStartTime = Date();UIView.animate(withDuration: circleGrowthTimeInterval, animations: {self.frame = CGRect(x: (screenWidth - self.CameraButtonDimension*1.5)/2, y: (screenHeight*0.9-self.CameraButtonDimension*1.25), width: self.CameraButtonDimension*1.5, height: self.CameraButtonDimension*1.5);self.layer.cornerRadius = self.CameraButtonDimension*1.5/2;self.whiteCircle.frame = CGRect(x: (self.frame.width-self.whiteCircleSize)/2, y: (self.frame.width-self.whiteCircleSize)/2, width: self.whiteCircleSize, height: self.whiteCircleSize);self.videoCameraProgressArc.path = UIBezierPath(ovalIn: CGRect(x: self.videoCameraProgressArcMargin, y: self.videoCameraProgressArcMargin, width: self.frame.width - self.videoCameraProgressArcMargin*2, height: self.frame.width - self.videoCameraProgressArcMargin*2)).cgPath;self.videoCameraProgressArc.opacity = 1;self.videoCameraProgressArc.transform = CATransform3DMakeRotation(CGFloat(-Double.pi/2), 0,0,1);self.videoCameraProgressArc.position = CGPoint(x: 0, y: self.frame.height);self.animateVideoRing();});}override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {//        print("MOVED");// CAPTURE SCENE SNAPSself.count += 1;print("TIME SINCE CLICK: ", Date().timeIntervalSince(self.videoCameraTimerStartTime));print("COUNT : ", self.count);}override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {self.videoCameraProgressArc.removeAllAnimations();self.videoCameraProgressArc.opacity = 0;if( Date().timeIntervalSince(self.videoCameraTimerStartTime) < self.maximumTimeForAPhoto ){print("PHOTO");}else {print("VIDEO");}UIView.animate(withDuration: circleGrowthTimeInterval, animations: {self.frame = CGRect(x: (screenWidth - self.CameraButtonDimension)/2, y: (screenHeight*0.9-self.CameraButtonDimension), width: self.CameraButtonDimension, height: self.CameraButtonDimension);self.layer.cornerRadius = self.CameraButtonDimension/2;self.whiteCircle.frame = CGRect(x: (self.frame.width-self.whiteCircleSize)/2, y: (self.frame.width-self.whiteCircleSize)/2, width: self.whiteCircleSize, height: self.whiteCircleSize);});}// MARK :  ANIMATION FUNCTIONALITYfunc animateVideoRing(){// Set the Initial Stroke Stateself.videoCameraProgressArc.strokeStart = 0self.videoCameraProgressArc.strokeEnd = 0CATransaction.begin();CATransaction.setAnimationDuration(10);CATransaction.setDisableActions(true);CATransaction.setCompletionBlock {self.videoRingAnimationDidFinish();}// Set animation end statelet start = CABasicAnimation(keyPath: "strokeStart")start.toValue = 0let end = CABasicAnimation(keyPath: "strokeEnd")end.toValue = 1// Play Animation Repetitivelylet group = CAAnimationGroup()group.animations = [start, end];group.duration = 10group.autoreverses = false;group.repeatCount = 0 // repeat 0 timesgroup.isRemovedOnCompletion = true;self.videoCameraProgressArc.add(group, forKey: nil)CATransaction.commit();}func videoRingAnimationDidFinish(){print("FINISHED");}}

Oscar

--

--

Oscar de la Hera
AR Tips And Tricks

Oscar is an award-winning Spanish Inventor whose work impacts lives through brands that include Nike, MoMA and Samsung.