Second, code Analysis
This control is essentially a class inherited from UIView. So the whole code is actually a custom UIView class.
The following initialization is performed according to the rules of UIView:
Required Init (coder Adecoder:nscoder) {
Super.init (Coder:adecoder)
}
Override Init (Frame:cgrect) {
Self.pointer = Uiimageview (Image:uiimage (named: "Pointer2.png"))
Self.gaugeview = UIImage (named: "Gaugeback.png")
Self.framecurr = Frame
SELF.GV = Gaugeparam (frame:frame)
Super.init (Frame:frame)
Self.setframeinit (frame)
}
Constructors nothing to say, explain required init, if you inherit init (), you must implement required initializers.
A structure was designed to initialize the parameters of the class
struct gaugeparam{
var maxnum:float = MAXVALUE
var minnum:float = 0.00
var maxangle:float = Maxoffsetangle
var minangle:float =-maxoffsetangle
var gaugevalue:float = 0.00
var gaugeangle:float =-maxoffsetangle
var frame:cgrect
var anglepervalue:float{
get{
Return (Self.maxangle-self.minangle)/(Self.maxnum-self.minnum)
}
}
var scalenum:float{
get{
Return (Float (defluatsize)/float (self.frame.size.width))
}
}
Init (frame:cgrect) {
Self.frame = Frame
}
}
The above structure is very simple, there is no good explanation.
Here's the play, Gaugepanel is the main class, where all the operations are done.
The code structure is as follows:
Individual algorithms to explain (the birds, please bypass):
Func Parsetox (radius:cgfloat,angle:cgfloat), cgfloat{
Let temp = Self.transtoradian (angle)
return Radius*cos (temp)
}
Knowledge of geometry in junior high school, knowing radius and angle for x and y coordinates.
Explain the code in Pointtoangle (duration:cgfloat,angle:cgfloat):
Catransform3drotate (Catransform3didentity,self.transtoradian (CGFloat (gv.gaugeangle) + distance*CGFloat (i)), 0, 0, 1 )
This is the result of a 3D rotation, as it is a planar rotation so it is carried around the z axis, and the last parameter is set to 1,self.transtoradian (CGFloat (gv.gaugeangle) + distance*cgfloat (i)) is to calculate a range of angles.
Values.addobject (Nsvalue (CATRANSFORM3D:V)), Catransform3drotate does not immediately produce a rotation, but instead adds it to a nsmutablearray array, The element of Nsmutablearray is not anyobject, it needs to be wrapped with nsvalue function, the real rotation occurs in pointer.layer.addAnimation (ani,forkey: "Any"), ANI contains the contents of the values.
The override Func DrawRect (rect:cgrect) function does not have an explicit invocation, and is automatically triggered at the time of the Containview.addsubview (panel).
OK, I want to rest, I hope this biased article to Swift beginners (in fact, everyone is a beginner, old birds is a few months of experience) helpful. The fallacy of this control and suggestions for improvement can be contacted with me. QQ: Navel forest 32819362.
Developing dashboard controls with Swift (ii)