zoukankan      html  css  js  c++  java
  • Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)

    1. //  
    2. //  ViewController.swift  
    3. //  JieUITapGestureRecognizer  
    4. //  
    5. //  Created by jiezhang on 14-10-4.  
    6. //  Copyright (c) 2014年 jiezhang. All rights reserved.  
    7. //  
    8.   
    9. import UIKit  
    10.   
    11. class ViewController: UIViewController, UIActionSheetDelegate {  
    12.       
    13.     @IBOutlet var im: UIImageView!  
    14.     var lastScaleFactor : CGFloat! = 1  //放大、缩小  
    15.     var netRotation : CGFloat = 1;//旋转  
    16.     var netTranslation : CGPoint!//平移  
    17.     var images : NSArray = ["meinv1.jpg","mv2.jpg","mv3.jpg","mv4.jpg","mv5.jpg","mv6.jpg"]// 图片数组  
    18.     var imageIndex : Int = //数组下标  
    19.   
    20.       
    21.     required init(coder aDecoder: NSCoder) {  
    22.         super.init(coder: aDecoder)  
    23.         netTranslation = CGPoint(x: 0, y: 0)  
    24.     }  
    25.       
    26.     override func viewDidLoad() {  
    27.         super.viewDidLoad()  
    28.         
    29.           
    30.         var tapGesture = UITapGestureRecognizer(target: self, action: "handleTapGesture:")  
    31.         //设置手势点击数,双击:点2下  
    32.         tapGesture.numberOfTapsRequired = 2  
    33.         self.view.addGestureRecognizer(tapGesture)  
    34.           
    35.         //手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上  
    36.         var pinchGesture = UIPinchGestureRecognizer(target: self, action: "handlePinchGesture:")  
    37.         self.view.addGestureRecognizer(pinchGesture)  
    38.           
    39.         //旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上  
    40.         var rotateGesture = UIRotationGestureRecognizer(target: self, action: "handleRotateGesture:")  
    41.         self.view.addGestureRecognizer(rotateGesture)  
    42.           
    43.         //拖手势  
    44.         var panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")  
    45. //        self.view.addGestureRecognizer(panGesture)  
    46.           
    47.         //划动手势  
    48.         //右划  
    49.         var swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")  
    50.         self.view.addGestureRecognizer(swipeGesture)  
    51.         //左划  
    52.         var swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")  
    53.         swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left //不设置是右  
    54.         self.view.addGestureRecognizer(swipeLeftGesture)  
    55.           
    56.         //长按手势  
    57.         var longpressGesutre = UILongPressGestureRecognizer(target: self, action: "handleLongpressGesture:")  
    58.         //长按时间为1秒  
    59.         longpressGesutre.minimumPressDuration = 1  
    60.         //允许15秒运动  
    61.         longpressGesutre.allowableMovement = 15  
    62.         //所需触摸1次  
    63.         longpressGesutre.numberOfTouchesRequired = 1  
    64.         self.view.addGestureRecognizer(longpressGesutre)  
    65.     }  
    66.   
    67.     override func didReceiveMemoryWarning() {  
    68.         super.didReceiveMemoryWarning()  
    69.         // Dispose of any resources that can be recreated.  
    70.     }  
    71.       
    72.     //双击屏幕时会调用此方法,放大和缩小图片  
    73.     func handleTapGesture(sender: UITapGestureRecognizer){  
    74.         //判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小   
    75.         if im.contentMode == UIViewContentMode.ScaleAspectFit{  
    76.             //把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView  
    77.            im.contentMode = UIViewContentMode.Center  
    78.         }else{  
    79.             im.contentMode = UIViewContentMode.ScaleAspectFit  
    80.         }  
    81.     }  
    82.       
    83.     //捏的手势,使图片放大和缩小,捏的动作是一个连续的动作  
    84.     func handlePinchGesture(sender: UIPinchGestureRecognizer){  
    85.         var factor = sender.scale  
    86.         if factor > 1{  
    87.             //图片放大  
    88.             im.transform = CGAffineTransformMakeScale(lastScaleFactor+factor-1, lastScaleFactor+factor-1)  
    89.         }else{  
    90.             //缩小  
    91.             im.transform = CGAffineTransformMakeScale(lastScaleFactor*factor, lastScaleFactor*factor)  
    92.         }  
    93.         //状态是否结束,如果结束保存数据  
    94.         if sender.state == UIGestureRecognizerState.Ended{  
    95.             if factor > 1{  
    96.                 lastScaleFactor = lastScaleFactor + factor - 1  
    97.             }else{  
    98.                 lastScaleFactor = lastScaleFactor * factor  
    99.             }  
    100.         }  
    101.     }  
    102.       
    103.     //旋转手势  
    104.     func handleRotateGesture(sender: UIRotationGestureRecognizer){  
    105.         //浮点类型,得到sender的旋转度数   
    106.         var rotation : CGFloat = sender.rotation  
    107.         //旋转角度CGAffineTransformMakeRotation,改变图像角度  
    108.         im.transform = CGAffineTransformMakeRotation(rotation+netRotation)  
    109.         //状态结束,保存数据  
    110.         if sender.state == UIGestureRecognizerState.Ended{  
    111.             netRotation += rotation  
    112.         }  
    113.     }  
    114.     //拖手势  
    115.     func handlePanGesture(sender: UIPanGestureRecognizer){  
    116.         //得到拖的过程中的xy坐标  
    117.         var translation : CGPoint = sender.translationInView(im)  
    118.         //平移图片CGAffineTransformMakeTranslation  
    119.         im.transform = CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y+translation.y)  
    120.         if sender.state == UIGestureRecognizerState.Ended{  
    121.             netTranslation.x += translation.x  
    122.             netTranslation.y += translation.y  
    123.         }  
    124.     }  
    125.     //划动手势  
    126.     func handleSwipeGesture(sender: UISwipeGestureRecognizer){  
    127.         //划动的方向  
    128.         var direction = sender.direction  
    129.         //判断是上下左右  
    130.         switch (direction){  
    131.         case UISwipeGestureRecognizerDirection.Left:  
    132.             println("Left")  
    133.             imageIndex++;//下标++  
    134.             break  
    135.         case UISwipeGestureRecognizerDirection.Right:  
    136.             println("Right")  
    137.             imageIndex--;//下标--  
    138.             break  
    139.         case UISwipeGestureRecognizerDirection.Up:  
    140.             println("Up")  
    141.             break  
    142.         case UISwipeGestureRecognizerDirection.Down:  
    143.             println("Down")  
    144.             break  
    145.         default:  
    146.             break;  
    147.         }  
    148.         //得到不越界不<0的下标  
    149.         imageIndex = imageIndex < 0 ? images.count-1:imageIndex%images.count  
    150.         //imageView显示图片  
    151.         im.image = UIImage(named: images[imageIndex] as String)  
    152.     }  
    153.       
    154.     //长按手势  
    155.     func handleLongpressGesture(sender : UILongPressGestureRecognizer){  
    156.           
    157.         if sender.state == UIGestureRecognizerState.Began{  
    158.             //创建警告  
    159.             var actionSheet = UIActionSheet(title: "Image options", delegate: self, cancelButtonTitle: "cancel", destructiveButtonTitle: "ok", otherButtonTitles: "other")  
    160.             actionSheet.showInView(self.view)  
    161.         }  
    162.     }  
    163. }  

    注意:滑动手势和拖手势冲突,两个选一个测试,至于测试的效果自己新建一个工程来把代码加入就行

  • 相关阅读:
    如何使用GOOGLE高级搜索技巧
    你所认为的极限,可能只是别人眼中的起点
    飞机选座——附:东航320选坐攻略
    古诗词里,从初识到相爱到分离到重逢的漫长过程
    从零开始学摄影
    Python之运维
    Linux用户和组密令大全
    centos7 下安装生物信息软件的问题小总结
    VMware锁定文件失败开启模块diskearly的操作失败未能启动虚拟机
    linux 基本命令整理--转
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6248963.html
Copyright © 2011-2022 走看看