为了完成手势识别,必须借助于手势识别器—
UIGestureRecognizer
1.
Tap点按手势
1. 创建一个点按事件
UITapGestureRecognizer
*tap = [[
UITapGestureRecognizer
alloc
]
initWithTarget
:
self
action
:
@selector
(tap:)];
2. 设置点击的次数
tap.
numberOfTapsRequired
点击的手指的个数
tap. numberOfTouchesRequired
3. 向要点击的view中添加手势
[ self . view addGestureRecognizer :tap]
tap. numberOfTouchesRequired
3. 向要点击的view中添加手势
[ self . view addGestureRecognizer :tap]
4. 代理方法
- (
BOOL
)gestureRecognizer:(
UIGestureRecognizer
*)gestureRecognizer shouldReceiveTouch:(
UITouch
*)touch;
2. LongPress 长按手势
- (
void
)addLongPress
{
UILongPressGestureRecognizer *longPress = [[ UILongPressGestureRecognizer alloc ] initWithTarget : self action : @selector (longPress:)];
longPress. minimumPressDuration = 0.01 ;
[ _imageView addGestureRecognizer :longPress];
}
- ( void )longPress:( UILongPressGestureRecognizer *)longPress
{
{
UILongPressGestureRecognizer *longPress = [[ UILongPressGestureRecognizer alloc ] initWithTarget : self action : @selector (longPress:)];
longPress. minimumPressDuration = 0.01 ;
[ _imageView addGestureRecognizer :longPress];
}
- ( void )longPress:( UILongPressGestureRecognizer *)longPress
{
// 长按包含多个状态,通过state标记
if (longPress. state == UIGestureRecognizerStateBegan ) {
NSLog ( @"%s" ,__func__);
}
}
if (longPress. state == UIGestureRecognizerStateBegan ) {
NSLog ( @"%s" ,__func__);
}
}
3. Swipe 清扫手势
- ( void )addSwipe
{
UISwipeGestureRecognizer *swipe = [[ UISwipeGestureRecognizer alloc ] initWithTarget : self action : @selector (swipe:)];
swipe. direction = UISwipeGestureRecognizerDirectionLeft ;
[ _imageView addGestureRecognizer :swipe];
UISwipeGestureRecognizer *swipeR = [[ UISwipeGestureRecognizer alloc ] initWithTarget : self action : @selector (swipe:)];
// swipeR.direction = UISwipeGestureRecognizerDirectionLeft;
[ _imageView addGestureRecognizer :swipeR];
}
- ( void )addSwipe
{
UISwipeGestureRecognizer *swipe = [[ UISwipeGestureRecognizer alloc ] initWithTarget : self action : @selector (swipe:)];
swipe. direction = UISwipeGestureRecognizerDirectionLeft ;
[ _imageView addGestureRecognizer :swipe];
UISwipeGestureRecognizer *swipeR = [[ UISwipeGestureRecognizer alloc ] initWithTarget : self action : @selector (swipe:)];
// swipeR.direction = UISwipeGestureRecognizerDirectionLeft;
[ _imageView addGestureRecognizer :swipeR];
}
细节: 默认一个手势只支持一个方向
// 识别滑动方向
@property
(
nonatomic
)
UISwipeGestureRecognizerDirection
direction;
4.
Rotation 旋转手势!!!!!!
- (
void
)addRotation
{
UIRotationGestureRecognizer *rotation = [[ UIRotationGestureRecognizer alloc ] initWithTarget : self action : @selector (rotation:)];
rotation. delegate = self ;
[ _imageView addGestureRecognizer :rotation];
}
- ( void )rotation:( UIRotationGestureRecognizer *)rotation
{
_imageView . transform = CGAffineTransformRotate ( _imageView . transform , rotation. rotation );
// 复位
rotation. rotation = 0 ;
}
{
UIRotationGestureRecognizer *rotation = [[ UIRotationGestureRecognizer alloc ] initWithTarget : self action : @selector (rotation:)];
rotation. delegate = self ;
[ _imageView addGestureRecognizer :rotation];
}
- ( void )rotation:( UIRotationGestureRecognizer *)rotation
{
_imageView . transform = CGAffineTransformRotate ( _imageView . transform , rotation. rotation );
// 复位
rotation. rotation = 0 ;
}
5.
Pinch 捏合手势
- (
void
)addPinch
{
UIPinchGestureRecognizer *pinch = [[ UIPinchGestureRecognizer alloc ] initWithTarget : self action : @selector (pinch:)];
pinch. delegate = self ;
[ _imageView addGestureRecognizer :pinch];
}
- ( void )pinch:( UIPinchGestureRecognizer *)pinch
{
_imageView . transform = CGAffineTransformScale ( _imageView . transform , pinch. scale , pinch. scale );
// 复位
pinch. scale = 1 ;
}
{
UIPinchGestureRecognizer *pinch = [[ UIPinchGestureRecognizer alloc ] initWithTarget : self action : @selector (pinch:)];
pinch. delegate = self ;
[ _imageView addGestureRecognizer :pinch];
}
- ( void )pinch:( UIPinchGestureRecognizer *)pinch
{
_imageView . transform = CGAffineTransformScale ( _imageView . transform , pinch. scale , pinch. scale );
// 复位
pinch. scale = 1 ;
}
6. 缩放并旋转
// 添加捏合和旋转
[
self
addPinch
];
[ self addRotation ];
[ self addRotation ];
// Simultaneously: 同时
// 调用这个方法判断是否支持很多手指
- ( BOOL )gestureRecognizer:( UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:( UIGestureRecognizer *)otherGestureRecognizer
{
return YES ;
}
7. 拖拽
- (
void
)addPan
{
UIPanGestureRecognizer *pan = [[ UIPanGestureRecognizer alloc ] initWithTarget : self action : @selector (pan:)];
[ _imageView addGestureRecognizer :pan];
}
- ( void )pan:( UIPanGestureRecognizer *)pan
{
CGPoint transP = [pan translationInView : _imageView ];
_imageView . transform = CGAffineTransformTranslate ( _imageView . transform , transP. x , transP. y );
[pan setTranslation : CGPointZero inView : _imageView ];
}
{
UIPanGestureRecognizer *pan = [[ UIPanGestureRecognizer alloc ] initWithTarget : self action : @selector (pan:)];
[ _imageView addGestureRecognizer :pan];
}
- ( void )pan:( UIPanGestureRecognizer *)pan
{
CGPoint transP = [pan translationInView : _imageView ];
_imageView . transform = CGAffineTransformTranslate ( _imageView . transform , transP. x , transP. y );
[pan setTranslation : CGPointZero inView : _imageView ];
}