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

 

人已赞赏
iOS文章

iOS UITableView的编辑模式

2019-11-9 12:16:38

iOS文章

iOS UITableView的style的区别与用法,以及分割线的显示与隐藏

2019-11-9 16:19:31

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索