Skip to content

Commit 4ebee35

Browse files
committed
Merge pull request #6 from skywinder/update
Set correct center values by CGRectGetMidX(Y)
2 parents c6ab284 + 242b04f commit 4ebee35

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

DOFavoriteButton/DOFavoriteButton.swift

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import UIKit
1313

1414
@IBDesignable
1515
public class DOFavoriteButton: UIButton {
16-
16+
1717
private var imageShape: CAShapeLayer!
1818
@IBInspectable public var image: UIImage! {
1919
didSet {
@@ -34,15 +34,15 @@ public class DOFavoriteButton: UIButton {
3434
}
3535
}
3636
}
37-
37+
3838
private var circleShape: CAShapeLayer!
3939
private var circleMask: CAShapeLayer!
4040
@IBInspectable public var circleColor: UIColor! = UIColor(red: 255/255, green: 172/255, blue: 51/255, alpha: 1.0) {
4141
didSet {
4242
circleShape.fillColor = circleColor.CGColor
4343
}
4444
}
45-
45+
4646
private var lines: [CAShapeLayer]!
4747
@IBInspectable public var lineColor: UIColor! = UIColor(red: 250/255, green: 120/255, blue: 68/255, alpha: 1.0) {
4848
didSet {
@@ -51,14 +51,14 @@ public class DOFavoriteButton: UIButton {
5151
}
5252
}
5353
}
54-
54+
5555
private let circleTransform = CAKeyframeAnimation(keyPath: "transform")
5656
private let circleMaskTransform = CAKeyframeAnimation(keyPath: "transform")
5757
private let lineStrokeStart = CAKeyframeAnimation(keyPath: "strokeStart")
5858
private let lineStrokeEnd = CAKeyframeAnimation(keyPath: "strokeEnd")
5959
private let lineOpacity = CAKeyframeAnimation(keyPath: "opacity")
6060
private let imageTransform = CAKeyframeAnimation(keyPath: "transform")
61-
61+
6262
@IBInspectable public var duration: Double = 1.0 {
6363
didSet {
6464
circleTransform.duration = 0.333 * duration // 0.0333 * 10
@@ -69,7 +69,7 @@ public class DOFavoriteButton: UIButton {
6969
imageTransform.duration = 1.0 * duration //0.0333 * 30
7070
}
7171
}
72-
72+
7373
override public var selected : Bool {
7474
didSet {
7575
if (selected != oldValue) {
@@ -81,35 +81,35 @@ public class DOFavoriteButton: UIButton {
8181
}
8282
}
8383
}
84-
84+
8585
public convenience init() {
8686
self.init(frame: CGRectZero)
8787
}
88-
88+
8989
public override convenience init(frame: CGRect) {
9090
self.init(frame: frame, image: UIImage())
9191
}
92-
92+
9393
public init(frame: CGRect, image: UIImage!) {
9494
super.init(frame: frame)
9595
self.image = image
9696
createLayers(image: image)
9797
addTargets()
9898
}
99-
99+
100100
public required init(coder aDecoder: NSCoder) {
101101
super.init(coder: aDecoder)
102102
createLayers(image: UIImage())
103103
addTargets()
104104
}
105-
105+
106106
private func createLayers(#image: UIImage!) {
107107
self.layer.sublayers = nil
108-
108+
109109
let imageFrame = CGRectMake(frame.size.width / 2 - frame.size.width / 4, frame.size.height / 2 - frame.size.height / 4, frame.size.width / 2, frame.size.height / 2)
110-
let imgCenterPoint = CGPointMake(imageFrame.origin.x + imageFrame.width / 2, imageFrame.origin.y + imageFrame.height / 2)
110+
let imgCenterPoint = CGPointMake(CGRectGetMidX(imageFrame), CGRectGetMidY(imageFrame))
111111
let lineFrame = CGRectMake(imageFrame.origin.x - imageFrame.width / 4, imageFrame.origin.y - imageFrame.height / 4 , imageFrame.width * 1.5, imageFrame.height * 1.5)
112-
112+
113113
//===============
114114
// circle layer
115115
//===============
@@ -120,17 +120,17 @@ public class DOFavoriteButton: UIButton {
120120
circleShape.fillColor = circleColor.CGColor
121121
circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 1.0)
122122
self.layer.addSublayer(circleShape)
123-
123+
124124
circleMask = CAShapeLayer()
125125
circleMask.bounds = imageFrame
126126
circleMask.position = imgCenterPoint
127127
circleMask.fillRule = kCAFillRuleEvenOdd
128128
circleShape.mask = circleMask
129-
129+
130130
let maskPath = UIBezierPath(rect: imageFrame)
131131
maskPath.addArcWithCenter(imgCenterPoint, radius: 0.1, startAngle: CGFloat(0.0), endAngle: CGFloat(M_PI * 2), clockwise: true)
132132
circleMask.path = maskPath.CGPath
133-
133+
134134
//===============
135135
// line layer
136136
//===============
@@ -146,7 +146,7 @@ public class DOFavoriteButton: UIButton {
146146
line.miterLimit = 1.25
147147
line.path = {
148148
let path = CGPathCreateMutable()
149-
CGPathMoveToPoint(path, nil, lineFrame.origin.x + lineFrame.width / 2, lineFrame.origin.y + lineFrame.height / 2)
149+
CGPathMoveToPoint(path, nil, CGRectGetMidX(lineFrame), CGRectGetMidY(lineFrame))
150150
CGPathAddLineToPoint(path, nil, lineFrame.origin.x + lineFrame.width / 2, lineFrame.origin.y)
151151
return path
152152
}()
@@ -159,7 +159,7 @@ public class DOFavoriteButton: UIButton {
159159
self.layer.addSublayer(line)
160160
lines.append(line)
161161
}
162-
162+
163163
//===============
164164
// image layer
165165
//===============
@@ -170,12 +170,12 @@ public class DOFavoriteButton: UIButton {
170170
imageShape.fillColor = imageColorOff.CGColor
171171
imageShape.actions = ["fillColor": NSNull()]
172172
self.layer.addSublayer(imageShape)
173-
173+
174174
imageShape.mask = CALayer()
175175
imageShape.mask.contents = image.CGImage
176176
imageShape.mask.bounds = imageFrame
177177
imageShape.mask.position = imgCenterPoint
178-
178+
179179
//==============================
180180
// circle transform animation
181181
//==============================
@@ -200,7 +200,7 @@ public class DOFavoriteButton: UIButton {
200200
0.6, // 6/10
201201
1.0 // 10/10
202202
]
203-
203+
204204
circleMaskTransform.duration = 0.333 // 0.0333 * 10
205205
circleMaskTransform.values = [
206206
NSValue(CATransform3D: CATransform3DIdentity), // 0/10
@@ -224,7 +224,7 @@ public class DOFavoriteButton: UIButton {
224224
0.9, // 9/10
225225
1.0 // 10/10
226226
]
227-
227+
228228
//==============================
229229
// line stroke animation
230230
//==============================
@@ -255,7 +255,7 @@ public class DOFavoriteButton: UIButton {
255255
0.944, // 17/18
256256
1.0, // 18/18
257257
]
258-
258+
259259
lineStrokeEnd.duration = 0.6 //0.0333 * 18
260260
lineStrokeEnd.values = [
261261
0.0, // 0/18
@@ -277,7 +277,7 @@ public class DOFavoriteButton: UIButton {
277277
0.944, // 17/18
278278
1.0, // 18/18
279279
]
280-
280+
281281
lineOpacity.duration = 1.0 //0.0333 * 30
282282
lineOpacity.values = [
283283
1.0, // 0/30
@@ -289,7 +289,7 @@ public class DOFavoriteButton: UIButton {
289289
0.4, // 12/30
290290
0.567 // 17/30
291291
]
292-
292+
293293
//==============================
294294
// image transform animation
295295
//==============================
@@ -333,7 +333,7 @@ public class DOFavoriteButton: UIButton {
333333
1.0 // 30/30
334334
]
335335
}
336-
336+
337337
private func addTargets() {
338338
//===============
339339
// add target
@@ -344,7 +344,7 @@ public class DOFavoriteButton: UIButton {
344344
self.addTarget(self, action: "touchDragEnter:", forControlEvents: UIControlEvents.TouchDragEnter)
345345
self.addTarget(self, action: "touchCancel:", forControlEvents: UIControlEvents.TouchCancel)
346346
}
347-
347+
348348
func touchDown(sender: DOFavoriteButton) {
349349
self.layer.opacity = 0.4
350350
}
@@ -360,30 +360,30 @@ public class DOFavoriteButton: UIButton {
360360
func touchCancel(sender: DOFavoriteButton) {
361361
self.layer.opacity = 1.0
362362
}
363-
363+
364364
public func select() {
365365
selected = true
366366
imageShape.fillColor = imageColorOn.CGColor
367-
367+
368368
CATransaction.begin()
369-
369+
370370
circleShape.addAnimation(circleTransform, forKey: "transform")
371371
circleMask.addAnimation(circleMaskTransform, forKey: "transform")
372372
imageShape.addAnimation(imageTransform, forKey: "transform")
373-
373+
374374
for i in 0 ..< 5 {
375375
lines[i].addAnimation(lineStrokeStart, forKey: "strokeStart")
376376
lines[i].addAnimation(lineStrokeEnd, forKey: "strokeEnd")
377377
lines[i].addAnimation(lineOpacity, forKey: "opacity")
378378
}
379-
379+
380380
CATransaction.commit()
381381
}
382-
382+
383383
public func deselect() {
384384
selected = false
385385
imageShape.fillColor = imageColorOff.CGColor
386-
386+
387387
// remove all animations
388388
circleShape.removeAllAnimations()
389389
circleMask.removeAllAnimations()

0 commit comments

Comments
 (0)