//
// ViewController.h
// UI2_UIGesture
//
// Created by zhangxueming on 15/7/9.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UIGestureRecognizerDelegate> @end
//
// ViewController.m
// UI2_UIGesture
//
// Created by zhangxueming on 15/7/9.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *names = @[@"blue",@"red",@"yellow"];
for (int i=0; i<3; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100*i, 180+100*i, 100, 100)];
imageView.image = [UIImage imageNamed:names[i]];
[self.view addSubview:imageView];
//打开用户交互使能
imageView.userInteractionEnabled = YES; //添加点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
//设置点击次数
tap.numberOfTapsRequired = 1;
//设置手指个数
tap.numberOfTouchesRequired = 2;
//给imageView 添加手势
[imageView addGestureRecognizer:tap]; //添加长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
longPress.numberOfTapsRequired = 0;
//longPress.numberOfTouchesRequired = 2;
[imageView addGestureRecognizer:longPress]; //添加移动手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[imageView addGestureRecognizer:pan]; //捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[imageView addGestureRecognizer:pinch];
//旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[imageView addGestureRecognizer:rotation];
//轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[imageView addGestureRecognizer:swipe];
}
} - (void)tapGesture:(UITapGestureRecognizer *)tap
{
NSLog(@"图片被点击");
} - (void)longPressGesture:(UILongPressGestureRecognizer *)longPress
{
NSLog(@"长按手势被触发");
} - (void)panGesture:(UIPanGestureRecognizer *)pan
{
NSLog(@"移动手势被触发");//5 10 15 5 5 5
UIView *view = pan.view;//5+5+5
//获取手势的偏移量
CGPoint px = [pan translationInView:self.view];
if(pan.state == UIGestureRecognizerStateBegan || pan.state == UIGestureRecognizerStateChanged)
{
//改变手势对应的view中心点坐标
pan.view.center = CGPointMake(view.center.x+px.x, view.center.y+px.y);
}
//设置偏移量为0;
[pan setTranslation:CGPointZero inView:self.view];
} - (void)pinchGesture:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"捏合手势被触发");
if(pinch.scale == UIGestureRecognizerStateBegan || pinch.scale ==UIGestureRecognizerStateChanged)
{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
}
//设置系数为1
pinch.scale = 1.0;
} - (void)rotationGesture:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"旋转手势被触发");
if (rotation.state == UIGestureRecognizerStateBegan || rotation.state == UIGestureRecognizerStateChanged) {
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
} rotation.rotation = 0.0;
} - (void)swipeGesture:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"轻扫手势被触发");
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

UI2_UIGesture的更多相关文章

随机推荐

  1. 【机器学习算法-python实现】决策树-Decision tree(1) 信息熵划分数据集

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 决策书算法是一种逼近离散数值的分类算法,思路比較简单,并且准确率较高.国际权威的学术组织,数据挖掘国际 ...

  2. 专注网格剖分 - TetGen

    提要 记得大三那一年有一门课叫做高等有限元,最后的作业就是网格剖分算法的实现,我和同学一起花了些时间做了一个Qt程序,他写算法,我写界面,最后成绩竟然出奇的拿了90多... 今天要介绍的这款软件Tet ...

  3. 强连通分量(LRJ训练指南)

    #include <iostream> #include <queue> #include <string> #include <cstdio> #in ...

  4. iOS和android游戏纹理优化和内存优化(cocos2d-x)

    1.2d游戏最占内存的无疑是图片资源. 2.cocos2d-x不同平台读取纹理的机制不同.ios下面使用 CGImage,android和windows下是直接调用png库.我测试了下,使用png库直 ...

  5. HTML目录

    1. 表格标记 2. HTML常用标记 3. 博客园添加访问人数统计 4. 如何快速掌握CSS(各种CSS工具) 5. HTTP协议状态码详解(HTTP Status Code)(转)

  6. 海量数据处理算法—Bit-Map

    原文:http://blog.csdn.net/hguisu/article/details/7880288 1. Bit Map算法简介 来自于<编程珠玑>.所谓的Bit-map就是用一 ...

  7. python脚本初探---新手如何直接编写一个hello world模块即可执行的.py文件

    废话不多说,就讲一下这个背景吧: 事情是这个样子的~ 本着好学的精神,咱就买了本书,学习python结果呢,发现python的教程都是一个样子滴,上来的第一个hello world 都是通过IDLE来 ...

  8. C# 代码生成工具 Millennials

    Millennials 是一个可定制的 C# 代码生成工具,支持 MVC 和三层架构.ADO.NET.Nhibernate 和 LINQ. 项目主页:http://www.open-open.com/ ...

  9. Socket 使用笔记与注意事项(一)

    SocketAsyncEventArgs 1.该参数可以重复使用. 2.SocketAsyncEventArgs 的事件执行触发之后可以使用. 3.SocketAsyncEventArgs 的事件还在 ...

  10. TCP/IP协议原理与应用笔记26:网际协议(IP)之 分片(Fragmentation)

    1. 分片(Fragmentation) 适应在不同的MTU的物理网上传输. 备注: MTU:最大传输单元,Maximum Transmission Unit,它是指一种通信协议的某一层上面所能通过的 ...