IOS 手势学习(点击,长按,轻扫,拖拽,旋转,捏合缩放)

热门标签

,

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

点击  UITapGestureRecognizer

长按 UILongPressGestureRecognizer

轻扫 UISwipeGestureRecognizer

拖拽 UIPanGestureRecognizer

旋转 UIRotationGestureRecognizer

捏合缩放 UIPinchGestureRecognizer
import "ViewController.h"

@interface ViewController () <UIGestureRecognizerDelegate>
{
    UIView * _view;
}
@end


@implementation ViewController

-(void)viewDidLoad {
  [super viewDidLoad];

//创建视图
  _view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
  _view.backgroundColor=[UIColor blueColor];
  _view.center=self.view.center;
  [self.view addSubview:_view];
//长按
    UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClick)];

//在长按过程中移动10个单位也算长按
longPress.allowableMovement=10;
//[longPress setAllowableMovement:10];     //同上

//长按的最短时间
longPress.minimumPressDuration=2;
[_view addGestureRecognizer:longPress];

//轻扫
    UISwipeGestureRecognizer * swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction)];

//往右边的方向 ——>
swipe.direction=UISwipeGestureRecognizerDirectionRight;
[_view addGestureRecognizer:swipe];

//轻扫
    UISwipeGestureRecognizer * swipe2=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction2)];

//往左边的方向 <——
swipe2.direction=UISwipeGestureRecognizerDirectionLeft;
[_view addGestureRecognizer:swipe2];

//轻扫
    UISwipeGestureRecognizer * swipe3=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction3)];

//往上边的方向 ⬆️
swipe3.direction=UISwipeGestureRecognizerDirectionUp;
[_view addGestureRecognizer:swipe3];

//轻扫
    UISwipeGestureRecognizer * swipe4=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction4)];

//往下边的方向 ⬇️
swipe4.direction=UISwipeGestureRecognizerDirectionDown;
[_view addGestureRecognizer:swipe4];

//拖拽
    UIPanGestureRecognizer * pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];

[_view addGestureRecognizer:pan];

//旋转
    UIRotationGestureRecognizer * rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    rotation.delegate=self;
    [_view addGestureRecognizer:rotation];


//捏合缩放
    UIPinchGestureRecognizer * pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    pinch.delegate=self;
    [_view addGestureRecognizer:pinch];

}

pragma mark - 点击
-(void) tapClick
{
  NSLog(@"点击");
}
#pragma mark - 长按
-(void)longPressClick
{
  NSLog(@"长按");
}
pragma mark - 向右滑
-(void) swipeAction
{
  NSLog(@"right");
}
#pragma mark - 向左滑

-(void) swipeAction2
{
  NSLog(@"left");
}
#pragma mark - 向上滑
-(void) swipeAction3
{
  NSLog(@"up");
}
#pragma mark - 向下滑

-(void) swipeAction4
{
  NSLog(@"down");
}

pragma mark - 拖拽移动
-(void) panAction:(UIPanGestureRecognizer *) pan
{
  //获取移动的大小
  CGPoint point=[pan translationInView:pan.view];

//更改视图的中心点坐标
  CGPoint points=_view.center;
  points.x+=point.x;
  points.y+=point.y;
  _view.center=points;
//每次都清空一下,消除坐标叠加
  [pan setTranslation:CGPointZero inView:pan.view];
}
pragma mark - 旋转
-(void) rotation:(UIRotationGestureRecognizer *) rote
{
  //获取当前旋转的度数
  CGFloat rotation=rote.rotation;


//通过仿射变换实现旋转
  _view.transform=CGAffineTransformRotate(_view.transform, rotation);
  //防止旋转叠加,清零
  rote.rotation=0;


}
pragma mark - 缩放捏合
-(void) pinch:(UIPinchGestureRecognizer *) pinch
{
  //获取比例
  CGFloat scale=pinch.scale;

//通过仿射变换实现缩放
  _view.transform=CGAffineTransformScale(_view.transform, scale, scale);

//防止比例叠加
  pinch.scale=1;

}

pragma mark - 代理方法实现旋转 + 缩放捏合 可同时进行
-(BOOL) gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer )otherGestureRecognizer
{
  return YES;
}
(void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

@end

 

标签:

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术Dog|博客
原文地址:《IOS 手势学习(点击,长按,轻扫,拖拽,旋转,捏合缩放)》 发布于2019-11-09

分享到:
赞(0) 打赏

评论 抢沙发

6 + 5 =


IOS 手势学习(点击,长按,轻扫,拖拽,旋转,捏合缩放)

长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?