How do I reset a CABasicAnimation? - ios

I have an animation which is a bar, which travels across the screen according to a timer which shortens each time something is tapped. My problem is that the animation only plays once. I try calling the animation to reset and begin the bar again, but nothing happens. Here is some code:
-(IBAction)targetTapped:(id)sender {
[Time invalidate];
targX = arc4random() %619;
targY = arc4random() %925;
NSLog([NSString stringWithFormat:#"X = %li", (long)targX]);
NSLog([NSString stringWithFormat:#"Y = %li", (long)targY]);
Target.center = CGPointMake(targX, targY);
Score = Score + 1;
if (Score >= 225) {
timeMax = 0.5;
}
else {
timeMax = 5 - (Score * 0.02);
}
Time = [NSTimer scheduledTimerWithTimeInterval:timeMax target:self selector:#selector(Lose) userInfo:nil repeats:NO];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0.0,895.0)];
[path addLineToPoint:CGPointMake(768.0, 895.0)];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.view.bounds;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [[UIColor blueColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 5.0f;
pathLayer.lineJoin = kCALineJoinBevel;
[self.view.layer addSublayer:pathLayer];
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:#"strokeEnd"];
pathAnimation.duration = timeMax;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[pathLayer animationForKey:#"strokeEnd"];
[pathLayer addAnimation:pathAnimation forKey:#"strokeEnd"];
}

You can create the pathLayer once:
[self.pathLayer removeAnimationForKey:#"strokeEnd"];
if (self.pathLayer == nil) {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0.0,895.0)];
[path addLineToPoint:CGPointMake(768.0, 895.0)];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.view.bounds;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [[UIColor blueColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 5.0f;
pathLayer.lineJoin = kCALineJoinBevel;
[self.view.layer addSublayer:pathLayer];
self.pathLayer = pathLayer;
}
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:#"strokeEnd"];
pathAnimation.duration = timeMax;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[self.pathLayer addAnimation:pathAnimation forKey:#"strokeEnd"];

Related

iOS Obj-c gradient background transition

I'd like to gradually change the background gradient from
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.blackColor CGColor],(id)[UIColor.blueColor CGColor], nil];
to
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.grayColor CGColor],(id)[UIColor.cyanColor CGColor], nil];
[EDIT] My current code is:
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.blackColor CGColor],(id)[UIColor.blueColor CGColor], nil];
NSArray *fromColors = gradient.colors;
NSArray *toColors =[NSArray arrayWithObjects:(id)[UIColor.grayColor CGColor],(id)[UIColor.cyanColor CGColor], nil];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: #"colors"];
animation.fromValue = fromColors;
animation.toValue = toColors;
animation.duration = 3.00;
animation.removedOnCompletion = false;
animation.autoreverses = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
if (![gradient.animationKeys containsObject:#"colors"]) {
[gradient addAnimation:animation forKey:#"colors"];
}
[self.background.layer addSublayer:gradient];
But it doesn't do anything. Can someone help me please?
I don't think "colors" are supported in CABasicAnimation.keypath.But how to meet the requirement, I still can't think of the answer.https://stackoverflow.com/questions/5459673/how-can-i-know-the-values-in-cabasicanimation-keypath

How to Add GIf image as layer on view?

I am working on a project in which A video is recorded.When recording is finished, I play video using AVPlayer adding layer to view. Now I want a gif image as adding layer to that view.
I successfully Add gif image as layer, but image is not animated. It is like a static image.I use Library for GIF image.
My code is below
self.avPlayer = [AVPlayer playerWithURL:self.urlstring];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.preview_view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.preview_view.layer addSublayer:videoLayer];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"02" withExtension:#"gif"];
CALayer *layer = [[CALayer alloc]init];
layer.frame = self.img_gif.bounds;
layer.contents = (id) [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]].CGImage;
[self.preview_view.layer addSublayer:layer];
Please help me with it
Thank you
I have successfully done it. I added my GIF image to layer.
- (CAKeyframeAnimation *)createGIFAnimation:(NSData *)data{
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)(data), nil);
int frameCount =(int) CGImageSourceGetCount(src);
// Total loop time
float time = 0;
// Arrays
NSMutableArray *framesArray = [NSMutableArray array];
NSMutableArray *tempTimesArray = [NSMutableArray array];
// Loop
for (int i = 0; i < frameCount; i++){
// Frame default duration
float frameDuration = 0.1f;
// Frame duration
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(src,i,nil);
NSDictionary *frameProperties = (__bridge NSDictionary*)cfFrameProperties;
NSDictionary *gifProperties = frameProperties[(NSString*)kCGImagePropertyGIFDictionary];
// Use kCGImagePropertyGIFUnclampedDelayTime or kCGImagePropertyGIFDelayTime
NSNumber *delayTimeUnclampedProp = gifProperties[(NSString*)kCGImagePropertyGIFUnclampedDelayTime];
if(delayTimeUnclampedProp) {
frameDuration = [delayTimeUnclampedProp floatValue];
} else {
NSNumber *delayTimeProp = gifProperties[(NSString*)kCGImagePropertyGIFDelayTime];
if(delayTimeProp) {
frameDuration = [delayTimeProp floatValue];
}
}
// Make sure its not too small
if (frameDuration < 0.011f){
frameDuration = 0.100f;
}
[tempTimesArray addObject:[NSNumber numberWithFloat:frameDuration]];
// Release
CFRelease(cfFrameProperties);
// Add frame to array of frames
CGImageRef frame = CGImageSourceCreateImageAtIndex(src, i, nil);
[framesArray addObject:(__bridge id)(frame)];
// Compile total loop time
time = time + frameDuration;
}
NSMutableArray *timesArray = [NSMutableArray array];
float base = 0;
for (NSNumber* duration in tempTimesArray){
//duration = [NSNumber numberWithFloat:(duration.floatValue/time) + base];
base = base + (duration.floatValue/time);
[timesArray addObject:[NSNumber numberWithFloat:base]];
}
// Create animation
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:#"contents"];
animation.duration = time;
animation.repeatCount = HUGE_VALF;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.values = framesArray;
animation.keyTimes = timesArray;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.calculationMode = kCAAnimationDiscrete;
return animation;
}
To add image in layer of view write below code
CALayer *layer = [CALayer layer];
layer.frame = self.preview_view.bounds;
CAKeyframeAnimation *animation = [self createGIFAnimation:[NSData dataWithContentsOfURL:url]];
[layer addAnimation:animation forKey:#"contents"];
["YOUR VIEW".layer addSublayer:layer];
Try this with your image view.
CALayer *layer = [CALayer layer];
layer.backgroundColor = [[UIColor whiteColor] CGColor];
layer.contents = (id) [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]].CGImage;
NSURL *url = [[NSBundle mainBundle] URLForResource:#"02" withExtension:#"gif"];
[[self.view layer] addSublayer:layer];
[layer setNeedsDisplay];
[self.view addSubview: animateImageView];

Draw letter(text) with UIBezierPath single line

I am using this and this for reference. I am new for using UIBezierPath, and i want to draw path on character(letter) code i am using is as follow.
CGMutablePathRef letters = CGPathCreateMutable();
CTFontRef font = CTFontCreateWithName(CFSTR("Courier New"), 200.0f, NULL);
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:CFBridgingRelease(font), kCTFontAttributeName,nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:#"B"
attributes:attrs];
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
CFArrayRef runArray = CTLineGetGlyphRuns(line);
// for each RUN
for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
{
// Get FONT for this run
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
// for each GLYPH in run
for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
{
// get Glyph & Glyph-data
CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
CGGlyph glyph;
CGPoint position;
CTRunGetGlyphs(run, thisGlyphRange, &glyph);
CTRunGetPositions(run, thisGlyphRange, &position);
// Get PATH of outline
{
CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
CGPathAddPath(letters, &t, letter);
CGPathRelease(letter);
}
}
}
CFRelease(line);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path appendPath:[UIBezierPath bezierPathWithCGPath:letters]];
CGPathRelease(letters);
CFRelease(font);
NSLog(#"==> %#",path);
// Creates layer
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = drawingView.bounds;
pathLayer.bounds = CGPathGetBoundingBox(path.CGPath);
//pathLayer.backgroundColor = [[UIColor yellowColor] CGColor];
pathLayer.geometryFlipped = NO;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [[UIColor blackColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 3.0f;
pathLayer.lineJoin = kCALineJoinBevel;
[drawingView.layer addSublayer:pathLayer];
self.pathLayer = pathLayer;
// Add pointer
UIImage *penImage = [UIImage imageNamed:#"hand.png"];
CALayer *penLayer = [CALayer layer];
penLayer.contents = (id)penImage.CGImage;
penLayer.anchorPoint = CGPointMake(0.35f, 0.98f);
penLayer.frame = CGRectMake(0.0f, 0.0f, penImage.size.width, penImage.size.height);
[pathLayer addSublayer:penLayer];
self.penLayer = penLayer;
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:#"strokeEnd"];
pathAnimation.duration = 5.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[self.pathLayer addAnimation:pathAnimation forKey:#"strokeEnd"];
CAKeyframeAnimation *penAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
penAnimation.duration = 5.0;
penAnimation.path = self.pathLayer.path;
penAnimation.calculationMode = kCAAnimationPaced;
penAnimation.delegate = self;
[self.penLayer addAnimation:penAnimation forKey:#"position"];
This code draws latter latter in double line like the image below
instead i want to draw latter in single line like the image below
and the hand should draw the lines on centre of the font and not the border.

UIView Animating a 2D Bouncing Ball (Squash & Stretch) in iOS

Looking for Animating a view like Bouncing Ball using Squash & Stretch, in cocoa using core animation...
1 - Bouncing Ball Animation
;;;;
CABasicAnimation *animMoveUp = [CABasicAnimation animationWithKeyPath:#"position"];
animMoveUp.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animMoveUp.fromValue = [NSValue valueWithCGPoint:_bottom];
animMoveUp.toValue = [NSValue valueWithCGPoint:_top];
CABasicAnimation *scaleAnim1 = [CABasicAnimation animationWithKeyPath:#"transform"];
scaleAnim1.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.0, 1.0)];
scaleAnim1.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
CABasicAnimation *bounceAnim1 = [CABasicAnimation animationWithKeyPath:#"transform"];
bounceAnim1.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
bounceAnim1.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.0, 1.0, 1.0)];
CABasicAnimation *animMoveDown = [CABasicAnimation animationWithKeyPath:#"position"];
animMoveDown.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animMoveDown.fromValue = [NSValue valueWithCGPoint:_top];
animMoveDown.toValue = [NSValue valueWithCGPoint:_bottom];
CABasicAnimation *scaleAnim2 = [CABasicAnimation animationWithKeyPath:#"transform"];
scaleAnim2.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.0, 1.0)];
scaleAnim2.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
CABasicAnimation *bounceAnim2 = [CABasicAnimation animationWithKeyPath:#"transform"];
bounceAnim2.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
bounceAnim2.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.0, 1.0, 1.0)];
CABasicAnimation *animMoveUp1 = [CABasicAnimation animationWithKeyPath:#"position"];
animMoveUp1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animMoveUp1.fromValue = [NSValue valueWithCGPoint:_bottom];
animMoveUp1.toValue = [NSValue valueWithCGPoint:_top];
CABasicAnimation *scaleAnim3 = [CABasicAnimation animationWithKeyPath:#"transform"];
scaleAnim3.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.0, 1.0)];
scaleAnim3.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
CABasicAnimation *bounceAnim3 = [CABasicAnimation animationWithKeyPath:#"transform"];
bounceAnim3.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
bounceAnim3.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.0, 1.0, 1.0)];
CABasicAnimation *animMoveDown2 = [CABasicAnimation animationWithKeyPath:#"position"];
animMoveDown2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animMoveDown2.fromValue = [NSValue valueWithCGPoint:_top];
animMoveDown2.toValue = [NSValue valueWithCGPoint:_bottom];
CABasicAnimation *scaleAnim4 = [CABasicAnimation animationWithKeyPath:#"transform"];
scaleAnim4.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.0, 1.0)];
scaleAnim4.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
CABasicAnimation *bounceAnim4 = [CABasicAnimation animationWithKeyPath:#"transform"];
bounceAnim4.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
bounceAnim4.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.0, 1.0, 1.0)];
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = #[animMoveUp, scaleAnim1, bounceAnim1,
animMoveDown, scaleAnim2, bounceAnim2,
animMoveUp1, scaleAnim3, bounceAnim3,
animMoveDown2, scaleAnim4, bounceAnim4];
animGroup.duration = 2.5;
animGroup.delegate = self;
animGroup.fillMode = kCAFillModeForwards;
animGroup.removedOnCompletion = NO;
[_imgView.layer addAnimation:animGroup forKey:nil];
The following code will generate the animation above:
CAKeyframeAnimation *trans = [CAKeyframeAnimation animationWithKeyPath:#"transform.translation.y"];
NSArray *values = #[#(-200),#(20),#(0)];
trans.values = values;
NSArray *times = #[#(0.0),#(0.85),#(1)];
trans.keyTimes = times;
trans.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
trans.autoreverses = YES;
trans.duration = 1.0;
trans.repeatCount = INFINITY;
CAKeyframeAnimation *scaleXAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale.x"];
NSArray *scaleXValues = #[#(0.75),#(0.75),#(1)];
scaleXAnimation.values = scaleXValues;
NSArray *scaleXtimes = #[#(0.0),#(0.85),#(1)];
scaleXAnimation.keyTimes = scaleXtimes;
scaleXAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
scaleXAnimation.autoreverses = YES;
scaleXAnimation.duration = 1.0;
scaleXAnimation.repeatCount = INFINITY;
CAKeyframeAnimation *scaleYAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale.y"];
NSArray *scaleYValues = #[#(0.75),#(1),#(0.85)];
scaleYAnimation.values = scaleYValues;
NSArray *scaleYtimes = #[#(0.1),#(0.5),#(1)];
scaleYAnimation.keyTimes = scaleYtimes;
scaleYAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
scaleYAnimation.autoreverses = YES;
scaleYAnimation.duration = 1.0;
scaleYAnimation.repeatCount = INFINITY;
[_ballView.layer addAnimation:scaleXAnimation forKey:#"scaleX"];
[_ballView.layer addAnimation:scaleYAnimation forKey:#"scaleY"];
[_ballView.layer addAnimation:trans forKey:#"trans"];

EXC_BAD_ACCESS drawing graph

I'm drawing graph and for that I use SHLineGraphView now after drawing one graph I need to remove that and dray again. in line
CGPathMoveToPoint(backgroundPath, NULL, _leftMarginToLeave, plot.xPoints[0].y);
I am getting get EXC_BAD_ACCESS Message.
this is my function. I add NSZombieEnabled YES in my scheme
- (void)drawPlot:(SHPlot *)plot {
NSDictionary *theme = plot.plotThemeAttributes;
CAShapeLayer *backgroundLayer = [CAShapeLayer layer];
backgroundLayer.frame = self.bounds;
backgroundLayer.fillColor = ((UIColor *)theme[kPlotFillColorKey]).CGColor;
backgroundLayer.backgroundColor = [UIColor clearColor].CGColor;
[backgroundLayer setStrokeColor:[UIColor clearColor].CGColor];
[backgroundLayer setLineWidth:((NSNumber *)theme[kPlotStrokeWidthKey]).intValue];
CGMutablePathRef backgroundPath = CGPathCreateMutable();
CAShapeLayer *circleLayer = [CAShapeLayer layer];
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 75, 75)];
circleLayer.path = circle.CGPath;
circleLayer.strokeColor = [UIColor orangeColor].CGColor;
circleLayer.fillColor = [UIColor whiteColor].CGColor;
circleLayer.shadowColor =[UIColor blackColor].CGColor;
circleLayer.lineWidth = 3.0;
[self.layer addSublayer:circleLayer];
CGMutablePathRef circlePath = CGPathCreateMutable();
CAShapeLayer *graphLayer = [CAShapeLayer layer];
graphLayer.frame = self.bounds;
graphLayer.fillColor = [UIColor clearColor].CGColor;
graphLayer.backgroundColor = [UIColor clearColor].CGColor;
[graphLayer setStrokeColor:((UIColor *)theme[kPlotStrokeColorKey]).CGColor];
[graphLayer setLineWidth:((NSNumber *)theme[kPlotStrokeWidthKey]).intValue];
CGMutablePathRef graphPath = CGPathCreateMutable();
double yRange = [_yAxisRange doubleValue];
double yIntervalValue = yRange / INTERVAL_COUNT;
[plot.plottingValues enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSDictionary *dic = (NSDictionary *)obj;
__block NSNumber *_key = nil;
__block NSNumber *_value = nil;
[dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
_key = (NSNumber *)key;
_value = (NSNumber *)obj;
}];
int xIndex = [self getIndexForValue:_key forPlot:plot];
double height = self.bounds.size.height - BOTTOM_MARGIN_TO_LEAVE;
double y = height - ((height / ([_yAxisRange doubleValue] + yIntervalValue)) * [_value doubleValue]);
(plot.xPoints[xIndex]).x = ceil((plot.xPoints[xIndex]).x);
(plot.xPoints[xIndex]).y = ceil(y);
}];
CGPathMoveToPoint(graphPath, NULL, _leftMarginToLeave, plot.xPoints[0].y);
CGPathMoveToPoint(backgroundPath, NULL, _leftMarginToLeave, plot.xPoints[0].y);
int count = _xAxisValues.count;
for(int i=0; i< count; i++){
CGPoint point = plot.xPoints[i];
CGPathAddLineToPoint(graphPath, NULL, point.x, point.y);
CGPathAddLineToPoint(backgroundPath, NULL, point.x, point.y);
CGPathAddEllipseInRect(circlePath, NULL, CGRectMake(point.x - 5, point.y - 5, 10, 10));
}
CGPathAddLineToPoint(backgroundPath, NULL, _leftMarginToLeave + PLOT_WIDTH, self.bounds.size.height - BOTTOM_MARGIN_TO_LEAVE);
CGPathAddLineToPoint(backgroundPath, NULL, _leftMarginToLeave, self.bounds.size.height - BOTTOM_MARGIN_TO_LEAVE);
CGPathCloseSubpath(backgroundPath);
backgroundLayer.path = backgroundPath;
graphLayer.path = graphPath;
circleLayer.path = circlePath;
CGPathRelease(backgroundPath);
CGPathRelease(circlePath);
CGPathRelease(graphPath);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"strokeEnd"];
animation.duration = 1;
animation.fromValue = #(0.0);
animation.toValue = #(1.0);
[graphLayer addAnimation:animation forKey:#"strokeEnd"];
backgroundLayer.zPosition = 0;
graphLayer.zPosition = 1;
circleLayer.zPosition = 2;
[self.layer addSublayer:graphLayer];
[self.layer addSublayer:circleLayer];
[self.layer addSublayer:backgroundLayer];
NSUInteger count2 = _xAxisValues.count;
for(int i=0; i< count2; i++){
CGPoint point = plot.xPoints[i];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor clearColor];
btn.tag = i;
btn.frame = CGRectMake(point.x - 10, point.y - 10, 40, 40);
[btn addTarget:self action:#selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
objc_setAssociatedObject(btn, kAssociatedPlotObject, plot, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self addSubview:btn];
}
}
Thanks

Resources