- //
- //ViewController.swift
- //JieUITapGestureRecognizer
- //
- //Createdbyjiezhangon14-10-4.
- //Copyright(c)2014年jiezhang.Allrightsreserved.
- importUIKit @H_403_65@ classViewController:UIViewController,UIActionSheetDelegate{ @H_403_65@ @IBOutletvarim:UIImageView! @H_403_65@ varlastScaleFactor:CGFloat!=1//放大、缩小
- varnetRotation:CGFloat=1;//旋转 @H_403_65@ varnetTranslation:CGPoint!//平移
- varimages:NSArray=["meinv1.jpg","mv2.jpg","mv3.jpg","mv4.jpg","mv5.jpg","mv6.jpg"]//图片数组 @H_403_65@ varimageIndex:Int=0//数组下标
- @H_403_65@ requiredinit(coderaDecoder:NSCoder){ @H_403_65@ super.init(coder:aDecoder)
- netTranslation=CGPoint(x:0,y:0) @H_403_65@ }
- overridefuncviewDidLoad(){
- super.viewDidLoad() @H_403_65@ vartapGesture=UITapGestureRecognizer(target:self,0); background-color:inherit">action:"handleTapGesture:")
- //设置手势点击数,双击:点2下 @H_403_65@ tapGesture.numberOfTapsrequired=2
- self.view.addGestureRecognizer(tapGesture)
- //手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上 @H_403_65@ varpinchGesture=UIPinchGestureRecognizer(target:self,0); background-color:inherit">action:"handlePinchGesture:")
- self.view.addGestureRecognizer(pinchGesture)
- //旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上 @H_403_65@ varrotateGesture=UIRotationGestureRecognizer(target:self,0); background-color:inherit">action:"handleRotateGesture:")
- self.view.addGestureRecognizer(rotateGesture)
- //拖手势 @H_403_65@ varpanGesture=UIPanGestureRecognizer(target:self,0); background-color:inherit">action:"handlePanGesture:")
- //self.view.addGestureRecognizer(panGesture)
- //划动手势
- //右划
- varswipeGesture=UISwipeGestureRecognizer(target:self,0); background-color:inherit">action:"handleSwipeGesture:") @H_403_65@ self.view.addGestureRecognizer(swipeGesture)
- //左划 @H_403_65@ varswipeLeftGesture=UISwipeGestureRecognizer(target:self,0); background-color:inherit">action:"handleSwipeGesture:")
- swipeLeftGesture.direction=UISwipeGestureRecognizerDirection.Left//不设置是右 @H_403_65@ self.view.addGestureRecognizer(swipeLeftGesture)
- //长按手势
- varlongpressGesutre=UILongPressGestureRecognizer(target:self,0); background-color:inherit">action:"handleLongpressGesture:")
- //长按时间为1秒
- longpressGesutre.minimumPressDuration=1
- //允许15秒运动
- longpressGesutre.allowableMovement=15
- //所需触摸1次
- longpressGesutre.numberOfTouchesrequired=1 @H_403_65@ self.view.addGestureRecognizer(longpressGesutre)
- } @H_403_65@ overridefuncdidReceiveMemoryWarning(){ @H_403_65@ super.didReceiveMemoryWarning()
- //DispoSEOfanyresourcesthatcanberecreated.
- //双击屏幕时会调用此方法,放大和缩小图片
- funchandleTapGesture(sender:UITapGestureRecognizer){
- //判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小
- ifim.contentMode==UIViewContentMode.ScaleAspectFit{
- //把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView
- im.contentMode=UIViewContentMode.Center @H_403_65@ }else{
- im.contentMode=UIViewContentMode.ScaleAspectFit
- //捏的手势,使图片放大和缩小,捏的动作是一个连续的动作 @H_403_65@ funchandlePinchGesture(sender:UIPinchGestureRecognizer){
- varfactor=sender.scale @H_403_65@ iffactor>1{
- //图片放大 @H_403_65@ im.transform=CGAffineTransformMakeScale(lastScaleFactor+factor-1,lastScaleFactor+factor-1)
- }else{
- //缩小
- im.transform=CGAffineTransformMakeScale(lastScaleFactor*factor,lastScaleFactor*factor)
- //状态是否结束,如果结束保存数据 @H_403_65@ ifsender.state==UIGestureRecognizerState.Ended{
- iffactor>1{ @H_403_65@ lastScaleFactor=lastScaleFactor+factor-1
- lastScaleFactor=lastScaleFactor*factor
- //旋转手势 @H_403_65@ funchandleRotateGesture(sender:UIRotationGestureRecognizer){
- //浮点类型,得到sender的旋转度数 @H_403_65@ varrotation:CGFloat=sender.rotation
- //旋转角度CGAffineTransformMakeRotation,改变图像角度 @H_403_65@ im.transform=CGAffineTransformMakeRotation(rotation+netRotation)
- //状态结束,保存数据 @H_403_65@ netRotation+=rotation
- //拖手势
- funchandlePanGesture(sender:UIPanGestureRecognizer){
- //得到拖的过程中的xy坐标
- vartranslation:CGPoint=sender.translationInView(im)
- //平移图片CGAffineTransformMakeTranslation
- im.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x,netTranslation.y+translation.y) @H_403_65@ netTranslation.x+=translation.x @H_403_65@ netTranslation.y+=translation.y
- funchandleSwipeGesture(sender:UISwipeGestureRecognizer){
- //划动的方向 @H_403_65@ vardirection=sender.direction
- //判断是上下左右 @H_403_65@ switch(direction){
- caseUISwipeGestureRecognizerDirection.Left: @H_403_65@ println("Left")
- imageIndex++;//下标++ @H_403_65@ break
- caseUISwipeGestureRecognizerDirection.Right: @H_403_65@ println("Right")
- imageIndex--;//下标-- @H_403_65@ caseUISwipeGestureRecognizerDirection.Up: @H_403_65@ println("Up")
- break @H_403_65@ caseUISwipeGestureRecognizerDirection.Down:
- println("Down") @H_403_65@ default: @H_403_65@ break;
- //得到不越界不<0的下标
- imageIndex=imageIndex<0?images.count-1:imageIndex%images.count
- //imageView显示图片
- im.image=UIImage(named:images[imageIndex]asString) @H_403_65@ funchandleLongpressGesture(sender:UILongPressGestureRecognizer){ @H_403_65@ ifsender.state==UIGestureRecognizerState.Began{
- //创建警告
- varactionSheet=UIActionSheet(title:"Imageoptions",0); background-color:inherit">delegate:self,0); background-color:inherit">cancelButtonTitle:"cancel",0); background-color:inherit">destructiveButtonTitle:"ok",0); background-color:inherit">otherButtonTitles:"other") @H_403_65@ actionSheet.showInView(self.view)
- }