Touch events:
Uitouch: A mobile phone first click on the screen, will form a Uitouch object, know to leave the destruction. Indicates a touch. The Uitouch object can indicate the position and state of the screen where the finger is touching, and the state is divided into touching, moving and leaving.
The specific methods are described as follows:
1.override Func Touchesbegan (_ Touches:set<uitouch>, with Event:uievent?)
Notifies the caller when one or more fingers touch the view or window to trigger the secondary method, touches is a collection of Uitouch, through Uito we can detect the properties of the touch event, whether it is the click or double, and the location of the touch.
2.override Func touchesmoved (_ Touches:set<uitouch>, with Event:uievent?)
Tells the recipient that one or more fingers trigger a move event on a view or window. Multi-touch is not allowed by default, and the UIView property must be set to TRUE if you want to accept multi-touch events.
// Multi-Touch support true
3.override Func touchesmoved (_ Touches:set<uitouch>, with Event:uievent?)
The Uitouch instance object emitted when a touch event ends
4.override Func touchescancelled (_ Touches:set<uitouch>, with Event:uievent?)
Notifies the recipient when the system issues a cancellation event (such as a warning box when memory consumption is used)
Sample code:
OverrideFunc Touchesbegan (_ Touches:set<uitouch>, withEvent: Uievent?) { forTouch:anyobjectinchtouches {let T:uitouch= Touch as!Uitouch//The background returns to white when the screen is continuously tapped twice ifT.tapcount = =2{Self.view.backgroundColor=Uicolor.white}Else ifT.tapcount = =1{Self.view.backgroundColor=Uicolor.blue} }}
Override event: Uievent? ) { // get the coordinate position of the click for in touches { as ! uitouch Print (t.location (in: Self.view)) } }
OverrideFunc touchesended (_ Touches:set<uitouch>, withEvent: Uievent?) { ifTouches.count = =2 { //Get Touch PointLet first = (touches asNsset). allobjects[0] as!Uitouch Let second= (touches asNsset). allobjects[1] as!Uitouch//get touch Point coordinatesLet Firstpoint = First.location (inch: Self.view) let Secondpoint= Second.location (inch: Self.view)//calculate the distance between two pointsLet DeltaX = Secondpoint.x-firstpoint.x let DeltaY= Secondpoint.y-Firstpoint.y let Initialdistance= sqrt (deltax + deltay *deltay) Print ("distance between two points: \ (initialdistance)") //calculate the angle between two pointsLet height = secondpoint.y-firstpoint.y Let width= Firstpoint.x-secondpoint.x let Rads= Atan (height/width) Let degrees=180.0* Double (rads)/. Pi Print ("angle between two points: \ (degrees)") } }
Override event: Uievent? ) { print ("event canceled! " ) }
swift--Touch (Uitouch) event (click, move, lift)