您的位置:首页 > 产品设计 > UI/UE

UI05_手势识别器

2015-08-08 22:00 316 查看
准备份工作:先建立一个显示图片

UIImage *image=[UIImage imageNamed:@"36CE666C27E56BAA8CC58C3A05342DC3.png"];
self.imageView=[[UIImageView alloc]initWithImage:image];
self.imageView.frame=CGRectMake(50, 100, 300, 300);
[self.view addSubview:self.imageView];
[_imageView release];


特别注意:
label和image的用户交互式关闭的
把图片的用户交互打开,他默认的是关闭的,此外还有一个控件是label
self.imageView.userInteractionEnabled=YES;


手势的使用

1.点击
(1)UITapGesturRecognizer *tap=[[UITapGesturRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
(2)设置点击几次会触发方法
tap.numberOfTapsRequired=2;
(3)设置几个手指进行点击
tap.numberOfTouchesRequired=2;
//将手势添加到对应的图片上
[self.imageView addGesturRecognizer:tap];
只要添加进去我们就可以释放掉
[tap release];
#pragma mark 点击方法
-(void)tapAction:(UITapGestureRecognizer *) gesture{
NSLog(@"测试一下");
self.imageView.image=[UIImage imageNamed:@"E9DD63DB371D340A9441F62B094BD63A.png"];
}


2.长按手势
(1) 创建
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongPressAction:)];
(2)设置长按触发的最小时间
longPress.minimumPressDuration=2;
(3)允许用户的手指的长按过程中允许移动的距离
longPress.allowableMovement=200;
(4)把手势添加到图片上
[self.imageView addGestureRecognizer:longPress];
[longPress release];
-(void)LongPressAction:(UILongPressGestureRecognizer *)longPress
{
//知道当前这个东西的长按状态
//longPress.state
//长按之后让其弹出一个UIAlertView
if (self.alertView==nil)
{
self.alertView=[[UIAlertView alloc]initWithTitle:@"ceshi " message:@"看戏怕" delegate:self cancelButtonTitle:@"quxiap" otherButtonTitles:@"en ", nil];
self.alertView.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
[self.alertView show];
}
[self.alertView show];
}


3.旋转

(1)创建一个旋转的手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(RotationAction:)];
(2)把手势放到对应的图片上
[self.imageView addGestureRecognizer:rotation];
(3)释放
[rotation release];

#pragma mark 通过图片的旋转手势,让图片发生旋转
-(void)RotationAction:(UIRotationGestureRecognizer *)rotation
{
//可以通过手势获取手势添加的视图是哪一个
UIImageView *imageView=(UIImageView *)rotation.view;
//旋转的操作
//通过视图的transform属性,让视图进行旋转
imageView.transform=CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation=0;
//将转的调制0
}


4.捏合

(1)创建
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction :)];
(2)添加到图片上
[self.imageView addGestureRecognizer:pinch];
(3)释放
[pinch release];
#pragma mark 通过捏合手势 缩放图片

-(void)pinchAction :(UIPinchGestureRecognizer *)pinch
{
//通过手势找对应的视图
UIImageView *imageView=(UIImageView *)pinch.view;
//通过手势transform改变图片的尺寸
//原来的transform大小后面的xy指x缩放的手势    imageView.transform=CGAffineTransformScale(imageView.transform, pinch.scale ,pinch.scale);
//为了防止手势的变化让图片消失
pinch.scale=1;
}


5.拖拽

(1)创建
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc ]initWithTarget:self action:@selector(panAction:)];
(2)添加手势
[self.imageView addGestureRecognizer:pan];
(3)释放
[pan release];

#pragma mark 通过拖拽手势,让视图随着手势的移动而移动
-(void)panAction:(UIPanGestureRecognizer *)pan
{
//通过手势找视图
UIImageView *imageView=(UIImageView *)pan.view;
//通过手势获得经过点
CGPoint p = [pan translationInView:imageView];
//设置移动的位置
//前面是原始内容  后面的是要改变的xy他自己就会自己改变
imageView.transform=CGAffineTransformTranslate(imageView.transform, p.x, p.y);
//为了防止手势在操作过程的时候视图消失
//将每一次移动的最后都成为下一次的起点  防止图片消失
[pan setTranslation:CGPointZero inView:imageView];
}


6.轻扫:这个需要指定方向

(1)创建
UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
(2)添加
[self.imageView addGestureRecognizer:swipe];
(3)释放
[swipe release];
(4)轻扫方向:向左
swipe.direction=UISwipeGestureRecognizerDirectionRight;

#pragma mark 轻扫对应的方法
-(void)swipe:(UISwipeGestureRecognizer *)swipe
{
if (swipe.direction==UISwipeGestureRecognizerDirectionRight)
{
NSLog(@"向右");
}
}


7.屏幕边际手势,iOS7.0之后出现的手势

UIScreenEdgePanGestureRecognizer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: