UI中各种手势的使用点击,捏合,清扫,旋转,平移,边缘移动,长按
#import "RootViewController.h"
@interface RootViewController ()
{
UIImageView *imageView;
NSInteger number;//用于存放图片的名字
BOOL flag;//用于标记当前是哪张图片
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
//默认值是yes;用于点击事件更改照片
flag = YES;
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"美女1.png"]];
imageView.frame = [UIScreen mainScreen].bounds;
//contentMode内容模式
//UIViewContentModeScaleToFill规模填充
//UIViewContentModeScaleAspectFit内容符合固定方面。剩余部分是透明的
//UIViewContentModeScaleAspectFill内容扩展与固定方面填补。可能是剪的一部分内容。
//UIViewContentModeRedraw重划边界变化(调用-setNeedsDisplay)
//UIViewContentModeCenter内容保持相同大小。定位调整。
imageView.contentMode = UIViewContentModeScaleToFill;
//UIIamgeView 和 UILabel一样,默认的交互是关闭的NO
//所以用userInteractionEnabled = YES
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
[imageView release];
//UISegmentedControl,分段控制器,继承于UIControl
//参数:参数类型,数组内存放每个分段控制按钮的标题
NSArray *items = @[@"点击", @"捏合", @"旋转", @"清扫", @"平移", @"边缘移动", @"长按"];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
segmentedControl.frame = CGRectMake(0, 20, self.view.frame.size.width, 40);
//关联方法
//分段控制器,触发事件是UIControlEventValueChanged//和button的不一样UIControEventTouchUpInside
[segmentedControl addTarget:self action:@selector(changeGesture:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
[segmentedControl release];
// Do any additional setup after loading the view.
}
- (void)changeGesture:(UISegmentedControl *)aControl
{
//选中的是第几个区域
//selectedSegmentIndex, 从0开始依次增加
NSLog(@"%ld", aControl.selectedSegmentIndex);
//抽象基类的特点:不能够直接使用,必须通过子类化的形式才能使用,换言之,只能使用子类
//UIGestureRecognizer,手势识别器,其实是对触摸的封装,是抽象基类
//移除之前的手势
for (UIGestureRecognizer *gesture in imageView.gestureRecognizers) {
[imageView removeGestureRecognizer:gesture];
}
switch (aControl.selectedSegmentIndex) {
case 0://单击
{
//单击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesture)];
//单击次数
tap.numberOfTapsRequired = 2;
//需要几个手指同时点击
tap.numberOfTouchesRequired = 1;
//把手势添加到某个视图上
[imageView addGestureRecognizer:tap];
//释放
[tap release];
}
break;
case 1://捏合
{
//捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[imageView addGestureRecognizer:pinch];
[pinch release];
}
break;
case 2://旋转
{ //旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
[imageView addGestureRecognizer:rotation];
[rotation release];
}
break;
case 3://清扫
{//清扫手势
// imageView.image = [UIImage imageNamed:@"美女1.png"];
// imageView.contentMode = UIViewContentModeScaleToFill;
//向左滑动
UISwipeGestureRecognizer *leftWipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swape:)];
//设置初始值1
number = 1;
//设置方向, 左右(只支持一个方向)
leftWipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:leftWipe];
[leftWipe release];
//向右滑动
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swape:)];
//设置初始值1
number = 1;
//设置方向, 左右(只支持一个方向)
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:rightSwipe];
[rightSwipe release];
}
break;
case 4://平移
{
//平移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[imageView addGestureRecognizer:pan];
[pan release];
}
break;
case 5://边缘移动//扁平化之后才有的
{
//注意:边缘移动手势触摸的机制,必须保证视图紧贴屏幕的边缘
UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePan)];
//边缘(支持一个方向)
edgePan.edges = UIRectEdgeLeft;
edgePan.edges = UIRectEdgeRight;
[imageView addGestureRecognizer:edgePan];
[edgePan release];
}
break;
case 6://长按
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
//时间相应(按多长时间)
longPress.minimumPressDuration = 1.0;
[imageView addGestureRecognizer:longPress];
[longPress release];
}
break;
default:
break;
}
}
#pragma mark ---单击响应方法的实现
/**
* 单击响应方法的实现
通过点击实现两个图片的交换显示
*/
- (void)gesture
{
//1.第一种方法
// if (flag) {
// imageView.image = [UIImage imageNamed:@"美女1.png"];
// imageView.contentMode = UIViewContentModeScaleToFill;
// flag = NO;
// } else {
//
// imageView.image = [UIImage imageNamed:@"美女2.png"];
// imageView.contentMode = UIViewContentModeScaleToFill;//图片填充
// flag = YES;
//
// }
//2.第二种方法,三目运算
imageView.image = flag ? [UIImage imageNamed:@"美女2.png"] : [UIImage imageNamed:@"美女1.png"];
flag = !flag;
}
#pragma mark ---捏合方法的实现
- (void)pinch:(UIPinchGestureRecognizer *)aPinch
{
//aPinch.scale缩放比率
NSLog(@"%.2f", aPinch.scale);
//图像的变形 transform 是UIView的属性
//图像的变形
//CGAffineTransformScale(缩放比例, X轴, Y轴)
//参数1:原始图的transform
//参数2:X轴上改变的比率
//参数3:Y轴上改变的比率
imageView.transform = CGAffineTransformScale(imageView.transform, aPinch.scale, aPinch.scale);
//重置比率(是为了让图像的比率正常)
aPinch.scale = 1;
}
#pragma mark ---旋转方法的实现
- (void)rotation:(UIRotationGestureRecognizer *)aRotation
{
//旋转角度
NSLog(@"%.2f", aRotation.rotation);
//改变图片旋转角度
//参数1:视图原先的transform
//参数2:变化的角度
imageView.transform = CGAffineTransformRotate(imageView.transform, aRotation.rotation);
//重置角度
aRotation.rotation = 0;
}
#pragma mark ---清扫方法的实现
- (void)swape:(UISwipeGestureRecognizer *)aSwape
{
if (aSwape.direction == UISwipeGestureRecognizerDirectionLeft) {
number++;
if (number == 9) {//当向左滑动9次时,第9次变为从1张图片开始
number = 1;
}
}
if (aSwape.direction == UISwipeGestureRecognizerDirectionRight) {
number--;
if (number == 0) {//本身是1当向右滑动1次时变为0,变为从8张图片开始
number = 8;
}
}
NSString *name = [NSString stringWithFormat:@"美女%ld.png", number];
imageView.image = [UIImage imageNamed:name];
}
#pragma mark ---平移方法的实现
- (void)pan:(UIPanGestureRecognizer *)aPan
{
//手指移动的位置(X轴和Y轴偏移量)
CGPoint point = [aPan translationInView:imageView];
NSLog(@"%@", NSStringFromCGPoint(point));
// //更改center偏移量
// //方法1.
// CGPoint temp = imageView.center;
// temp.x += point.x;
// temp.y += point.y;
// imageView.center = temp;
// //重置point
// [aPan setTranslation:CGPointZero inView:imageView];
//
//方法2
//参数1:图片原有的transform
//参数2:X轴位移
//参数3:Y轴位移
imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);
[aPan setTranslation:CGPointZero inView:imageView];
}
#pragma mark ---边缘移动方法的实现
- (void)edgePan
{
NSLog(@"边缘");
}
#pragma mark ---长按方法的实现
- (void)longPress:(UILongPressGestureRecognizer *)aLongPress
{
//手势状态
//aLongPress.state
if (aLongPress.state == UIGestureRecognizerStateBegan) {
//屏幕截图
//绘制一张图片,屏幕大小
UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
//绘画结束
UIGraphicsEndImageContext();
//存到相册中
UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
// 结束触发
// if (aLongpress.state == UIGestureRecognizerStateEnded) {
// <#statements#>
// }
}
#pragma mark ---屏幕截图提示方法的实现
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo {
UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"图片已保存" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alterView show];
[alterView release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
UI中各种手势的使用点击,捏合,清扫,旋转,平移,边缘移动,长按的更多相关文章
- iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)
1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...
- iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义
1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...
- UI中的七种手势
// // GestureRecognizerViewController.m #import "GestureRecognizerViewController.h" #impor ...
- iOS中CollectionView由于多次点击造成错误的解决方案
iOS中CollectionCiew由于多次点击,会给程序造成错误. 这个时候,我们可以用过手势类来进行判断和过滤. 但是,有一个快捷的解决方法,那就是给用户响应增加延时操作. 具体代码如下: [co ...
- [转] 「指尖上的魔法」 - 谈谈 React Native 中的手势
http://gold.xitu.io/entry/55fa202960b28497519db23f React-Native是一款由Facebook开发并开源的框架,主要卖点是使用JavaScrip ...
- Android中的手势
Android对两种手势行为提供了支持:1.对于第一种手势行为而言,Android提供了手势检测,并为手势检测提供了相应的监听器.2.对于第二种手势行为,Android允许开发者添加手势,并提供了相应 ...
- jquery ui中 accordion的问题及我的解决方法
原文:jquery ui中 accordion的问题及我的解决方法 jquery有一套所谓的ui组件,很不错的.如果有兴趣的朋友,可以参考http://jqueryui.com/ 但其中的accord ...
- 关于Element UI中页面样式小问题
一,修改组件dialog窗口的大小 二,在我使用upload组件上传一张美女图片时,发现当预览图片时,图片是灰色的,点击一下才会变亮,这种效果使我很不舒服,于是我通过添加下面的一条样式,问题解决了(可 ...
- jquery ui中的dialog,官网上经典的例子
jquery ui中的dialog,官网上经典的例子 jquery ui中dialog和easy ui中的dialog很像,但是最近用到的时候全然没有印象,一段时间不用就忘记了,这篇随笔介绍一下这 ...
随机推荐
- C中的C文件与h文件辨析(转)
简单的说其实要理解C文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程,一般说来编译器会做以下几个过程: 1.预处理阶段 2.词法与语法分析阶段 3.编译阶段,首先编译成 ...
- js实现select跳转
js简单实现select跳转功能:代码例如以下 <!DOCTYPE html> <html> <head> <title></title> ...
- 【Java】Java_01初步
1.编程语言的发展史和发展主线 计算机语言如果你将它当做一个产品,就像我们平时用的电视机.剃须刀.电脑.手机等, 他的发展也是有规律的. 任何一个产品的发展规律都是:向着人更加容易使用.功能越来越强大 ...
- 由于删除DBF文件报错 —— ORA-01033: ORACLE initialization or shutdown in progress
由于移动或删除DBF文件报错:ORA-01033: ORACLE initialization or shutdown in progress 原因:一般该类故障通常是由于移动文件而影响了数据库日 ...
- 非常酷的 Javascript 简单调试工具Blackbird
Blackbird 是一个开源的 Javascript 调试工具,默认提供一种非常酷的方式展现 Javascript 调试信息,如下图,效果如何呢? 在我们的日常的学习或工作中,经常都会接触到 Jav ...
- Arthas安装问题
1. 下载安装 方式一: 安装Arthas: curl -L https://alibaba.github.io/arthas/install.sh | sh 启动Arthas: ./as.sh 报t ...
- C++语言基础(13)-抽象类和纯虚函数
一.基本语法 在C++中,可以将虚函数声明为纯虚函数,语法格式为: ; 纯虚函数没有函数体,只有函数声明,在虚函数声明的结尾加上=0,表明此函数为纯虚函数. 最后的=0并不表示函数返回值为0,它只起形 ...
- gdb 详解
环境:gcc (OpenWrt/Linaro GCC 4.8) 以如下的简单代码为例,说明gdb的使用. void func1(int a, int b) { int c; c = a + b; } ...
- centos6.5下redis集群配置(多机多节点)
可参考官网文档:redis集群配置 需要注意的是,集群中的每个节点都会涉及到两个端口,一个是用于处理客户端操作的(如下介绍到的6379/6380),另一个是10000+{监听端口},用于集群各个节点间 ...
- linux IP动态变动之后 , 需要做的杂项操作
linux的动态ip经常变来变去,目前还没找到固定它不变化的方法.所以每次变动之后都需要做以下的操作,极其麻烦.(必须找到让linux IP 固定的方法) 1.先找到变化之后的动态ip地址 ifcon ...