Class Myview:uiview {
var lview:uiview!
var time:nstimer!
Override Init (Frame:cgrect) {
Super.init (Frame:frame)
Turn on multi-touch
Self.multipletouchenabled = True
LView = UIView (Frame:cgrect (x:0, y:0, width:50, height:50));
Lview.backgroundcolor = Uicolor.yellowcolor ();
Self.addsubview (LView)
}
Required init? (Coder Adecoder:nscoder) {
Super.init (Coder:adecoder)
}
The method that is called when the click is started
Override Func Touchesbegan (touches:set<uitouch>, withevent event:uievent?) {
println (count)
Got a Uitouch object.
Let Touch:uitouch = Touches.anyobject () as! Uitouch;
Let touch = Touches.first as uitouch!
Get the number of clicks
Let Tapcount = Touch.tapcount;
if (Tapcount = = 1)
{
Click Singletap ()
Using timer delay call
Time = Nstimer.scheduledtimerwithtimeinterval (0.5, target:self, selector: "Singletap", Userinfo:nil, Repeats:false)
}else
{
termination Timer
Time.invalidate ()
Doubletap ()
}
}
Func Singletap () {
Print ("click")
}
Func Doubletap () {
Print ("Double-click")
}
The method that is called when the touch moves
Override Func touchesmoved (touches:set<uitouch>, withevent event:uievent?) {
var touch = Touches.anyobject () as! Uitouch
Let touch = Touches.first as uitouch!
Let location = Touch.locationinview (self)
println ("Location: \ (location.x), \ (LOCATION.Y), \ (location)")
Let lastlocation = Touch.previouslocationinview (self)
println ("Lastlocation: \ (lastlocation.x), \ (LASTLOCATION.Y), \ (lastlocation)")
Let sub = location.x-lastlocation.x
If sub > 0{
Print ("right")
}else If sub < 0 {
Print ("left")
}else {
Print ("Move")
}
Let the small rectangle follow the gesture move Lview.center = Location
LView.frame.origin = Location
}
}
Swift Touch and gesture