UIGestureRecognizer这是手势操作的虚拟基类,要想使用它必须继承它,它继承NSObject.
它包含以下几个自动手势识别的子类, 每个子类都实现响应的功能. 如果想开发新的功能可以继承UIGestureRecognizer实现你想要的功能.
typedef enum {
UIGestureRecognizerStatePossible,//默认状态
UIGestureRecognizerStateBegan, //开始手势
UIGestureRecognizerStateChanged, //手势改变中,通常理解为在正在进行的某种动作
UIGestureRecognizerStateEnded, //手势结束,放手的时候调用
UIGestureRecognizerStateCancelled, //取消状态,动作停止时调用
UIGestureRecognizerStateFailed, //识别器不能识别的手势
} UIGestureRecognizerState
1. UITapGestureRecognizer
这个子类是对单击的监控
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doTap:)];
tapGesture.numberOfTapsRequired = 2; //设置双击的监控
tapGesture.numberOfTouchesRequired = 2; //设置两个指头的监控
[self.view addGestureRecognizer:tapGesture];
// Do any additional setup after loading the view from its nib.
}
-(void)doTap:(UITapGestureRecognizer*)recognizer
{
NSUInteger finger = [recognizer numberOfTapsRequired];
NSUInteger fingers = [recognizer numberOfTouchesRequired];
NSLog(@"%d击 %d个指头", finger, fingers);
}
2.UIPinchGestureRecognizer 和 3.UIRotationGestureRecognizer
这个子类是对捏合操作时的监控 和 旋转时的监控
@property (weak, nonatomic) IBOutlet UIImageView *image;
CGFloat rotation, preRotation;
CGFloat scale, preScale;
- (void)viewDidLoad
{
[super viewDidLoad];
preScale = 1;
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doScale:)];
pinchGesture.delegate = self;
[self.view addGestureRecognizer:pinchGesture];
UIRotationGestureRecognizer* rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(doRotation:)];
rotationGesture.delegate = self;
[self.view addGestureRecognizer:rotationGesture];
}
//UIGestureRecognizerDelegate代理方法 作用是允许两个手势同时对一个控件进行操作
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(void)imageViewAdjust
{
CGAffineTransform f = CGAffineTransformMakeScale(scale * preScale, scale * preScale);
f = CGAffineTransformRotate(f, rotation + preRotation);
_image.transform = f;
}
-(void)doRotation:(UIRotationGestureRecognizer*)recognizer
{
rotation = recognizer.rotation;
[self imageViewAdjust];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {
preRotation = rotation + preRotation;
rotation = 0;
}
}
-(void)doScale:(UIPinchGestureRecognizer*)recognizer
{
scale = recognizer.scale;
[self imageViewAdjust];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {
preScale = scale * preScale;
scale = 0;
}
}
4.UISwipeGestureRecognizer
这个子类是对上下左右滑动时的监控
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer* swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doTap:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown; //设置对哪个方向的滑动进行监控
swipeGesture.numberOfTouchesRequired = 3; //对几个指头滑动时的监控
[self.view addGestureRecognizer:swipeGesture];
// Do any additional setup after loading the view from its nib.
}
-(void)doTap:(UISwipeGestureRecognizer*)recognizer
{
NSUInteger finger = [recognizer numberOfTouches];
NSLog(@"%d个指头 上下滑动", finger);
}
5.UIPanGestureRecognizer
这个子类是对控件的移动操作.比如按住一个图片移动到另一个位置
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(doPan:)];
[_image addGestureRecognizer:panGesture]; //这里设置的意思是可以对_image这个控件进行绑定.这样鼠标移动控件也跟着移动
}
-(void)doPan:(UIPanGestureRecognizer*)recognizer
{
CGPoint p = [recognizer translationInView:self.view]; //你手移动了多少距离,相对于绑定的控件.
recognizer.view.center = CGPointMake(recognizer.view.center.x + p.x,
recognizer.view.center.y + p.y); //recognizer.view是绑定控件的坐标,相加后就等于手现在的坐标
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; //每次移动时坐标都是递增的.这样控件的位置就可以正常了.
}
6.UIScreenEdgePanGestureRecognizer
IOS7刚出的新类还不知道怎么用呢. 摸索中...
7.UILongPressGestureRecognizer
这个子类是对长按时的监控
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer* pressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
pressGesture.minimumPressDuration = 2; //长按2秒
pressGesture.numberOfTouchesRequired = 1; //1个指头
[self.view addGestureRecognizer:pressGesture];
}
-(void)longPress:(UILongPressGestureRecognizer*)recognizer
{
if ( recognizer.state == UIGestureRecognizerStateEnded ) {
//手指头离开时执行
}
}
UIGestureRecognizerDelegate代理方法
// 询问delegate,两个手势是否同时接收消息,返回YES同时接收。返回NO,不同时接收(如果另外一个手势返回YES,则并不能保证不同时接收消息)the default implementation returns NO。
// 这个函数一般在一个手势接收者要阻止另外一个手势接收自己的消息的时候调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return NO;
}
// 询问一个手势接收者是否应该开始解释执行一个触摸接收事件
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
// 询问delegate是否允许手势接收者接收一个touch对象
// 返回YES,则允许对这个touch对象审核,NO,则不允许。
// 这个方法在touchesBegan:withEvent:之前调用,为一个新的touch对象进行调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}