UIView的点击事件与触摸手势

本文介绍了如何在iOS应用中为UIView及其子类添加手势识别器,包括在init、loadView或viewDidLoad方法中设置。示例中提到了向左滑动的手势识别器用于WebView,以及长按事件的添加到搜索按钮。讨论了UIGestureRecognizer的主要子类,如UILongPressGestureRecognizer、UIPanGestureRecognizer等,用于实现长按、拖动、捏合、旋转、滑动和点击等不同手势。
摘要由CSDN通过智能技术生成

在创建的UIView子类或者UIViewController的 init方法,或者 loadView或者 viewDidLoad等方法中加入手势识别器。

并用选择器指定响应动作对象方法。

一下是UIVew中加入方式

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
	//向右滑动
	UISwipeGestureRecognizer  *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
	[self addGestureRecognizer:swipeRight];
			        
	//向左滑动
	UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
	swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
	[self addGestureRecognizer:swipeLeft];
			
	//单击事件     
	UITapGestureRecognizer* singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
	//点击的次数
	singleRecognizer.numberOfTapsRequired = 1;
	//给self.view添加一个手势监测;
	[self addGestureRecognizer:singleRecognizer];
			
	//双击事
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值