IOS开发之UI手势
2019独角兽企业重金招聘Python工程师标准>>>
点击手势和双击手势
#import "TapViewController.h"
@interface TapViewController ()
@end
@implementation TapViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//点击手势:
//=================单击===========
//1.创建一个点击手势对象
//UIGestureRecognizer是所有手势的父类,一般不会直接使用哪个它
//而是使用它的子类
//参数1:响应手势的对象;
//参数2:响应的消息
//功能:发生点击动作,对象去响应这个手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer
alloc]initWithTarget:self action:@selector(tapGesture:)];
//2.添加手势到指定的视图上(任何手势都可以添加到任何继承自UIView的类的对象上)
[self.view addGestureRecognizer:tapGesture];
//=================双击===========
//1.创建点击对象
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(doubleTap:)];
//2.设置点击次数
doubleTap.numberOfTapsRequired = 2;
//4.忽略其他手势
//单击的时候忽略双击;
[tapGesture requireGestureRecognizerToFail:doubleTap];
//3.将手势添加到指定的视图控制器上
[self.view addGestureRecognizer:doubleTap];
}
#pragma mark -单击手势响应
- (void)tapGesture:(UITapGestureRecognizer *)tap{
//获取点击手势的坐标
CGPoint point = [tap locationInView:self.view];
// NSLog(@"%@",NSStringFromCGPoint(point));
NSLog(@"单击");
}
#pragma mark - 双击事件响应
- (void)doubleTap:(UIGestureRecognizer *)doubleTap{
//获取点击手势的坐标
CGPoint point = [doubleTap locationInView:self.view];
//NSLog(@"%@",NSStringFromCGPoint(point));
NSLog(@"双击");
}
@end
长按手势:关键在于设置手势的时间
#import "LongPressViewController.h"
@interface LongPressViewController ()
@end
@implementation LongPressViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建长按手势对象
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
//3.设置长按时间
longPress.minimumPressDuration = 1;
//2.将手势添加到视图控制器上;
[self.view addGestureRecognizer:longPress];
}
#pragma mark -响应长按手势
//这个方法在手势的状态发生改变的时候调用
- (void)longPress : (UILongPressGestureRecognizer *)longPress{
//手势的状态:(针对所有的手势都适用)
//UIGestureRecognizerStateBegan:手势开始
//UIGestureRecognizerStateChanged:手势改变
//UIGestureRecognizerStateEnded:手势结束
//longPress setState:<#(UIGestureRecognizerState)#>
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按");
}
// NSLog(@"长按");
}
@end
滑动手势:关键在于设置滑动的方向
#import "SwipeViewController.h"
@interface SwipeViewController ()
@end
@implementation SwipeViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建滑动手势对象
UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(swipGesture:)];
//3.滑动方向
//注意:如果想要可以两个方向都可以,滑动方向必须添加一个右划的手势
[swip setDirection:UISwipeGestureRecognizerDirectionLeft];
//2.将手势添加到视图控制器上
[self.view addGestureRecognizer:swip];
//=============右划默认是从左向右滑动==========
UISwipeGestureRecognizer *swipRight = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(swipGesture:)];
// [swip setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipRight];
}
#pragma mark - 滑动手势响应事件
- (void)swipGesture:(UISwipeGestureRecognizer *)swipGesture{
if (swipGesture.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"右滑回到下一页");
}else{
NSLog(@"左滑到上一页");
}
}
@end
拖动手势
#import "PanViewController.h"
@interface PanViewController ()
@end
@implementation PanViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建拖动手势对象
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(pan:)];
//2.添加到视图
[self.imageView addGestureRecognizer:panGesture];
}
//这个方法会在拖动过程中时时调用;
#pragma mark - 拖动手势方法
- (void)pan:(UIPanGestureRecognizer *)pan{
self.imageView.center = [pan locationInView:self.view];
NSLog(@"拖动");
}
@end
旋转手势:
#import "RotationViewController.h"
@interface RotationViewController ()
@end
@implementation RotationViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//1.创建旋转手势对象
UIRotationGestureRecognizer *rotatioin = [[UIRotationGestureRecognizer alloc]
initWithTarget:self action:@selector(rotationG:)];
//2.添加到视图上
[self.imageView addGestureRecognizer:rotatioin];
}
#pragma mark - 旋转手势事件
- (void) rotationG:(UIRotationGestureRecognizer *)ro{
static CGFloat lastRotation = 0;
//® self.imageView.center = [rotation locationInView:self.view];
CGFloat triangle = ro.rotation + lastRotation;
self.imageView.transform = CGAffineTransformMakeRotation(triangle);
if (ro.state == UIGestureRecognizerStateEnded) {
lastRotation = ro.rotation + lastRotation;
}
NSLog(@"旋转");
}
@end
缩放手势
#import "PinchViewController.h"
@interface PinchViewController ()
@end
@implementation PinchViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//1.创建pinch手势对象
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(pinchGesture:)];
//2.添加到视图上
[self.view addGestureRecognizer:pinch];
}
- (void)pinchGesture:(UIPinchGestureRecognizer *)pinch{
static CGFloat lastScale =1;
CGFloat scale = pinch.scale;
//3.通过手势的缩放比例去改变图片的缩放形变;
[self.imageView setTransform:CGAffineTransformMakeScale(scale, scale)];
if (pinch.state == UIGestureRecognizerStateEnded) {
lastScale = pinch.scale * lastScale;
}
NSLog(@"缩放");
}
@end
转载于:https://my.oschina.net/luhoney/blog/666058
IOS开发之UI手势相关推荐
- IOS开发之UI基础LOL英雄展示-15
IOS开发之UI基础LOL英雄展示-15 // // ViewController.m // 15-英雄展示-单组数据 // // Created by 鲁军 on 2021/2/3. //#impo ...
- iOS开发之UI开发(UITableView)
UITableView 继承自UIScrollView,性能极佳 UITableView的两种样式 UITableViewStylePlain列表样式 UITableViewStyleGrouped ...
- (0107)iOS开发之UI实时调试InjectionIII的使用
AppStore : 下载:Injectionlll 下载地址 使用步骤: 1.设置InjectionIII 打开InjectionIII工具,选择Open Project,选择你的代码所在的路径,然 ...
- (0010) iOS 开发之UI布局兼容 4s/5/6/7 屏幕解决方案
iOS开发技术分享群 147787076 如上带标注的效果图,是1080*1920 也就是6p 的尺寸.如何根据6p的标注,在各种iPhone 尺寸上完美适配尼? 适用:UI 各屏幕/分辨率适配方案( ...
- iOS开发之AVKit框架使用
2019独角兽企业重金招聘Python工程师标准>>> iOS开发之AVKit框架使用 一.引言 在iOS开发框架中,AVKit是一个非常上层,偏应用的框架,它是基于AVFounda ...
- (0045) iOS 开发之MBProgressHUD 源码学习
(0045) iOS 开发之MBProgressHUD 源码学习 第一部分:学习所得和分析线程 1. 学习到了kvo 的使用 和屏幕方向的旋转判断. 2. 如果调起这个 HUD 的方法不是在主线程调 ...
- (0016)iOS 开发之Mac上Navicat Premium 创建远程连接和本地连接
1.下载安装 (百度云盘里面有安装文件和注册机) 链接: https://pan.baidu.com/s/1kVG1k71 密码: mr5g 破解教程看这篇博客:http://blog.csdn.ne ...
- 李洪强iOS开发之RunLoop的原理和核心机制
李洪强iOS开发之RunLoop的原理和核心机制 搞iOS之后一直没有深入研究过RunLoop,非常的惭愧.刚好前一阵子负责性能优化项目,需要利用RunLoop做性能优化和性能检测,趁着这个机会深入研 ...
- IOS开发之MD5加密和钥匙串的使用-oc
IOS开发之MD5加密和钥匙串的使用-oc 源码在我的主页,md5加密是用户登录安全的一个保障.不可逆的,可以暴力破解的. // // ViewController.m // MD5演练 // // ...
最新文章
- shell脚本中获取当前所在目录地址
- 瞬间教你学会使用java中list的retainAll方法
- 潭州课堂25班:Ph201805201 并发(非阻塞,epoll) 第十课 (课堂笔记)
- linux创建名称为学号的文件,Linux操作系统下用户管理及文件管理.doc
- sql server 跟踪_SQL Server作业性能–跟踪
- [转载] 【python】str与json类型转换
- .net知识和学习方法系列(二十一)CLR-枚举
- 个人的Directx9研究总结 (1)
- java请假系统毕业设计_jsp企业员工考勤管理系统
- ae插件form_四分钟了解全网最受欢迎的10大AE插件
- 公司内网openvpn部署,
- 探索数藏未来 秦储数字藏品座谈会成功召开
- 2018年第九届蓝桥杯真题解析 | 日志统计【Python】
- 使用__vmx_vmwrite遇到的问题
- WBS 及 WBS字典
- 单片机STM32的5个时钟源知识,你不能错过。
- c语言中英文字母的符号,C语言中的符号(国外英文资料).doc
- WinEdt引用参考文献
- Arch Linux 系统迁移
- Diem(原Libra)学习笔记