iOS手势的实现方法

本文实例为大家分享了iOS手势的具体实现代码,供大家参考,具体内容如下

效果

细节

1.UITouch

#import "ViewController_0.h"

@interface ViewController_0 ()

@property (nonatomic, strong)UILabel *label;

@end

@implementation ViewController_0

- (void)viewDidLoad {

  [super viewDidLoad];

  self.label          = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
  self.label.backgroundColor  = [UIColor yellowColor];
  self.label.layer.borderWidth = 1;
  [self.view addSubview:self.label];

  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"拖动方块";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

  //1.拿到手势
  UITouch *touch = [touches anyObject];

  //2.拿到touch 所在view的坐标
  CGPoint point = [touch locationInView:self.view];

  //3.让label拿到坐标
  self.label.center = point;

  NSLog(@"1.手指接触到了屏幕");
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

  //1.拿到手势
  UITouch *touch = [touches anyObject];

  //2.拿到touch 所在view的坐标
  CGPoint point = [touch locationInView:self.view];

  //3.让label拿到坐标
  self.label.center = point;

  NSLog(@"2.手指在屏幕上移动");
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

   NSLog(@"3.手指刚离开屏幕");
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

  NSLog(@"4.手势失效了");
}

@end

2.UITapGestureRecognizer

#import "ViewController_1.h"

@interface ViewController_1 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_1

- (void)viewDidLoad {

  [super viewDidLoad];

  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"电脑上操作tap手势 alt +shift 需要连续点击次数2次";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
  self.label.backgroundColor    = [UIColor orangeColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

  // 1.创建tap手势
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
  tap.numberOfTapsRequired  = 2; //需要轻轻点击的次数
  tap.numberOfTouchesRequired = 2;//需要的手指数量 :2根手指alt +shift 需要匹配点击次数2次(其实直接用默认的就好)
  [self.label addGestureRecognizer:tap];
}

- (void)labelTap:(UITapGestureRecognizer *)tap {

  int num = [self.label.text intValue];
  num++;
  self.label.text = [NSString stringWithFormat:@"%d",num ];
}

@end

3.UILongPressGestureRecognizer

#import "ViewController_2.h"

@interface ViewController_2 () <UIGestureRecognizerDelegate>

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_2

- (void)viewDidLoad {

  [super viewDidLoad];

  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"long长按手势";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(40, 150, 200, 150)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
  longPress.numberOfTapsRequired     = 1;
  longPress.numberOfTouchesRequired    = 1;
  longPress.minimumPressDuration     = 1.0;
  longPress.delegate  = self;
  [self.label addGestureRecognizer:longPress];
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {

  if(longPress.state == UIGestureRecognizerStateBegan) {

    NSLog(@"手势状态开始");

  } else if(longPress.state == UIGestureRecognizerStateEnded) {

    NSLog(@"手势状态结束");

  } else if(longPress.state == UIGestureRecognizerStateChanged) {

    NSLog(@"手势状态改变");

    NSInteger num = [self.label.text integerValue];
    num ++;
    self.label.text = [NSString stringWithFormat:@"%ld",(long)num];
  }
}

@end

4.UISwipeGestureRecognizer

#import "ViewController_3.h"

@interface ViewController_3 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_3

- (void)viewDidLoad {

  [super viewDidLoad];

  UILabel *textlabel   = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text     = @"swipe手势 向右滑动➕1,你也可以设置左划上划下划";
  textlabel.numberOfLines = 0;
  textlabel.font     = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

  UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
  swipeGesture.direction         = UISwipeGestureRecognizerDirectionRight;//默认就是向右划
  [self.label addGestureRecognizer:swipeGesture];
}

-(void)swipeAction:(UISwipeGestureRecognizer *)swipe {

  if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {

    NSLog(@"现在响应左划手势");

  } else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {

    NSLog(@"现在响应右划手势");

    NSInteger num  = [self.label.text integerValue];
    num ++;
    self.label.text = [NSString stringWithFormat:@"%ld",(long)num];

  } else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {

    NSLog(@"现在响应上划手势");

  } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {

    NSLog(@"现在响应下划手势");
  }
}

@end

5.UIPanGestureRecognizer

#import "ViewController_4.h"

@interface ViewController_4 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_4

- (void)viewDidLoad {

  [super viewDidLoad];

  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"pan手势,拖动方块";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
  [self.label addGestureRecognizer:pan];
}

- (void)panAction:(UIPanGestureRecognizer *)pan {

  CGPoint offset  = [pan locationInView:self.view];
  self.label.center = offset;
}

@end

6.UIRotationGestureRecognizer

#import "ViewController_5.h"

@interface ViewController_5 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_5

- (void)viewDidLoad {

  [super viewDidLoad];

  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"模拟器测试旋转手势时,按住 option键,再用触摸板或鼠标操作";
  textlabel.font   = [UIFont systemFontOfSize:12];
  textlabel.numberOfLines = 0;
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(40, 200, 200, 50)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"we are friends";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

  //(模拟器测试捏合和旋转手势时,按住 option 键,再用触摸板或鼠标操作)
  UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
  [self.view addGestureRecognizer:rotation];

}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {

  self.label.transform = CGAffineTransformRotate(self.label.transform, rotation.rotation);
  rotation.rotation = 0; // 这个很重要!!!!!

//  static float orginState;
//
//  self.label.transform = CGAffineTransformMakeRotation(rotation.rotation + orginState);
//
//  if (rotation.state == UIGestureRecognizerStateEnded) {
//
//    orginState = orginState + rotation.state;
//    self.label.transform = CGAffineTransformMakeRotation(rotation.rotation);
//  }
}

@end

7.UIPinchGestureRecognizer

#import "ViewController_6.h"

@interface ViewController_6 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_6

- (void)viewDidLoad {

  [super viewDidLoad];

  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"模拟器测试捏合手势时,按住 option键,再用触摸板或鼠标操作";
  textlabel.font   = [UIFont systemFontOfSize:12];
  textlabel.numberOfLines = 0;
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 80, 80)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

//  (模拟器测试捏合和旋转手势时,按住 option 键,再用触摸板或鼠标操作)
  UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pichGesture:)];
  [self.view addGestureRecognizer:pinch];
}

- (void)pichGesture:(UIPinchGestureRecognizer *)pinch {

  static float originScale = 1;

  //手势缩放返回的scale 是相对于上一次的
  self.label.transform = CGAffineTransformMakeScale(pinch.scale * originScale , pinch.scale*originScale);

  if (pinch.state == UIGestureRecognizerStateEnded) {

    originScale = originScale * pinch.scale;
  }
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • iOS手势密码的实现方法

    本次讲的手势密码,是在九个按键上实现的,这里讲的是手势密码的基本实现和效果 同样先上效果图 其实就是对画图功能的一个实现,再加上手势操作结合起来. 屏幕宽度高度,方便下面操作,不做解释 #define ScreenHeight [[UIScreen mainScreen] bounds].size.height #define ScreenWidth [[UIScreen mainScreen] bounds].size.width 控制器.m文件 这里的imageView是用来装手势画图之后的

  • 基于JS实现Android,iOS一个手势动画效果

    废话不多说了,先给大家展示下效果图: 这是iOS下的效果,android下完全一致.通过do_GestureView组件和do_Animation组件,deviceone能很容易实现复杂的跨平台纯原生动画效果,这个示例就是通过手势控制图片上下动画滑动实现开合效果,还支持声音效果. 下面是主要的代码 //index.ui.js var do_Animator1 = mm("do_Animator"); do_Animator1.append(500, { y: -1334, curve:

  • IOS 单击手势的添加实现代码

    IOS 单击手势的添加实现代码 一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UIGestureRecognizerDelegate> @end RootViewController.m #import "RootViewController.h" @interface RootV

  • IOS中的七种手势小结

    今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: // 初始化一个UIimageView UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 300, 300)]; imageView.image = [UIImage imageNamed:@"12.jpg"]; // UIImageView的用户交互是默认关闭的,

  • iOS轻点、触摸和手势代码开发

    一.响应者链 以UIResponder作为超类的任何类都是响应者.UIView和UIControl是UIReponder的子类,因此所有视图和所有控件都是响应者. 1.初始相应器 事件首先会传递给UIApplication对象,接下来会传递给应用程序的UIWindow,UIWindow会选择一个初始相应器来处理事件.初始响应器会选择下面的方式选择1.对于触摸事件,UIWindow会确定用户触摸的视图,然后将事件交给注册了这个视图的手势识别器或则注册视图层级更高的手势识别器.只要存在能处理事件的识

  • IOS 七种手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)详解及实例代码

    IOS 七种手势操作 今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: 1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer

  • 使用Swift代码实现iOS手势解锁、指纹解锁实例详解

    一.手势密码 1. 1.1.用UIButton组成手势的节点. 1.2.当手指接触屏幕时,调用重写的 touchesBegan:withEvent方法(在touchesBegan里调用setNeedsDisplay,这样就会自动调用drawRect方法). 1.3.当手指在屏幕上滑动时,调用重写的touchesEnded:withEvent方法. 这两个方法执行的操作是一样的:通过locationInView获取 触摸的坐标,然后用 CGRectContainsPoint 判断手指是否经过UIB

  • IOS中各种手势操作实例代码

    先看下效果 手势相关的介绍 IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种: 1.点击  UITapGestureRecognizer 2.平移  UIPanGestureRecognizer 3.缩放  UIPinchGestureRecognizer 4.旋转  UIRotationGestureRecognizer 5.轻扫  UISwipeGestureRecognizer 我们上面这个实例中就用到了上面这5种手势,不

  • iOS 用Swipe手势和动画实现循环播放图片示例

    主要想法 添加3个ImageView展示图片,实现图片的无限循环. 使用Swipe手势识别用户向右或向左滑动图片. 使用CATransition给ImageView.layer添加动画,展示图片更换的效果. 实现 在storyboard添加三个UIImageView,用来展示图片.而数组imageArray则用来保存图片对象. @interface ViewController () @property (strong, nonatomic) IBOutlet UIImageView *midd

  • iOS手势的实现方法

    本文实例为大家分享了iOS手势的具体实现代码,供大家参考,具体内容如下 效果 细节 1.UITouch #import "ViewController_0.h" @interface ViewController_0 () @property (nonatomic, strong)UILabel *label; @end @implementation ViewController_0 - (void)viewDidLoad { [super viewDidLoad]; self.la

  • iOS UIScrollView和控制器返回手势冲突解决方法

    开发中,有部分UI,会将UIScrollView横向铺在底层,上面放tableView 或一些视图左右滚动切换,底层的scrollView会和Nav ViewController原有的返回手势冲突 解决办法,重写UIScrollView 的gestureRecognizerShouldBegin,在ScrollView滚动到头的时候,屏蔽ScrollView的手势 class GesturesConflictScrollView: UIScrollView { override func ges

  • IOS 手势操作详解及实例总结篇

    iOS手势操作总结 手势操作种类 UITapGestureRecognizer: 敲击,点击 UILongPressGestureRecognizer: 长按 UIPinchGestureRecognizer: 缩放 UIRotationGestureRecognizer: 旋转 UISwipeGestureRecongizer: 轻扫 UIPanGestureRecognizer: 拖拽 手势操作的代理方法(UIGestureRecognizerDelegate) 手势可能发生的条件,返回NO

  • IOS手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)

    下面通过图文并茂的方式给大家分享下IOS手势操作(拖动.捏合.旋转.点按.长按.轻扫.自定义)的相关内容. 1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer(捏合) UIRotatio

  • IOS  手势操作详解及实例总结篇

    iOS手势操作总结 手势操作种类 UITapGestureRecognizer: 敲击,点击 UILongPressGestureRecognizer: 长按 UIPinchGestureRecognizer: 缩放 UIRotationGestureRecognizer: 旋转 UISwipeGestureRecongizer: 轻扫 UIPanGestureRecognizer: 拖拽 手势操作的代理方法(UIGestureRecognizerDelegate) 手势可能发生的条件,返回NO

  • iOS UIView常见属性方法小结

    下面通过实例代码给大家详细介绍了iOS UIView常见属性方法,具体代码如下所示: UIView : UIResponder /** 通过一个frame来初始化一个UI控件 */ - (id)initWithFrame:(CGRect)frame; // YES:能够跟用户进行交互 @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // default is YES // 控件的一

  • .net平台推送ios消息的实现方法

    本文实例讲述了.net平台推送ios消息的实现方法.分享给大家供大家参考. 具体实现步骤如下: 1.ios应用程序中允许向客户推送消息 2.需要有苹果的证书以及密码(怎么获取,网上搜一下,需要交费的) 3.iphone手机一部,安装了该ios应用程序 4..net 项目中引用PushSharp.Apple.dll,PushSharp.Core.dll(这两个文件在网上搜一下,有源码的) 5.开始写代码,定义全局的对象PushBroker pusher = new PushBroker(); 6.

  • php、java、android、ios通用的3des方法(推荐)

    php服务器,java服务器,android,ios开发兼容的3des加密解密, php <?php class DES3 { var $key = "my.oschina.net/penngo?#@"; var $iv = "01234567"; function encrypt($input){ $size = mcrypt_get_block_size(MCRYPT_3DES,MCRYPT_MODE_CBC); $input = $this->pk

  • IOS 静态方法与动态方法详解

    IOS 静态方法与动态方法详解 1.问题提出 iOS中有静态方法与动态方法,那么两种方法的异同是什么? 2.问题分析 因为每个对象都由相应的数据结构与方法相构成,一个程序可能有多个属于同一个类的对象,而每个对象的数据结构应该是不一的,但方法是相同的,若为每个对象开辟内存空间来存储方法,必然是对内存空间极大的浪费.因此apple是通过类对象与元类来解决这个问题的. 从根本来说,c++.objective-c.java都发源于c语言,因此这些语言实际上可以理解了经过封装的c语言,所以它们更加方便使用

随机推荐