//先创建图片
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"10_0.jpg"];
imageView.tag = 1;
//添加滑动手势
[imageView addGestureRecognizer:sgr];
imageView.userInteractionEnabled=YES;
UISwipeGestureRecognizer *rightsgr=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];
rightsgr.direction=UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:rightsgr];
[self.view addSubview:imageView];
}
-(void)swipeClick:(UISwipeGestureRecognizer*)sgr
{ //四个手势监听四个方向,四个手势可以都响应这个方法,然后在这个方法里判断手势的方向。
if (sgr.direction & UISwipeGestureRecognizerDirectionRight)
NSLog(@"向右滑动了");
else if (sgr.direction & UISwipeGestureRecognizerDirectionLeft)
NSLog(@"向左滑动了");
else if (sgr.direction & UISwipeGestureRecognizerDirectionUp)
NSLog(@"向上滑动了");
else if (sgr.direction & UISwipeGestureRecognizerDirectionDown)
NSLog(@"向下滑动了");}
//添加点击手势 为图片添加点击手势很方便的
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"10_0.jpg"];
imageView.tag = 1;
//给imageView添加敲击手势
UITapGestureRecognizer *tgr=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
//这个事件表示,发生敲击,响应[target action];
[imageView addGestureRecognizer:tgr];
imageView.userInteractionEnabled=YES;
[self.view addSubview:imageView];
//一个手势只能添加到一个视图上,但一个视图可以添加多个手势
//添加双击手势
UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapClick:)];
[imageView addGestureRecognizer:doubleTap];
doubleTap.numberOfTapsRequired=2;
//设置双击失败,才响应单击
[tgr requireGestureRecognizerToFail:doubleTap];
//优先响应双击,确定不双击,才响应单击
}
-(void)tapClick:(UITapGestureRecognizer*)tgr
{
// 获得拥有手势的视图 适合所有手势
// UIView * view = tgr.view;
NSLog(@"图片发生敲击事件");
//可以在这里图片进行点击处理 不用再给图片添加点击方法
}
-(void)doubleTapClick:(UITapGestureRecognizer*)tgr
{
NSLog(@"图片被双击了!你跟hello kitty有多大仇?");
}
//捏合手势 在图片的放大方面很有用的 想看高清图片时很有用
- (void)createImageView
{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"10_0.jpg"];
imageView.center=self.view.center;
imageView.tag = 1;
/*给imageView添加捏合手势*/
UIPinchGestureRecognizer *pgr=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pich:)];
[imageView addGestureRecognizer:pgr];
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
}
-(void)pich:(UIPinchGestureRecognizer*)pgr
{
//用来保存我们每次捏合的结果
static CGFloat scale = 1;
pgr.view.transform=CGAffineTransformMakeScale(scale*pgr.scale, pgr.scale*scale);
//scale是上次捏合结果比如50%,pgr.scale是本次手指捏合的比例如50%,最后结果25%
//随着捏合,pgr.scale不断改变
if (pgr.state==UIGestureRecognizerStateEnded) {
scale*=pgr.scale;
}
}
//还有摇动手势
- (void)createImageView
{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
NSString *str=[[NSBundle mainBundle] pathForResource:@"10_3" ofType:@"jpg"];
NSData *data=[NSData dataWithContentsOfFile:str];
imageView.image=[UIImage imageWithData:data];
// imageView.image = [UIImage imageNamed:@"10_3.jpg"];
//启动动画
[imageView shakeWithRaid:0.5 duration:0.5 repeatCount:0];
//停止
// [imageView stopShake];
[self.view addSubview:imageView];
}
转载于:https://my.oschina.net/u/2436241/blog/492573