- (void)awakeFromNib
{
self.lineColor
= [UIColor
whiteColor];
self.lineHeight
=
2.0;
self.lineSpacing
=
8.0;
self.lineWidth
=
30.0;
[self
updateAppearance];
}
- (void)updateAppearance {
CGFloat x =
CGRectGetWidth(self.bounds)
/ 2.0;
CGFloat heightDiff =
self.lineHeight
+
self.lineSpacing;
CGFloat y =
CGRectGetHeight(self.bounds)
/ 2.0
- heightDiff;
_topLayer = [self
createLayer];
_topLayer.position
=
CGPointMake(x , y);
y += heightDiff;
_middleLayer = [self
createLayer];
_middleLayer.position
=
CGPointMake(x , y);
y += heightDiff;
_bottomLayer = [self
createLayer];
_bottomLayer.position
=
CGPointMake(x , y);
[self
transformToState:_currentState];
}
- (void)transformToState:(AnimationButtonState)state
{
_currentState = state;
CATransform3D transform;
switch (state) {
case
AnimationButtonStateCross:{
transform =
CATransform3DMakeTranslation(0.0,
_middleLayer.position.y-_topLayer.position.y,
0.0);
_topLayer.transform
=
CATransform3DRotate(transform,
M_PI_4,
0.0,
0.0,
1.0);
_middleLayer.transform
=
CATransform3DMakeScale(0.,
0.,
0.);
transform =
CATransform3DMakeTranslation(0.0,
_middleLayer.position.y-_bottomLayer.position.y,
0.0);
_bottomLayer.transform
=
CATransform3DRotate(transform, -M_PI_4,
0.0,
0.0,
1.0);
}break;
default:{
_topLayer.transform
=
CATransform3DIdentity;
_middleLayer.transform
=
CATransform3DIdentity;
_bottomLayer.transform
=
CATransform3DIdentity;
}break;
}
}
- (CAShapeLayer
*)createLayer {
CAShapeLayer *layer = [CAShapeLayer
new];
UIBezierPath *path = [UIBezierPath
new];
[path
moveToPoint:CGPointMake(0,
0)];
[path
addLineToPoint:CGPointMake(self.lineWidth,
0)];
layer.path
= path.CGPath;
layer.lineWidth
=
self.lineHeight;
layer.strokeColor
=
self.lineColor.CGColor;
layer.lineCap
=
@"round";
CGPathRef bound =
CGPathCreateCopyByStrokingPath(layer.path,
nil,
layer.lineWidth,
kCGLineCapButt,
kCGLineJoinMiter,
layer.miterLimit);
layer.bounds
=
CGPathGetBoundingBox(bound);
CGPathRelease(bound);
[self.layer
addSublayer:layer];
return layer;
}