码迷,mamicode.com
首页 > 其他好文 > 详细

打钩的动画

时间:2015-08-17 14:04:49      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:ios

打钩的动画

by 伍雪颖


技术分享


#import "CheckboxControl.h"

@implementation CheckboxControl {
   
NSInteger lineWidth;
   
UIColor *lineColor;
}

- (
void)awakeFromNib {
   
lineWidth = 5.0;
   
lineColor = [UIColor colorWithRed:0.078 green:0.196 blue:0.314 alpha:1.000];
}

- (
void)show {
   
_lineLayer = [self checkmarkLayerWithColor];
    [
self.layer addSublayer:_lineLayer];
   
_lineLayer.strokeStart = 0;
   
_lineLayer.strokeEnd = 0;
    [
CATransaction begin];
    [
CATransaction setCompletionBlock:^{
       
_lineLayer.strokeStart = 0;
       
_lineLayer.strokeEnd = 1;
    }];
    [
CATransaction commit];
}

- (
void)hide {
    [
CATransaction begin];
    [
CATransaction setCompletionBlock:^{
       
_lineLayer.strokeStart = 1;
       
_lineLayer.strokeEnd = 1;
    }];
    [
CATransaction commit];
}

- (
CAShapeLayer *)checkmarkLayerWithColor {
   
CAShapeLayer *ret = [CAShapeLayer new];
    ret.
strokeColor = lineColor.CGColor;
    ret.
fillColor = [UIColor clearColor].CGColor;
    ret.
lineCap = kCALineCapRound;
    ret.
lineJoin = kCALineJoinRound;
    ret.
lineWidth = 4;
    ret.
path = [self checkmarkPath].CGPath;
   
return ret;
}

- (
UIBezierPath *)checkmarkPath {
   
UIBezierPath *path = [UIBezierPath new];
    [path
moveToPoint:[self startPoint]];
    [path
addLineToPoint:[self middlePoint]];
    [path
addLineToPoint:[self endPoint]];
   
return path;
}

- (
CGPoint)startPoint {
   
CGFloat angle = 13 * M_PI /12;
   
return CGPointMake([self bounsCenter].x + [self innerRadius] * cos(angle), [self bounsCenter].y + [self innerRadius] * sin(angle));
}

- (
CGPoint)middlePoint {
   
return CGPointMake([self bounsCenter].x - [self innerRadius] * 0.25, [self bounsCenter].y + [self innerRadius] * 0.8);
}

- (
CGPoint)endPoint {
   
CGFloat angle = 7 * M_PI /4;
   
return CGPointMake([self bounsCenter].x + [self outerRadius] * cos(angle), [self bounsCenter].y + [self outerRadius] * sin(angle));
}

- (
CGPoint)bounsCenter {
   
return CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
}

- (
CGRect)insetRect {
   
CGRect ret = self.bounds;
   
CGRectInset(ret, 2 * lineWidth, 2 * lineWidth);
   
return ret;
}

- (
CGFloat)innerRadius {
   
return MIN([self insetRect].size.width, [self insetRect].size.height) / 2;
}

- (
CGFloat)outerRadius {
   
return sqrt(pow([self insetRect].size.width, 2) + pow([self insetRect].size.height, 2)) / 2;
}

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

打钩的动画

标签:ios

原文地址:http://blog.csdn.net/rainlesvio/article/details/47723753

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!