iOS - UITapGesture 是否开始和结束?

标签 ios xcode uibutton uitapgesturerecognizer

问题:

我正在尝试使用UITapGestureRecognizer更改 View 的背景颜色。本质上,当用户点击时,我希望背景改变颜色,并且在释放或完成时,背景颜色返回到零,或者没有背景颜色。

此操作类似于默认的 UIButton,但我需要将其放在 View 上。

问题:

我尝试使用类似的代码:

- (void)handleTapGesture:(UITapGestureRecognizer *)recognizer
{
        if(recognizer.state == UIGestureRecognizerStateBegan) {
            self.backgroundColor = [UIColor greenColor];
        }
        
        if(recognizer.state == UIGestureRecognizerStateEnded) {
            self.backgroundColor = nil;
        }
}

没有任何成功。这是了解点击手势何时开始和结束的正确方法吗?

最佳答案

使用这些方法可能会更容易,这些方法应该已经成为 UIViewController 中响应程序链的一部分,除非它们已经在其他地方处理

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    
    //CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    NSLog(@"began");
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    //CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    NSLog(@"end");
}

关于iOS - UITapGesture 是否开始和结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11398403/

相关文章:

ios - xcode中的特定对象选择

ios - 如何将图像放在 Swift 的 UIImageView 上?

ios - .xib 文件不在 Compile Sources 构建阶段,但仍能正确构建;怎么会这样?

iphone - 在 MapView 上点击注释时隐藏弹出窗口

iphone - Interface Builder(iPhone dev)自定义按钮背景图像不考虑拉伸(stretch)设置

ios - UIBarButtonItem 让我的 iPhone 应用崩溃

ios - iOS7 设备上的 SKEmitter 错误

iphone - 在 iOS 中将图像淡入透明

ios - 按钮作为 Swift 中滑动手势 Action 的参数

ios - UIButton (IB) 中的属性 titleLabel 不尊重 setFont