添加IOS开发中点击手势的实现代码
来源:爱站网时间:2021-01-19编辑:网友分享
在开发过程中,我们可能会遇到很多问题,当我们将手势添加到视图中,但不想单击视图上方的视图来触发手势时,要怎么操作呢?现在爱站技术频道就为大家介绍添加IOS开发中点击手势的实现代码,一起来学习吧!
在开发过程中,我们可能会遇到很多问题,当我们将手势添加到视图中,但不想单击视图上方的视图来触发手势时,要怎么操作呢?现在爱站技术频道就为大家介绍添加IOS开发中点击手势的实现代码,一起来学习吧!
IOS 单击手势的添加实现代码
一,效果图。

二,工程图。

三,代码。
RootViewController.h
#import@interface RootViewController : UIViewController @end
RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//添加背景
[self addView];
}
#pragma -mark -functions
//添加背景
-(void)addView
{
self.title=@"单击手势的添加";
UIView *parentView=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];
parentView.backgroundColor=[UIColor redColor];
[self.view addSubview:parentView];
//单击的手势
UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
tapRecognize.numberOfTapsRequired = 1;
tapRecognize.delegate = self;
[tapRecognize setEnabled :YES];
[tapRecognize delaysTouchesBegan];
[tapRecognize cancelsTouchesInView];
[self.view addGestureRecognizer:tapRecognize];
}
#pragma UIGestureRecognizer Handles
-(void) handleTap:(UITapGestureRecognizer *)recognizer
{
NSLog(@"---单击手势-------");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
以上就是爱站技术频道介绍的关于添加IOS开发中点击手势的实现代码,在项目开发的时候,也需要进行测试,这样才知道项目是否达到了要求。
