手势的使用方法
来源:互联网 发布:怎样加入淘宝客推广 编辑:程序博客网 时间:2024/06/06 13:24
手势的使用方法
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutletUIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//------------------------------轻触手势---------------------------
//1.创建手势识别对象
UITapGestureRecognizer * tap = [[UITapGestureRecognizeralloc] init];
//2.设置对象属性
//2.1设置点击次数
// tap.numberOfTapsRequired = 2;
//2.2 设置手指个数
// tap.numberOfTouchesRequired = 2;
//2.3设置代理
tap.delegate = self;
//3.添加监听事件
[tap addTarget:selfaction:@selector(tap:)];
//4.把手势对象添加到要作用的view上
[self.imageViewaddGestureRecognizer:tap];
//------------------------------轻触手势---------------------------
//------------------------------长按手势---------------------------
//1.创建手势对象
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPress:)];
//2.设置对象属性
//2.1 设置长按时间
longPress.minimumPressDuration =2;
//2.2 设置允许移动误差
longPress.allowableMovement = 30;
//3.添加监听事件
//4.添加到要作用的view上
[self.imageViewaddGestureRecognizer:longPress];
//------------------------------长按手势---------------------------
//------------------------------拖拽手势---------------------------
//1.创建手势对象
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(pan:)];
//2.设置属性
//3.监听事件
//4.添加到要作用的view上
[self.imageViewaddGestureRecognizer:pan];
//------------------------------拖拽手势---------------------------
}
//拖拽手势方法
- (void) pan:(UIPanGestureRecognizer *) panGesture
{
CGPoint point = [panGesture translationInView:panGesture.view];
NSLog(@"%@",NSStringFromCGPoint(point));
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x
, point.y);
//每次拖拽后的数据清零,防止数值累加
[panGesture setTranslation:CGPointZeroinView:panGesture.view];
// self.imageView.transform = CGAffineTransformMakeTranslation(point.x, point.y);
}
=======================
//给按钮添加拖动的手势
-(void)addGestureToSuspendedBtn:(UIPanGestureRecognizer *)pan {
// //获取当前作用点的坐标
// CGPoint p = [pan locationInView:pan.view];
//获取移动的距离大小
CGPoint point= [pan translationInView:pan.view];
//更改视图的中心点坐标
CGPoint points=pan.view.center;
points.x+=point.x;
points.y+=point.y;
pan.view.center=points;
//每次都清空一下消除坐标叠加(把当前手势所在的点的坐标变为(0,0))
[pan setTranslation:CGPointZeroinView:self.btn];
}
=================
//长按手势方法
- (void) longPress:(UILongPressGestureRecognizer *) longGesture
{
if (longGesture.state ==UIGestureRecognizerStateBegan)
{
[UIViewanimateWithDuration:0.5animations:^{
self.imageView.alpha =0.2;
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:0.5animations:^{
self.imageView.alpha =1;
}];
}];
NSLog(@"longPress");
}
}
//实现代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
//通过touch获取点击坐标
CGPoint point = [touch locationInView:touch.view];
if(point.y >self.imageView.bounds.size.height/2)
{
return NO;
}
return YES;
}
- (void) tap:(UITapGestureRecognizer *) tapGesture
{
NSLog(@"我点!!!");
}
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutletUIImageView *imageView;
@property (nonatomic,assign)int index;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化
self.index =1;
//--------------捏合手势---------------------------
//1.创建手势
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinch:)];
//2.设置属性
//3.监听事件
//设置代理
pinch.delegate = self;
//4.添加到要作用的view上
[self.imageViewaddGestureRecognizer:pinch];
//--------------捏合手势---------------------------
//--------------旋转手势---------------------------
//1.创建手势对象
UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizeralloc] initWithTarget:selfaction:@selector(rotation:)];
//设置代理
rotation.delegate = self;
//2.添加到要做用的view上
[self.imageViewaddGestureRecognizer:rotation];
//--------------旋转手势---------------------------
//--------------轻扫手势---------------------------
//1.创建手势对象
//向左下一张
UISwipeGestureRecognizer * leftSwipe = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipe:)];
//设置轻扫方向
leftSwipe.direction =UISwipeGestureRecognizerDirectionLeft;
//2.添加要作用的view上
[self.imageViewaddGestureRecognizer:leftSwipe];
//向右上一张
UISwipeGestureRecognizer * rightSwipe = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipe:)];
//设置方向
rightSwipe.direction =UISwipeGestureRecognizerDirectionRight;
[self.imageViewaddGestureRecognizer:rightSwipe];
//--------------轻扫手势---------------------------
}
//实现轻扫的方法
- (void) swipe:(UISwipeGestureRecognizer *) swiprGesture
{
//添加动画效果
CATransition * trans = [CATransitionanimation];
trans.type = @"cube";
//判断清扫方向
if (swiprGesture.direction ==UISwipeGestureRecognizerDirectionLeft)
{
//下一张
self.index++;
trans.subtype =kCATransitionFromRight;
}
elseif (swiprGesture.direction ==UISwipeGestureRecognizerDirectionRight)
{
//上一张
self.index--;
trans.subtype =kCATransitionFromLeft;
}
//让图片循环
if (self.index >4)
{
self.index =1;
}
else if (self.index <1)
{
self.index =4;
}
//拼接图片名
NSString * imageName = [NSStringstringWithFormat:@"img%02d",self.index];
self.imageView.image = [UIImageimageNamed:imageName];
[self.imageView.layeraddAnimation:trans forKey:nil];
}
//是否支持同时多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
//实现旋转手势方法
- (void) rotation:(UIRotationGestureRecognizer *) rotationGesture
{
NSLog(@"%f",rotationGesture.rotation);
self.imageView.transform =CGAffineTransformRotate(self.imageView.transform, rotationGesture.rotation);
rotationGesture.rotation = 0;
// self.imageView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
}
//实现捏合手势事件方法
- (void) pinch:(UIPinchGestureRecognizer *) pinchGesture
{
NSLog(@"%f",pinchGesture.scale);
self.imageView.transform =CGAffineTransformScale(self.imageView.transform, pinchGesture.scale, pinchGesture.scale);
pinchGesture.scale = 1;
// self.imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
}
- 手势的使用方法
- 史上最全IOS的touch事件手势使用方法
- UITapGestureRecognizer 手势使用方法
- UITapGestureRecognizer 手势使用方法
- Android 滑动手势简单使用方法
- Delphi-XE5-手势操作-Gestures-使用方法
- Delphi-XE5-手势操作-Gestures-使用方法
- 定义响应的手势类:长按手势,缩放手势
- 添加手势 向右滑的手势
- 交警手势的理解
- “打架”的鼠标手势
- android 手势的学习
- 手势的学习实践
- android的手势识别
- ios手势的综合
- 手势的基本用法
- iOS 手势的用法
- Android的手势交互
- Quartz2d水印图片做法
- 一键把.app 输出为 .ipa
- UVa 725 Division
- 创建Linux内核函数的Man手册
- ZOJ 3940 Modulo Query(优先队列)
- 手势的使用方法
- 美团Android DEX自动拆包及动态加载简介
- C++以任意一个字符为分隔符,读取一行内容,显示到控制台
- Java编程入门:前言
- ListView封装实现下拉刷新和上拉加载
- BMP图片格式详细解析--以256色为例还原256色BMP图像数据
- Android gradle proguard 代码混淆详解
- linux上NFS客户端登陆错误
- Android夜间模式初探