bingoogolapple / bingoogolapple.github.io

个人主页。同时也通过 Issues 记录学习笔记
http://www.bingoogolapple.cn
86 stars 19 forks source link

UIView不接收触摸事件的三种情况及手势识别 #63

Open bingoogolapple opened 9 years ago

bingoogolapple commented 9 years ago

UIView不接收触摸事件的三种情况

不接收用户交互

userInteractionEnable = NO

隐藏

hidden = YES

透明

alpha = 0.0~0.01

提示:UIImageView的userInteractionEnable默认就是NO,因此UIImageView以及它的子控件默认是不能接收触摸事件的

bingoogolapple commented 9 years ago

敲击手势识别(研究手势识别代理)

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 0 .打开UIImageView的用户交互
    self.iconView.userInteractionEnabled = YES;
     /*
    // 1.创建手势识别器
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    // 1.1设置手势识别器的属性
    // 设置用户必须点击几次才能出发点击事件
    tap.numberOfTapsRequired = 2;
    // 设置用户必须两根手指同时点击才会促发事件
    tap.numberOfTouchesRequired = 2;

    // 2.添加手势识别器到view
    [self.iconView addGestureRecognizer:tap];

    // 3.监听手势识别器
    [tap addTarget:self action:@selector(tapView)];
     */

    [self.iconView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)]];
}

- (void)tapView
{
    NSLog(@"图片被点击了");
}
bingoogolapple commented 9 years ago

长按和轻扫

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 向上
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] init];
    // 设置轻扫的方向
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    [self.customView addGestureRecognizer:swipe];
    [swipe addTarget:self action:@selector(swipeView)];

    // 向下
    UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] init];
    // 设置轻扫的方向
    swipe2.direction = UISwipeGestureRecognizerDirectionDown;
    [self.customView addGestureRecognizer:swipe2];
    [swipe2 addTarget:self action:@selector(swipeView2)];

    // 左边
    UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc] init];
    // 设置轻扫的方向
    swipe3.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.customView addGestureRecognizer:swipe3];
    [swipe3 addTarget:self action:@selector(swipeView3)];

    // 右边
    UISwipeGestureRecognizer *swipe4 = [[UISwipeGestureRecognizer alloc] init];
    // 设置轻扫的方向
    swipe4.direction = UISwipeGestureRecognizerDirectionRight;
    [self.customView addGestureRecognizer:swipe4];
    [swipe4 addTarget:self action:@selector(swipeView4)];

}
- (void)swipeView4
{
    NSLog(@"轻扫事件右");
}

- (void)swipeView3
{
    NSLog(@"轻扫事件左");
}

- (void)swipeView2
{
    NSLog(@"轻扫事件下");
}

- (void)swipeView
{
    NSLog(@"轻扫事件上");
}

- (void)test
{
    // 长按事件
    // 1.创建手势识别器
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
    // 1.1设置长按手势识别器的属性
    //    longPress.minimumPressDuration = 5;

    // 手指按下后事件响应之前允许手指移动的偏移位
    longPress.allowableMovement = 50;

    // 2.添加手势识别器到View
    [self.customView addGestureRecognizer:longPress];

    // 3.监听手势识别器
    [longPress addTarget:self action:@selector(longPressView)];
}

 -(void)longPressView
{
    NSLog(@"长按事件");
}
bingoogolapple commented 9 years ago

通过下面的代理方法启用同时响应多个手势

shoudRecognizeSimulaneousWithGesutreRecognizer