点击手势demo

本文介绍了一个名为UIGestureViewController的iOS视图控制器类实现,该控制器通过添加手势识别器来响应不同数量的点击事件,并在界面上显示点击次数及触碰的手指数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<span style="background-color: rgb(153, 153, 255);">//
//  UIGestureViewController.m
//  LJEExcise
//
//  Created by xiaoyao on 14/11/29.
//  Copyright (c) 2014年 lijien. All rights reserved.
//

#import "UIGestureViewController.h"

#define kLABELFRAME (CGRectMake(20, 150, 200, 50))
#define kVIEWFRAME (CGRectMake(20, 220, 200, 80))

@interface UIGestureViewController () {
  UILabel *_touchLabel;
  UIView *_touchView;
}
@end

@implementation UIGestureViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  _touchLabel = [[UILabel alloc] initWithFrame:kLABELFRAME];
  _touchLabel.backgroundColor = [UIColor blueColor];
  _touchLabel.tag = 9999;
  _touchLabel.textColor = [UIColor redColor];
  // 当UI控件不允许用户使用交互属性的时候应当打开其交互
  _touchLabel.userInteractionEnabled = YES;
  // 是否支持多点触碰
  _touchLabel.multipleTouchEnabled = YES;
  [self.view addSubview:_touchLabel];
  
  _touchView = [[UIView alloc] initWithFrame:kVIEWFRAME];
  _touchView.backgroundColor = [UIColor purpleColor];
  [self.view addSubview:_touchView];
  
  for (int i = 1; i < 6; i++) {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(handleTap:)];
    // 连续点击的次数
    tap.numberOfTapsRequired = i;
    // 允许两个手指的触碰
    tap.numberOfTouchesRequired = 1;
    [_touchView addGestureRecognizer:tap];
  }
}

- (void)handleTap:(UITapGestureRecognizer *)tap {
  // 用户的第几个手指触碰
  NSUInteger touchNum = tap.numberOfTouches;
  NSUInteger tapNum = tap.numberOfTapsRequired;
  _touchLabel.text = [NSString stringWithFormat:@"tapNum: %lu touchNum: %lu",tapNum, touchNum];
  [_touchLabel performSelector:@selector(setText:) withObject:nil afterDelay:2];
}

- (void)setText:(UILabel *)label {
  [_touchLabel setText:@"aaa"];
  
  return;
}
@end
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值