UI2_UIGesture
//
// 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的更多相关文章
随机推荐
- [AngularJS] Using Services in Angular Directives
Directives have dependencies too, and you can use dependency injection to provide services for your ...
- css常用知识
1.基本语法规范p {color:#ff0000;background:#ffffff}a.其中"p"称为"选择器"(selectors),指明我们要给&quo ...
- 0c-40-ARC下多对象内存管理
1个人拥有1条狗. 问题1:人拥有狗作为成员变量,此时使用weak,释放过程是什么样? Person *p = [Person new]; Dog *d = [Dog new]; //设置人拥有dog ...
- objective-c中使用cocoa的NSPredicate,谓词(十四)
holydancer原创,如需转载,请在显要位置注明: 转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details ...
- Golang学习 - strconv 包
------------------------------------------------------------ // 将布尔值转换为字符串 true 或 false func FormatB ...
- Android小项目之十 应用程序更新的签名问题
------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...
- Oracle基础 TO_CHAR函数参考(转)
Postgres 格式化函数提供一套有效的工具用于把各种数据类型(日期/时间,int,float,numeric)转换成格式化的字符串以及反过来从格式化的字符串转换成原始的数据类型. 注意:所有格式化 ...
- [Android]天气App 2 项目搭建
对于天气App,为了简化一些功能,暂时模仿MUUI系统提供的那个App. 本项目需要引入本人经常使用的一个工具库DroidTool,这个是本人根据工作中,收集到一些工具类,下载地址. ...
- LearnMVC5-AddAView
原创文章,转载必需注明出:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/learnmvc5-addaview/ 添加视图 在本章节 ...
- Linux常用命令之seq
标题:seq命令的使用 作用:seq命令用于以指定增量从首数开始打印数字到尾数,即产生从某个数到另外一个数之间的所有整数,并且可以对整数的格式.宽度.分割符号进行控制 语法: [1] seq [选项] ...