This time we will learn gesture, TableView, alertview three kinds of technology.
First, Gesture
In iOS, you can use the system's built-in gesture recognition (Gesturerecognizer), or you can create your own gestures.
Gesturerecognizer converts a low-level transition to a higher execution behavior and then binds to the view object, so when a gesture occurs, the bound view object responds and determines whether the action corresponds to a specific gesture (Swipe,pinch,pan, Rotation
If it can recognize a gesture, it sends a message to the view that binds it.
The Uikit framework provides predefined gesturerecognizer:uitapgesturerecognizer, Uipangesturerecognizer, Uipinchgesturerecognizer, Uiswipegesturerecognizer, Uirotationgesturerecognizer, Uilongpressgesturerecognizer.
Adding gestures takes only three steps:
- Create a gesture recognizer (such as a UITapGestureRecognizer) instance, set the target (typically self) in its AddTarget method, and the action method, specifying properties such as the identifier of the recognizer instance Isuserinteractionenable = True
- Additional recognizers to view: *view.addgesturerecognizer (recognizer instance)
- Implement the method specified by the action
Full code:
Import UIKit
Class Viewcontroller:uiviewcontroller {
@IBOutlet weak var tapview:uiview!
var lastrotation = CGFloat ()
Let Taprec = UITapGestureRecognizer ()
Override Func Viewdidload () {
Super.viewdidload ()
Taprec.addtarget (Self, Action: #selector (Viewcontroller.tappedview))
Tapview.addgesturerecognizer (TAPREC)
Tapview.isuserinteractionenabled = True
}
Func Tappedview ()
{
Print ("Hello")
}
}
Second, UITableView
Table is one of the most common UI controls for displaying data. Data display style: List style plain, block style grouped.
TableView implementation steps:
- Add UITableView and prototype Cells.
- A class is required to follow the Uitableviewdelegate, Uitableviewdatasource protocol (and implement three specific methods later), and then set the proxy for TableView to the class just now: Tableview.delegate = Self (or other class name), Tableview.datasource = self.
- Set the identifier of the Tableviewcell (as long as it is unique)
- Initializing data
- Implement the three specific, required methods of the Uitableviewdatasource protocol to achieve data display in these three methods: set the number of sections, the number of rows in a single section, and display cell data.
Here is an example of TableView:
Import UIKit
Class Viewcontroller:uiviewcontroller,uitableviewdelegate,uitableviewdatasource {
var myName = [String] ()
@IBOutlet weak var txtname:uitextfield!
@IBOutlet weak var tvwnames:uitableview!
Override Func Viewdidload () {
Super.viewdidload ()
Tvwnames.delegate = self// 2nd step
Tvwnames.datasource = Self
Myname.append ("AA")//initial default value
}
@IBAction Func Btn_add (_ Sender:uibutton) {
If let name = txtName.Text {
Myname.append (name)
Tvwnames.reloaddata ()
}
}
@IBAction Func Btn_save (_ Sender:uibutton) {
}
@IBAction Func Btn_read (_ Sender:uibutton) {
}
Func numberofsections (in Tableview:uitableview), Int {
Return 1
}
The number of rows within a single section
Func TableView (_ Tableview:uitableview, Numberofrowsinsection section:int), Int {
Return Myname.count
}
Func TableView (_ Tableview:uitableview, Cellforrowat indexpath:indexpath), UITableViewCell {
Let Cellidentifier = "MyName"
Let cell = Tableview.dequeuereusablecell (Withidentifier:cellidentifier, For:indexpath)// 4th step Setting data
Cell.textlabel?. Text = Myname[indexpath.row]//Set initial data
return cell//returns data
}
}
TableView can also customize the table: Develop a display template for each row (cell).
Alert Actionview:
iOS has two kinds of pop-up prompts: Alert (on-screen popup), Actionsheet (bottom popup menu).
Put a button first, then right button to drag a @ibaction method, then ...
Full code:
@IBAction func btnexit (Sender:uibutton) {
Let Alertcontroller = Uialertcontroller (title: "caption", message: "This is what I want to show", Preferredstyle:.Alert)//Indicates a frame alert, or it can be a actionsheet
Let okaction = uialertaction (title: "Good", Style:.) Default, Handler: {
Action in
Print ("Hello")
Exit (0)
})
Let cancelaction = uialertaction (title: "Cancel", Style:.) Cancel, Handler:nil)//can be used. Destructive warning: text color turns red
Alertcontroller.addaction (cancelaction)
Alertcontroller.addaction (okaction)
Self.presentviewcontroller (Alertcontroller, Animated:true, Completion:nil)
Some are self.present (Alertcontroller, Animated:true, Completion:nil), in short a pattern box
}
Second Exam complete code : Files.cnblogs.com/files/quanxi/2014110231.zip
files.cnblogs.com/files/quanxi/latest. zip
iOS fifth lesson--gesture and TableView