Swift Touch and gesture

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.