UITapGestureRecognizer 点击手势
UIPanGestureRecognizer 拖拽,慢速移动
// 拖拽,慢速移动手势
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
// [_imageView addGestureRecognizer:pan];
/*
// 快递移动 (下)
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hanleSwipe:)];
// 设置快速手势的方向我下
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[_imageView addGestureRecognizer:swipeDown];
// 快递移动 (上)
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hanleSwipe:)];
// 设置快速手势的方向为上
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[_imageView addGestureRecognizer:swipeUp];
*/
// 旋转手势
// UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
// [_imageView addGestureRecognizer:rotation];
// 长按手势
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
// // 长按时间,2秒后相应方法
// longPress.minimumPressDuration = 2;
// [_imageView addGestureRecognizer:longPress];
// 挤压手势(一般用来做图片缩放)
UIPinchGestureRecognizer *pinch = [[ UIPinchGestureRecognizer alloc ] initWithTarget : self action : @selector (handlePinch:)];
[ _imageView addGestureRecognizer :pinch];
}
// 拖拽,慢速移动手势
- ( void )handlePan:( UIPanGestureRecognizer *)pan
{
// 获取手指在屏幕上的偏移量
CGPoint point = [pan translationInView : self . view ];
// NSLog(@"%f:x %f:y", point.x, point.y);
_imageView . center = CGPointMake ( _imageView . center . x + point. x , _imageView . center . y + point. y );
// 将手势位置还原
[pan setTranslation : CGPointZero inView : self . view ];
}
// 快速移动手势
- ( void )hanleSwipe:( UISwipeGestureRecognizer *)swipe
{
// 判断手势的方向
if (swipe. direction == UISwipeGestureRecognizerDirectionDown )
{
// 动画
[ UIView beginAnimations : nil context : NULL ];
// 多久之后执行动画
// [UIView setAnimationDelay:1];
// 动画持续时间
[ UIView setAnimationDuration : 1 ];
// 代码写在中间...
CGRect rect = _imageView . frame ;
rect. origin . y = 300 ;
_imageView . frame = rect;
// 提交动画
[ UIView commitAnimations ];
}
else if (swipe. direction == UISwipeGestureRecognizerDirectionUp )
{
[ UIView animateWithDuration : 1 animations :^{
CGRect rect = _imageView . frame ;
rect. origin . y = - 100 ;
_imageView . frame = rect;
}];
}
}
// 旋转手势
- ( void )handleRotation:( UIRotationGestureRecognizer *)rotation
{
static CGFloat lastScale;
// 利用仿射变换旋转
_imageView . transform = CGAffineTransformMakeRotation (lastScale + rotation. rotation );
// 判断是否旋转结束
if (rotation. state == UIGestureRecognizerStateEnded ) {
lastScale += rotation. rotation ;
}
}
// 长按手势,会被多次的调用
- ( void )handleLongPress:( UILongPressGestureRecognizer *)longPress
{
static BOOL isHiden = NO ;
if (longPress. state == UIGestureRecognizerStateBegan )
{
NSLog ( @"开始" );
isHiden = !isHiden;
[ UIView animateWithDuration : 1 animations :^{
_imageView . alpha = isHiden ? 0.1 : 1 ;
}];
}
else if (longPress. state == UIGestureRecognizerStateEnded )
{
NSLog ( @"结束" );
}
}
// 缩放手势
- ( void )handlePinch:( UIPinchGestureRecognizer *)pinch
{
// NSLog(@"%f", pinch.scale);
// CGAffineTransformScale 缩放变换
// _imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
// pinch.scale = 1;
// 限制最大和最小倍数
static CGFloat lastScale;
static CGFloat max = 1.3 ;
static CGFloat min = 0.8 ;
if (pinch. state == UIGestureRecognizerStateBegan )
{
lastScale = pinch. scale ;
}
if (pinch. state == UIGestureRecognizerStateBegan ||
pinch. state == UIGestureRecognizerStateChanged )
{
// KVC
CGFloat currScale = [[ _imageView . layer valueForKeyPath : @"transform.scale" ] floatValue ];
CGFloat newScale = 1 - (lastScale - pinch. scale );
newScale = MAX (newScale, min / currScale);
newScale = MIN (newScale, max / currScale);
_imageView . transform = CGAffineTransformScale ( _imageView . transform , newScale, newScale);
lastScale = pinch. scale ;
}
UIPanGestureRecognizer 拖拽,慢速移动
UISwipeGestureRecognizer 快递移动
UIRotationGestureRecognizer 旋转手势
UIPinchGestureRecognizer 缩放UILongPressGestureRecognizer 长按
//手势的声明
// 拖拽,慢速移动手势
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
// [_imageView addGestureRecognizer:pan];
/*
// 快递移动 (下)
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hanleSwipe:)];
// 设置快速手势的方向我下
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[_imageView addGestureRecognizer:swipeDown];
// 快递移动 (上)
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hanleSwipe:)];
// 设置快速手势的方向为上
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[_imageView addGestureRecognizer:swipeUp];
*/
// 旋转手势
// UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
// [_imageView addGestureRecognizer:rotation];
// 长按手势
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
// // 长按时间,2秒后相应方法
// longPress.minimumPressDuration = 2;
// [_imageView addGestureRecognizer:longPress];
// 挤压手势(一般用来做图片缩放)
UIPinchGestureRecognizer *pinch = [[ UIPinchGestureRecognizer alloc ] initWithTarget : self action : @selector (handlePinch:)];
[ _imageView addGestureRecognizer :pinch];
}
// 拖拽,慢速移动手势
- ( void )handlePan:( UIPanGestureRecognizer *)pan
{
// 获取手指在屏幕上的偏移量
CGPoint point = [pan translationInView : self . view ];
// NSLog(@"%f:x %f:y", point.x, point.y);
_imageView . center = CGPointMake ( _imageView . center . x + point. x , _imageView . center . y + point. y );
// 将手势位置还原
[pan setTranslation : CGPointZero inView : self . view ];
}
// 快速移动手势
- ( void )hanleSwipe:( UISwipeGestureRecognizer *)swipe
{
// 判断手势的方向
if (swipe. direction == UISwipeGestureRecognizerDirectionDown )
{
// 动画
[ UIView beginAnimations : nil context : NULL ];
// 多久之后执行动画
// [UIView setAnimationDelay:1];
// 动画持续时间
[ UIView setAnimationDuration : 1 ];
// 代码写在中间...
CGRect rect = _imageView . frame ;
rect. origin . y = 300 ;
_imageView . frame = rect;
// 提交动画
[ UIView commitAnimations ];
}
else if (swipe. direction == UISwipeGestureRecognizerDirectionUp )
{
[ UIView animateWithDuration : 1 animations :^{
CGRect rect = _imageView . frame ;
rect. origin . y = - 100 ;
_imageView . frame = rect;
}];
}
}
// 旋转手势
- ( void )handleRotation:( UIRotationGestureRecognizer *)rotation
{
static CGFloat lastScale;
// 利用仿射变换旋转
_imageView . transform = CGAffineTransformMakeRotation (lastScale + rotation. rotation );
// 判断是否旋转结束
if (rotation. state == UIGestureRecognizerStateEnded ) {
lastScale += rotation. rotation ;
}
}
// 长按手势,会被多次的调用
- ( void )handleLongPress:( UILongPressGestureRecognizer *)longPress
{
static BOOL isHiden = NO ;
if (longPress. state == UIGestureRecognizerStateBegan )
{
NSLog ( @"开始" );
isHiden = !isHiden;
[ UIView animateWithDuration : 1 animations :^{
_imageView . alpha = isHiden ? 0.1 : 1 ;
}];
}
else if (longPress. state == UIGestureRecognizerStateEnded )
{
NSLog ( @"结束" );
}
}
// 缩放手势
- ( void )handlePinch:( UIPinchGestureRecognizer *)pinch
{
// NSLog(@"%f", pinch.scale);
// CGAffineTransformScale 缩放变换
// _imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
// pinch.scale = 1;
// 限制最大和最小倍数
static CGFloat lastScale;
static CGFloat max = 1.3 ;
static CGFloat min = 0.8 ;
if (pinch. state == UIGestureRecognizerStateBegan )
{
lastScale = pinch. scale ;
}
if (pinch. state == UIGestureRecognizerStateBegan ||
pinch. state == UIGestureRecognizerStateChanged )
{
// KVC
CGFloat currScale = [[ _imageView . layer valueForKeyPath : @"transform.scale" ] floatValue ];
CGFloat newScale = 1 - (lastScale - pinch. scale );
newScale = MAX (newScale, min / currScale);
newScale = MIN (newScale, max / currScale);
_imageView . transform = CGAffineTransformScale ( _imageView . transform , newScale, newScale);
lastScale = pinch. scale ;
}
}