iOS开篇——UI之UIGestureRecogzier_手势

一.UITouch

 1 //任何视图都可以触发此方法
 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 3     NSLog(@"视图被触摸了");
 4 }
 5
 6 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 7     NSLog(@"因意外停止了触摸");
 8 }
 9
10 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
11     NSLog(@"结束触摸");
12 }
13
14 //可以获得手指在视图移动的位置
15 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
16 //    CGFloat floct = [touches anyObject];
17     //手指触摸的点
18     NSLog(@"%@",[touches anyObject]);
19     //移动到的所有的点
20 //    NSLog(@"%@",[touches allObjects]);
21 }

二.UITapGestureRecognizer  点击手势

创建一个view 创建手势  使用addGestureRecognizer:方法 将手势添加到view上

 1 UIView * view = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 200, 200)];
 2     view.backgroundColor = [UIColor redColor];
 3
 4     //创建一个点击手势
 5     UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
 6     //设置点击次数
 7     tap.numberOfTapsRequired = 1;
 8     //把手势添加到控件上
 9     [view addGestureRecognizer:tap];
10
11     UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClicked:)];
12     tap1.numberOfTapsRequired = 2;
13     [view addGestureRecognizer:tap1];
14
15
16
17     [self.view addSubview:view];

三.UILongPressGestureRecognizer  长按手势

 1 UIView * view = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 200, 200)];
 2     view.backgroundColor = [UIColor redColor];
 3
 4     //创建一个长按手势
 5     UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
 6
 7     //设置长按最短时间
 8     longPress.minimumPressDuration = 1;
 9     //设置长按手指数量
10     longPress.numberOfTouchesRequired = 2;
11
12     //把手势添加到控件上
13     [view addGestureRecognizer:longPress];

四.UISwipeGestureRecognizer 滑动手势

 

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 30)];
    label.textColor = [UIColor grayColor];
    label.text = @"滑动手势";

    //创建滑动手势
    UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
    //添加到控件上
    [label addGestureRecognizer:swipe];

    //打开label的交互 默认为NO  是NO的时候 手势不能被响应
    label.userInteractionEnabled = YES;

    //设置滑动方向 每一个手势只能是一个方向
    //如果需要多方向  需要多次创建
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    /*
     typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
     UISwipeGestureRecognizerDirectionRight = 1 << 0,
     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
     UISwipeGestureRecognizerDirectionUp    = 1 << 2,
     UISwipeGestureRecognizerDirectionDown  = 1 << 3
     };
     */

    //设置滑动手指数量
    swipe.numberOfTouchesRequired = 1;

    [self.view addSubview:label];

五.UIPanGestureRecognizer 拖动手势

 1 - (void)createLabel{
 2     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 30)];
 3     label.textColor = [UIColor groupTableViewBackgroundColor];
 4     label.text = @"拖动手势";
 5     label.tag = 1;
 6     //创建拖动手势
 7     UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
 8     //添加到控件上
 9     [label addGestureRecognizer:pan];
10
11     //打开label的交互 默认为NO  是NO的时候 手势不能被响应
12     label.userInteractionEnabled = YES;
13
14
15
16     [self.view addSubview:label];
17 }
18
19 - (void)onClick:(UIPanGestureRecognizer *)pan{
20     static CGPoint offPoint;
21
22     UILabel * label = [self.view viewWithTag:1];
23     //获得手势的偏移量
24     CGPoint point  = [pan translationInView:self.view];
25
26     if (pan.state == UIGestureRecognizerStateBegan) {
27         offPoint = label.center;
28         return;
29     }
30
31     label.center = CGPointMake(point.x+offPoint.x, point.y+offPoint.y);
32 }

六.UIRotationGestureRecognizer 旋转手势

 1 - (void)createLabel{
 2     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 60)];
 3     label.textColor = [UIColor groupTableViewBackgroundColor];
 4     label.text = @"旋转手势";
 5     label.tag = 1;
 6     label.textAlignment = NSTextAlignmentCenter;
 7     label.adjustsFontSizeToFitWidth = YES;
 8
 9     label.font = [UIFont systemFontOfSize:60];
10     //创建旋转手势
11     UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
12     //添加到控件上
13     [label addGestureRecognizer:rotation];
14
15     //打开label的交互 默认为NO  是NO的时候 手势不能被响应
16     label.userInteractionEnabled = YES;
17     [self.view addSubview:label];
18 }
19
20 - (void)onClick:(UIRotationGestureRecognizer *)rotation{
21     UILabel * label = [self.view viewWithTag:1];
22
23     static CGFloat offFloat;
24
25     label.transform = CGAffineTransformMakeRotation(rotation.rotation +offFloat);
26     //结束时把上次旋转角度的记录下来
27     if (rotation.state == UIGestureRecognizerStateEnded) {
28         offFloat = offFloat + rotation.rotation;
29     }
30 }

七.UIPinchGestureRecognizer 捏合手势

 1 - (void)createLabel{
 2     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 60)];
 3     label.textColor = [UIColor groupTableViewBackgroundColor];
 4     label.text = @"捏合手势";
 5     label.tag = 1;
 6     label.textAlignment = NSTextAlignmentCenter;
 7     label.adjustsFontSizeToFitWidth = YES;
 8
 9     label.font = [UIFont systemFontOfSize:60];
10     //创建捏合手势
11     UIPinchGestureRecognizer * pinchGR = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
12     //添加到控件上
13     [label addGestureRecognizer:pinchGR];
14
15     //打开label的交互 默认为NO  是NO的时候 手势不能被响应
16     label.userInteractionEnabled = YES;
17     [self.view addSubview:label];
18 }
19
20 - (void)onClick:(UIPinchGestureRecognizer *)pinchGR{
21     UILabel * label = [self.view viewWithTag:1];
22     static CGFloat offFloat = 1;
23     label.transform = CGAffineTransformMakeScale(pinchGR.scale*offFloat, pinchGR.scale*offFloat);
24     //记录结束手势时的缩放比例
25     if (pinchGR.state == UIGestureRecognizerStateEnded) {
26         offFloat = offFloat* pinchGR.scale;
27     }
28 }
时间: 2024-10-07 05:30:29

iOS开篇——UI之UIGestureRecogzier_手势的相关文章

iOS开篇——UI之UITableView

1 #import "ViewController.h" 2 3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> 4 { 5 //创建数据源 6 NSMutableArray * _dataArray; 7 8 } 9 10 @end 11 12 @implementation ViewController 13 14 - (void)viewDidLoad { 15 [supe

iOS开篇——UI之UITextView

创建UITextView //创建一个单例对象 存储_str字符串 NSUserDefaults * hd = [NSUserDefaults standardUserDefaults]; _str = [hd objectForKey:@"str"]; UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; textView.delegate = self; te

iOS开篇——UI之UAlertView(提示框)

创建提示框 //创建提示框 //标题 提示内容 代理对象 按钮 UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"萨达姆已经做好战斗准备" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"不确定", nil]; 设置提示框样式

iOS开篇——UI之UITextField

创建文本输入框 UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 250, 40)]; 设置边框样式 textField.borderStyle = UITextBorderStyleRoundedRect; /* typedef NS_ENUM(NSInteger, UITextBorderStyle) { UITextBorderStyleNone, 无效果 UITextBorderS

iOS开篇——UI之UISegmentedControl (分段选择器)

创建分段选择器 UISegmentedControl * sc = [[UISegmentedControl alloc]initWithFrame:CGRectMake(50, 100, 200, 30)]; [sc insertSegmentWithTitle:@"第一页" atIndex:0 animated:YES]; [sc insertSegmentWithTitle:@"第二页" atIndex:1 animated:YES]; [sc insertS

iOS开篇——UI之UIActionSheet

UIActionSheet在iOS8.3之后已不建议使用. 可以使用 UIAlertController+UIAlertControllerStyleActionSheet获得同样的效果 创建UIActionSheet UIActionSheet * as = [[UIActionSheet alloc]initWithTitle:@"选择一个英雄" delegate:self cancelButtonTitle:@"取消" destructiveButtonTit

iOS开篇——UI之UIStepper (计步器)

UIStepper * stepper = [[UIStepper alloc]initWithFrame:CGRectMake(50, 100, 150, 40)]; //********最小值和最大值 stepper.maximumValue = 100; stepper.minimumValue = 0; //设置跃迁值 *** stepper.stepValue = 0.1; //设置镂空颜色 stepper.tintColor = [UIColor redColor]; //添加点击事

iOS开篇——UI之UIView

UIView的几个主要属性 1 /* 2 CGRect frame; 3 4 CGRect bounds; 5 6 CGPoint center; 7 8 CGAffineTransform transform; 9 */ frame 坐标与长宽 center 中心点 bounds 坐标系限制(?) UIView的形变 //view的变形 () //直接形变 view.transform = CGAffineTransformMakeScale(1, 1); //旋转形变 传入一个弧度 view

iOS开篇——UI之UILabel

创建label //创建标签对象 UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 300, 105)]; 添加标示 //添加标示 label.tag = 1; 设置label文字 //设置label文字 // label.text = @"这是一个标签视图,敌军还有三十秒到达战场.碾碎他们"; label.text = @"this is a label!"; 设置label字体位