UIGestureRecognizer
//
// ViewController.h
// IOSUIGesterRrcognizer0813
//
// Created by scjy on 15/8/13.
// Copyright (c) 2015年 scjy. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UIImageView *_imageView;
}
@end
/
// ViewController.m
// IOSUIGesterRrcognizer0813
//
// Created by scjy on 15/8/13.
// Copyright (c) 2015年 scjy. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
CGFloat scale;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array=@[@"点按",@"长按",@"轻扫",@"旋转",@"捏合",@"拖拽"];
UISegmentedControl *_segment=[[UISegmentedControl alloc]initWithItems:array];
_segment.frame=CGRectMake(0, 20, self.view.frame.size.width, 40);
[_segment addTarget:self action:@selector(doSegment:) forControlEvents:UIControlEventValueChanged];//注意是valuechanged而不是editingchanged
[self.view addSubview:_segment];
float hx=280;
float hy=280*220/391;
_imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, hx, hy)];
_imageView.center=self.view.center;
_imageView.image=[UIImage imageNamed:@"0.jpg"];
_imageView.userInteractionEnabled=YES;//交互打开
[self.view addSubview:_imageView];
}
-(void)doSegment:(UISegmentedControl*)sender
{
NSInteger index=sender.selectedSegmentIndex;
//移除所有手势
for (UIGestureRecognizer *gesture in _imageView.gestureRecognizers)
{
[_imageView removeGestureRecognizer:gesture];
}
switch (index) {
case 0:
{ //创建一个 点按 手势,并把手势添加到指定的视图中
UITapGestureRecognizer * aTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTapGesture:)];
[_imageView addGestureRecognizer:aTap];//添加手势按钮到指定图片
break;
}
case 1:
{
//长按
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(doLongPressGesture:)];
[_imageView addGestureRecognizer:longPress];//添加手势按钮到指定图片
break;
}
case 2:
{
//轻扫
UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(doSwipeGesture:)];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionUp|
UISwipeGestureRecognizerDirectionDown];//设置轻扫的类型,上下左右
[_imageView addGestureRecognizer:swipeGesture];//添加手势按钮到指定图片
break;
}
case 3:
{//按住alt键才能实现旋转
//旋转
UIRotationGestureRecognizer *Rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(doRotationGesture:)];
[_imageView addGestureRecognizer:Rotation];//添加手势按钮到指定图片
break;
}
case 4:
{
//捏合
UIPinchGestureRecognizer *Pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(doPinchGesture:)];
[_imageView addGestureRecognizer:Pinch];//添加手势按钮到指定图片
break;
}
case 5:
{
//拖拽
UIPanGestureRecognizer *Pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(doPanGesture:)];
[_imageView addGestureRecognizer:Pan];
break;
}
default:
break;
}
}
//点按的执行操作
-(void)doTapGesture:(UITapGestureRecognizer *)tap{
NSLog(@"点按");
//判断当前手势动作已经结束,执行手势
if (tap.state==UIGestureRecognizerStateEnded) {
UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",arc4random()%8]];//随机添加一张图片
_imageView.image=image;
}
}
//长按的执行事件
-(void)doLongPressGesture:(UILongPressGestureRecognizer *)LongPress{
//判断当前手势动作已经结束,执行手势
if (LongPress.state==UIGestureRecognizerStateEnded) {
UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",arc4random()%8]];//随机添加一张图片
_imageView.image=image;
}
}
//轻扫的执行事件
-(void)doSwipeGesture:(UISwipeGestureRecognizer *)Swipe{
UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",arc4random()%8]];//随机添加一张图片
_imageView.image=image;
}
//旋转的执行效果事件
-(void)doRotationGesture:(UIRotationGestureRecognizer *)Rotation{
[Rotation view].transform=CGAffineTransformRotate([[Rotation view]transform], [Rotation rotation]);
Rotation.rotation=0;
}
//捏合的执行效果事件
-(void)doPinchGesture:(UIPinchGestureRecognizer *)pinch{
if ([pinch state]==UIGestureRecognizerStateEnded) {
scale=1;
return;
}
CGFloat scale1=1.0-(scale-[pinch scale]);
CGAffineTransform current=CGAffineTransformScale(_imageView.transform, scale1, scale1);//transform变形
[_imageView setTransform:current];
scale=[pinch scale];//规模相对于接触点在屏幕坐标
}
//拖拽的执行效果事件
-(void)doPanGesture:(UIPanGestureRecognizer *)Pan{
//移动(转换)的点
CGPoint transFrom=[Pan translationInView:[Pan.view superview]];
//设置拖动的中心点移动的位置
[Pan.view setCenter:CGPointMake([Pan.view center].x+transFrom.x, [Pan.view center].y+transFrom.y)];
//设置要改变的视图,并开始移动
[Pan setTranslation:CGPointZero inView:[Pan.view superview]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end