-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionButton.swift
More file actions
80 lines (71 loc) · 3.48 KB
/
OptionButton.swift
File metadata and controls
80 lines (71 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// CommentButton.swift
// Real News
//
// Created by John Smith on 2/4/17.
// Copyright © 2017 John Smith. All rights reserved.
//
import Foundation
import UIKit
import RxSwift
class OptionButton:UIButton{
let squareLayer = CAShapeLayer()
let arrowLayer = CAShapeLayer()
var iconFrame:CGRect!
var color = UIColor.appTextColor
override init(frame:CGRect) {
super.init(frame:frame)
layer.addSublayer(squareLayer)
layer.addSublayer(arrowLayer)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func set(fire:Bool){
if fire{
let transform = CGAffineTransform.identity.translatedBy(x: 0, y: -iconFrame.height/3)
arrowLayer.setAffineTransform(transform)
}else{
let transform = CGAffineTransform.identity.translatedBy(x: 0, y: 0)
arrowLayer.setAffineTransform(transform)
}
}
override func layoutSubviews() {
super.layoutSubviews()
let squarePath = UIBezierPath()
let arrowHalfGap = iconFrame.width/6
let squareOriginHeight = iconFrame.origin.y + (iconFrame.height/3)
let squareBottom = iconFrame.origin.y+iconFrame.height
let squareRight = iconFrame.origin.x+iconFrame.width
let squareMidX = iconFrame.origin.x+(iconFrame.width/2)
squarePath.move(to: CGPoint(x: iconFrame.origin.x+(iconFrame.width/2 - arrowHalfGap), y: squareOriginHeight))
squarePath.addLine(to: CGPoint(x: iconFrame.origin.x, y: squareOriginHeight))
squarePath.move(to: CGPoint(x: iconFrame.origin.x, y: squareOriginHeight))
squarePath.addLine(to: CGPoint(x: iconFrame.origin.x, y: squareBottom))
squarePath.move(to: CGPoint(x: iconFrame.origin.x, y: squareBottom))
squarePath.addLine(to: CGPoint(x: squareRight, y: squareBottom))
squarePath.move(to: CGPoint(x: squareRight, y: squareBottom))
squarePath.addLine(to: CGPoint(x: squareRight, y: squareOriginHeight))
squarePath.move(to: CGPoint(x: squareRight, y: squareOriginHeight))
squarePath.addLine(to: CGPoint(x: iconFrame.origin.x+(iconFrame.width/2 + arrowHalfGap), y: squareOriginHeight))
let arrowPath = UIBezierPath()
arrowPath.move(to: CGPoint(x: squareMidX, y: iconFrame.origin.y+iconFrame.height/10))
arrowPath.addLine(to: CGPoint(x: squareMidX, y: iconFrame.origin.y+(iconFrame.height/3) * 2))
arrowPath.move(to: CGPoint(x: squareMidX, y: iconFrame.origin.y+iconFrame.height/10))
arrowPath.addLine(to: CGPoint(x:iconFrame.origin.x+(iconFrame.width/3.5),y:iconFrame.origin.y+iconFrame.height/4.5))
arrowPath.move(to: CGPoint(x: squareMidX, y:iconFrame.origin.y+iconFrame.height/10))
arrowPath.addLine(to: CGPoint(x:iconFrame.origin.x+(iconFrame.width) - (iconFrame.width/3.5),y:iconFrame.origin.y+iconFrame.height/4.5))
squareLayer.path = squarePath.cgPath
//squareLayer.fillColor = UIColor.appBackgroundColor.cgColor
squareLayer.fillColor = UIColor.clear.cgColor
squareLayer.strokeColor = color.cgColor
squareLayer.lineWidth = 1.0
squareLayer.strokeEnd = 1.0
arrowLayer.path = arrowPath.cgPath
//arrowLayer.fillColor = UIColor.appBackgroundColor.cgColor
arrowLayer.fillColor = UIColor.clear.cgColor
arrowLayer.strokeColor = color.cgColor
arrowLayer.lineWidth = 1.0
arrowLayer.strokeEnd = 1.0
}
}