viewcontroller.swift//jieuitapgesturerecognizer////Created by Jiezhang on 14-10-4.//Copyright (c) 2014 Jiezha Ng. All rights Reserved.//import Uikitclass Viewcontroller:uiviewcontroller, uiactionsheetdelegate {@IBOutlet var im: uiimageview! var lastscalefactor:cgfloat! = 1//zoom in, zoom out var netrotation:cgfloat = 1;//rotation var nettranslation:cgpoint!//translate var images:nsarray = ["Meinv1 . jpg "," mv2.jpg "," mv3.jpg "," mv4.jpg "," mv5.jpg "," mv6.jpg "]//picture array var imageindex:int = 0//array subscript required Init (CO Der Adecoder:nscoder) {super.init (coder:adecoder) nettranslation = Cgpoint (x:0, y:0)} Overri De func viewdidload () {super.viewdidload () var tapgesture = UITapGestureRecognizer (target:se LF, Action: "Handletapgesture:")//Set gesture hit number, double click: Point 2 under tapgesture.numberoftapsrequired = 2 self.view.addGe Sturerecognizer (Tapgesture)//gesture for pinch: Hold the option button with the mouse to do this action on the virtual device var PincHgesture = Uipinchgesturerecognizer (target:self, Action: "Handlepinchgesture:") Self.view.addGestureRecognizer (Pin chgesture)//rotate gesture: Hold the option button with the mouse to do this action on the virtual device var rotategesture = Uirotationgesturerecognizer (target: Self, Action: "Handlerotategesture:") Self.view.addGestureRecognizer (rotategesture)//drag gesture Var Pangesture = Uipangesturerecognizer (target:self, Action: "Handlepangesture:")//Self.view.addGestureRecognizer (PA ngesture)//swipe gesture//right-stroke var swipegesture = Uiswipegesturerecognizer (target:self, action: "Hand Leswipegesture: ") Self.view.addGestureRecognizer (swipegesture)//left row var swipeleftgesture = Uiswipeges Turerecognizer (Target:self, Action: "Handleswipegesture:") swipeleftgesture.direction = Uiswipegesturerecognizerdi Rection. Left//Not set is right Self.view.addGestureRecognizer (swipeleftgesture)//Long press gesture var longpressgesutre = U IlongpressgesturErecognizer (Target:self, Action: "Handlelongpressgesture:")//long-press time is 1 seconds longpressgesutre.minimumpressduration = 1//Allow 15 second movement longpressgesutre.allowablemovement = 15//required Touch 1 times Longpressgesutre.numberoftouch esrequired = 1 Self.view.addGestureRecognizer (longpressgesutre)} override func didreceivememorywarning () { Super.didreceivememorywarning ()//Dispose of any of the resources that can is recreated. }//This method is called when you double-click the screen to zoom in and out of the picture Func handletapgesture (sender:uitapgesturerecognizer) {//determines if ImageView's content mode is Uivie Wcontentmodescaleaspectfit, the mode is the original scale, according to the picture original time scale display size if Im.contentmode = = uiviewcontentmode.scaleaspectfit{ Change the ImageView mode to Uiviewcontentmodecenter, according to the original size of the picture display part of the center in ImageView im.contentmode = Uiviewcontentmode.center }else{Im.contentmode = Uiviewcontentmode.scaleaspectfit}}/pinch gesture to enlarge and shrink the image, pinch action is a continuous action Func Handlepinchgesture (sender:uipinchgestUrerecognizer) {var factor = Sender.scale If factor > 1{//Picture magnification im.transform = Cgaf Finetransformmakescale (Lastscalefactor+factor-1, lastscalefactor+factor-1)}else{//Zoom Out Im.tra Nsform = Cgaffinetransformmakescale (Lastscalefactor*factor, Lastscalefactor*factor)}//Whether the state ends, and if the data is saved if sender.state = = uigesturerecognizerstate.ended{if factor > 1{lastscalefactor = LASTSC Alefactor + factor-1}else{lastscalefactor = Lastscalefactor * Factor}} }//Rotation gesture func handlerotategesture (Sender:uirotationgesturerecognizer) {//float type, get sender's rotation degree VA R rotation:cgfloat = sender.rotation//rotation angle cgaffinetransformmakerotation, change image angle Im.transform = Cgaffinetra Nsformmakerotation (rotation+netrotation)//Status end, save data if sender.state = = uigesturerecognizerstate.ended{ Netrotation + = RoTation}}//Drag gesture func handlepangesture (Sender:uipangesturerecognizer) {//Get dragged in the process of the XY coordinate var t Ranslation:cgpoint = Sender.translationinview (IM)//panning picture cgaffinetransformmaketranslation Im.transform = CG Affinetransformmaketranslation (nettranslation.x+translation.x, nettranslation.y+translation.y) if sender.state = = U igesturerecognizerstate.ended{nettranslation.x + = translation.x Nettranslation.y + = TRANSLATION.Y }}//Scrub gesture func handleswipegesture (Sender:uiswipegesturerecognizer) {//the direction of the stroke var direction = Sender.direction//judgment is up or down switch (direction) {case Uiswipegesturerecognizerdirection.left: println ("left") imageindex++;//subscript + + BREAK case uiswipegesturerecognizerdirection.right: println ("right") imageindex--;//subscript--Break case Uiswipegesturerecognizerdirection.up : println ("Up") Break case UISwipeGestureRecognizerDirection.Down:println (' down ') break Default:break; }//Get non-crossed non-<0 subscript imageindex = ImageIndex < 0? Images.count-1:imageindex%images.count//imageview Display Picture im.image = UIImage (Named:images[imageindex] as Strin g)}//long-Press gesture func Handlelongpressgesture (sender:uilongpressgesturerecognizer) {if sender.stat E = = uigesturerecognizerstate.began{//create warning var actionsheet = uiactionsheet (title: "Image Options", Delegate:self, Cancelbuttontitle: "Cancel", Destructivebuttontitle: "OK", Otherbuttontitles: "Other") Actionsh Eet.showinview (Self.view)}}}
Note: Swipe gestures and drag gestures conflict, two choose a test, as for the effect of the test to create a new project to add code to the line
Swift Basics-Gesture recognition (double tap, pinch, rotate, drag, scrub, long Press)