Basic concepts and grammar knowledge about Swift. I've covered a lot in the previous chapters. This section and the next section are mainly targeted to explain the use of swift in the actual Uikit development and attention points. Let's take a look at the demo at last.
Demo Analysis:
1. There are three buttons on the interface, their widths are inconsistent.
2. Click on each button. Here is a red underline followed by "walking around".
First, the design in the storyboard
Notice that the red underline is placed arbitrarily. There is no deliberate setting of its position and width. And this red underline is a simple uiview.
Second, Tow line work
In this example, there are three iboutlet lines that work, such as the following code:
@IBOutlet weak var firstbutton:uibutton! @IBOutlet weak var underline:uiview! @IBAction func Btnclick (Sender:uibutton)
1. The first button is wired. It is due to the fact that the first button is clicked by default when the program is started, which changes the position and width of the red underline.
2. Underline this uiview is the red underline.
About the above "!" use assumptions with any doubts. Be able to participate inSwift: Optional type (Optional).
3. Btnclick This method is a common click event for three buttons.
Third, button click event Analysis
The code is as follows:
Uiview.animatewithduration (0.25, animations: {(), Void Inself.selectedbutton?). Selected = falsesender.selected = Trueself.selectedbutton = Sender //To set the width height first, then set the position, Otherwise the effect has an effect self.underline.frame.size.width = Sender.frame.size.widthself.underline.center.x = SENDER.CENTER.XSELF.UNDERLINE.FRAME.ORIGIN.Y = Cgrectgetmaxy (sender.frame) + 5 })
The first three sentences in the code are the classic button click Trilogy (used to toggle the button's click State), the following three sentences are used to achieve the last animation, the program is simple and clear, I do not do too much to explain the work.
Just want to explain two points:
1. Selectedbutton is a defined property that points to the selected button.
2. Self.selectedbutton?. Selected = False is in fact the shorthand form of the code. Can also be written in the following form, they are equivalent.
if (Self.selectedbutton! = nil) { self.selectedButton.selected = false}
Note: In the inference statement, we used the comparison inference, that is, whether Self.selectedbutton is nil.
In Objective-c, you may be able to infer by writing directly into if (Self.selectedbutton). But it is wrong to write in Swift.
Because Swift is a type-safe language, the inference condition must be a bool value. Even if you write in Swift if (1) This inference condition is also a failure.
Four, the first button is selected by default
This operation is actually very easy. Just add one sentence to the viewdidload.
Self.btnclick (Self.firstbutton)
However, an underscore will appear by default to move the effect. Because the underscore is not below the first button by default. So running the animation will move the past.
The solution is to disable the animation when it is loaded by default. So finally the code such as the following:
Class Viewcontroller:uiviewcontroller { weak var selectedbutton:uibutton! @IBOutlet weak var firstbutton:uibutton! @IBOutlet weak var underline:uiview! @IBAction func Btnclick (Sender:uibutton) { uiview.animatewithduration (0.25, animations: {()-Void in self . Selectedbutton?. Selected = False sender.selected = true Self.selectedbutton = Sender Self.underline.frame.size.width = Sender.frame.size.width self.underline.center.x = sender.center.x SELF.UNDERLINE.FRAME.ORIGIN.Y = Cgrectgetmaxy (sender.frame) + 5 }) } override Func Viewdidload () { super.viewdidload () // When the interface was just loaded in. Disable animation uiview.setanimationsenabled (False) Self.btnclick (Self.firstbutton) Uiview.setanimationsenabled (True) }}
Demo in Swift:uikit (i.)