Dynamically draw curves on iOS

Source: Internet
Author: User

Dynamically draw curves on iOS

A function needs to be completed recently, that is, to dynamically draw a curve on the screen. This curve can be used to describe the changes of data within a certain period of time. This is probably the following effect.

How can this effect be achieved? These three classes are required: UIBezierPath CAShapeLayer and CABasicAnimation. UIBezierPath is used to draw the corresponding curve Path, CAShapeLayer is used to provide the display position for the Path, and add the animation created by CABasicAnimation to the Path.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Environment + c + Environment/environment = "brush: java;"> UIBezierPath * path = [UIBezierPath bezierPath]; [path moveToPoint: CGPointMake (0.0, 20.0)]; [path addLineToPoint: CGPointMake (120.0, 500.0)]; [path addLineToPoint: CGPointMake (220, 0)]; [path addLineToPoint: CGPointMake (310, 40)]; [path addLineToPoint: CGPointMake (SCREEN_WIDTH, 110)];

Then we create our own attributes for CAShapeLayer and assign our path to it. Without this assignment, the layer won't be able to draw this effect for us, and it is also an incomplete layer.


CAShapeLayer *pathLayer = [CAShapeLayer layer];    pathLayer.frame = self.view.bounds;    pathLayer.path = path.CGPath;    pathLayer.strokeColor = [[UIColor redColor] CGColor];    pathLayer.fillColor = nil;    pathLayer.lineWidth = 2.0f;    pathLayer.lineJoin = kCALineJoinBevel;        [self.view.layer addSublayer:pathLayer];

Finally, we assign the animation to our layer. In our animation effect, we can change some of the parameters to control its painting effect.

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];    pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];    pathAnimation.duration = 2.0;    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];

Now we can run the code to get the effect in the previous image ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.