手势识别之平移、缩放、长按、旋转、滑动

简介:

前面了解了手势识别的点击以及代理方法,其他的几个手势识别都是差不多


#import "ViewController.h"
 
@interface ViewController ()
@property(nonatomic,strong)UIImageView *imgView;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"test.jpg"]];
    self.imgView.frame=CGRectMake(100, 100, 100, 100);
    self.imgView.userInteractionEnabled=YES;
    self.imgView.multipleTouchEnabled=YES;
    [self.view addSubview:self.imgView];
     
     //拖动
    UIPanGestureRecognizer *panGestureRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
    [self.imgView addGestureRecognizer:panGestureRecognizer];
     
    //滑动手势
    UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture:)];
    //方向
    swipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;
    [self.imgView addGestureRecognizer:swipeGestureRecognizer];
     
    //长按手势
    UILongPressGestureRecognizer *longPressGestureRecongnizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
     //按的最短时间
    longPressGestureRecongnizer.minimumPressDuration=2.0;
    // 长按手势识别之前点击次数
    longPressGestureRecongnizer.numberOfTapsRequired=1;
    //需要几个手指
    longPressGestureRecongnizer.numberOfTouchesRequired=1;
    //运行偏移量
    longPressGestureRecongnizer.allowableMovement=10.0;
    [self.imgView addGestureRecognizer:longPressGestureRecongnizer];
     
    //旋转
    UIRotationGestureRecognizer *rotationGestureRecognizer=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
    [self.imgView addGestureRecognizer:rotationGestureRecognizer];
     
    //缩放
    UIPinchGestureRecognizer *pinchGestureRecognizer=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGesture:)];
    [self.imgView addGestureRecognizer:pinchGestureRecognizer];
     
     
}
//平移
-(void)panGesture:(UIPanGestureRecognizer*)pan
{
    //在View中的位置
    CGPoint point=[pan locationInView:pan.view];
    //在View中的移动量 以手指按下的为原点
    CGPoint point1=[pan translationInView:self.view];
    NSLog(@"%@",NSStringFromCGPoint(point1));
//    NSLog(@"%@",NSStringFromCGPoint(point1));
    CGPoint temp=self.imgView.center;
    temp.x+=point1.x;
    temp.y+=point1.y;
    self.imgView.center=temp;
    //偏移量是增加的应该设为0
    [pan setTranslation:CGPointZero inView:pan.view];
}
//长按
-(void)longPressGesture:(UIGestureRecognizer*)gestureRecognizer
{
    NSLog(@"UILongPressGestureRecognizer");
}
 //滑动
-(void)swipeGesture:(UIGestureRecognizer *)gestureRecognizer
{
     NSLog(@"UISwipeGestureRecognizer");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
     
}
//旋转
-(void)rotationGesture:(UIRotationGestureRecognizer *)gestureRecognizer
{
    NSLog(@"旋转的角度:%lf",gestureRecognizer.rotation);
//    self.imgView.transform=CGAffineTransformMakeRotation(gestureRecognizer.rotation);
    self.imgView.transform=CGAffineTransformRotate(self.imgView.transform, gestureRecognizer.rotation);
    gestureRecognizer.rotation=0;
}
//缩放
-(void)pinchGesture:(UIPinchGestureRecognizer *)pinchGesture
{
    NSLog(@"%lf",pinchGesture.scale);
//    self.imgView.transform=CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
    self.imgView.transform=CGAffineTransformScale(self.imgView.transform, pinchGesture.scale, pinchGesture.scale);
    //回初始值
    pinchGesture.scale=1.0;
}
 
@end


相关文章
|
1月前
|
Python
平移
【5月更文挑战第15天】平移。
17 1
SwiftUI—如何对图像视图进行缩放和旋转
SwiftUI—如何对图像视图进行缩放和旋转
784 0
SwiftUI—如何对图像视图进行缩放和旋转
|
1月前
|
Python
缩放
【5月更文挑战第15天】缩放。
14 1
|
7月前
滚轮缩放
滚轮缩放
26 0
使用手势对UIImageView进行缩放、旋转和移动
使用手势对UIImageView进行缩放、旋转和移动
86 0
|
前端开发 JavaScript
canvas中的拖拽、缩放、旋转 (上) —— 数学知识准备
canvas中的拖拽、缩放、旋转 (上) —— 数学知识准备
736 0
canvas中的拖拽、缩放、旋转 (上) —— 数学知识准备
鼠标控制物体旋转、移动、缩放(Unity3D)
Unity3D对于鼠标操作物体的旋转、移动、缩放的功能点使用的比较多。 今天就分享如何使用Unity实现鼠标对于物体的旋转、移动、缩放。
|
图形学
Unity中的平移 缩放 旋转
Unity中的平移 缩放 旋转 1.最近想实现Unity中的平移 缩放 旋转功能, 类似于Unity编辑器状态下的效果.尝试了好几个方式,效果都有瑕疵,网友们谁有好的实现方式,请教我. 下面是我的一种实现方式: using System.
2207 0

热门文章

最新文章