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的更多相关文章
随机推荐
- codeforces Round #258(div2) D解题报告
D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 一天掌握Android JNI本地编程 快速入门
一.JNI(Java Native Interface) 1.什么是JNI: JNI(Java Native Interface):java本地开发接口 ...
- PHP __autoload函数(自动载入类文件)的使用方法(转)
详细出处参考:http://www.jb51.net/article/29625.htm 在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都存放在一个单独的文件里,这样会很容易实现对类进 ...
- select,epoll的比较
机制: select:只支持水平触发(数据不处理完无限通知) epoll:支持水平触发和边缘触发(仅通知一次) 单进程监控FD个数 select: 由FD_SETSIZE设置,默认值是2048.在大量 ...
- ClamAV
ClamAV 简介以及适用范围 ClamAV是一个在命令行下查毒软件,因为它不将杀毒作为主要功能,默认只能查出您计算机内的病毒,但是无法清除,至多删除文件.ClamAV可以工作很多的平台上,但是有少数 ...
- c++ 设计模式1
从面向对象谈起 1) 底层思维:向下,如何把握及其底层,从微观理解对象构造 (语言构造.编译转换.内存模型.运行时机制) 抽象思维: 向上,如何将我们的周围世界抽象为程序代码 (面向对象.组件封装 ...
- Struts2+hibernate3+Spring2的整合方法
浅谈Struts+hibernate+Spring的整合方法 摘要:本文将介绍Struts,Spring与hibernate的集成.希望大家能从中受用. 1.在工程中导入spring支持,导入的Jar ...
- DIH处理包含回车符换行符html标签内容的文本
数据样例:2010-03-19 10:18:06130010543234203guqun09-12月-12liuyin18-6月 -14<P style="MARGIN-TOP: 0p ...
- shake震动动画
- CALayer实现遮罩效果
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong)CALayer ...