// ViewController.swift
// 7.2-UIImage
//
// Created by mac on 16/7/2.
// Copyright © 2016年 zhang. All rights reserved.
//
import UIKit
let BOUNDS = UIScreen.mainScreen().bounds;
class ViewController: UIViewController {
//定义一个图片
var cureImage: UIImageView!
var sizeRect:CGSize = CGSizeMake(200, 200)
var netRotation:CGFloat = 1.0 //旋转
var lastScaleFactor : CGFloat! = 1 //放大、缩小
override func viewDidLoad() {
super.viewDidLoad()
//初始化图片
setImage();
//添加手势
addGertrue();
}
func setImage()
{
self.view.backgroundColor = UIColor.whiteColor();
cureImage = UIImageView()
cureImage.image = UIImage(named: "0")
cureImage.frame.size = sizeRect;
cureImage.center = self.view.center;
self.view.addSubview(cureImage);
}
//添加手势
func addGertrue()
{
//1.双击手势
let doubleTap = UITapGestureRecognizer(target: self, action: "clickDoubleTap:")
doubleTap.numberOfTapsRequired = 2
self.view.addGestureRecognizer(doubleTap);
//2.拖动手势
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action:"panGestureRecognizer:")
self.view.addGestureRecognizer(panGestureRecognizer);
//3.旋转手势
let RotationGesture = UIRotationGestureRecognizer (target: self, action:"rotationGestureClick:")
self.view.addGestureRecognizer(RotationGesture);
//4.捏合手势
let pinchGesture = UIPinchGestureRecognizer(target: self, action:"pinchGestureClick:");
self.view.addGestureRecognizer(pinchGesture)
//划动手势
//右划
let swipeGesture = UISwipeGestureRecognizer(target: self, action:"handleSwipeGesture:")
self.view.addGestureRecognizer(swipeGesture)
//左划
let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action:"handleSwipeGesture:")
swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left //不设置是右
self.view.addGestureRecognizer(swipeLeftGesture)
//长按手势
let longpressGesutre = UILongPressGestureRecognizer(target: self, action:"handleLongpressGesture:")
//长按时间为1秒
longpressGesutre.minimumPressDuration = 1
//允许15秒运动
longpressGesutre.allowableMovement = 15
//所需触摸1次
longpressGesutre.numberOfTouchesRequired = 1
self.view.addGestureRecognizer(longpressGesutre)
}
//实现手势方法 双击手势
func clickDoubleTap(sender: UITapGestureRecognizer)
{
let new = CGSizeMake(BOUNDS.size.width, BOUNDS.size.height)
if cureImage.frame.size == new {
cureImage.frame.size = sizeRect;
}else{
cureImage.frame.size = new;
}
cureImage.center = self.view.center;
}
//移动手势
func panGestureRecognizer(sender:UIPanGestureRecognizer)
{
let translation = sender.translationInView(cureImage)
cureImage.center = CGPointMake(cureImage.center.x + translation.x,cureImage.center.y + translation.y)
sender.setTranslation(CGPointZero, inView: cureImage)
}
//旋转手势
func rotationGestureClick(sender: UIRotationGestureRecognizer)
{
//浮点类型,得到sender的旋转度数
let rotation : CGFloat = sender.rotation
//旋转角度CGAffineTransformMakeRotation,改变图像角度
cureImage.transform = CGAffineTransformMakeRotation(rotation+netRotation)
//状态结束,保存数据
if sender.state == UIGestureRecognizerState.Ended{
netRotation += rotation
}
}
//捏合手势
func pinchGestureClick(sender: UIPinchGestureRecognizer)
{
let factor = sender.scale
if factor > 1{
//图片放大
cureImage.transform = CGAffineTransformMakeScale(lastScaleFactor+factor-1,lastScaleFactor+factor-1)
}else{
//缩小
cureImage.transform = CGAffineTransformMakeScale(lastScaleFactor*factor,lastScaleFactor*factor)
}
//状态是否结束,如果结束保存数据
if sender.state == UIGestureRecognizerState.Ended{
if factor > 1{
lastScaleFactor = lastScaleFactor + factor - 1
}else{
lastScaleFactor = lastScaleFactor * factor
}
}
}
//划动手势
func handleSwipeGesture(sender: UISwipeGestureRecognizer){
//划动的方向
let direction = sender.direction
//判断是上下左右
switch (direction){
case UISwipeGestureRecognizerDirection.Left:
print("Left")
break
case UISwipeGestureRecognizerDirection.Right:
print("Right")
break
case UISwipeGestureRecognizerDirection.Up:
print("Up;")
break
case UISwipeGestureRecognizerDirection.Down:
print("Down")
break
default:
break;
}
}
//长按手势
func handleLongpressGesture(sender : UILongPressGestureRecognizer){
print("你再按我一个试试!!!")
}
}