为什么将 UILabel 设置为完全透明会丢失点击手势?

编程入门 行业动态 更新时间:2024-10-26 19:35:31
本文介绍了为什么将 UILabel 设置为完全透明会丢失点击手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想不通,我也不认为这个也确实解释了.

I can't figure this out, and I don't think this really explains it either.

我有一个 UILabel,用户可以点击它来隐藏或显示它,设置如下:

I have a UILabel that can be tapped by the user to hide or show it, set up like this:

self.numberLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(hideOrShowNumber)];
[self.numberLabel addGestureRecognizer:tapGesture];

我想通过在 UILabel 上设置 alpha 值来为标签的隐藏和显示设置动画.但是,如果我将 alpha 值设置为 0.0f,则标签不再接受点击,因此即使用户可以隐藏标签,她也无法再显示!

I would like to animate the hiding and showing of the label by setting the alpha value on the UILabel. However, if I set the alpha value to 0.0f, the label no longer accepts taps, so even if the user can hide the label, she can't show it anymore!

我的解决方法是这样的:

My workaround goes like this:

隐藏标签时:- 将 alpha 值设置为 0.0f.- 将标签的文本颜色设置为黑色(由于背景为黑色,因此不可见)- 将 alpha 重置为 1.0f.

When hiding the label: - Animate the alpha value to 0.0f. - Set the text color of the label to black (which makes it invisible because the background is black) - Reset the alpha to 1.0f.

显示标签时:- 将 alpha 设置为 0.0f(因为隐藏标签时它保持在 1.0f).- 将文本颜色设置为黑色以外的其他颜色(取决于游戏状态).- 将 alpha 值设置为 1.0f.

When showing the label: - Set the alpha to 0.0f (because it was left at 1.0f when the label was hidden). - Set the text color to another color than black (depending on game state). - Animate the alpha value to 1.0f.

代码如下(其中包含一些状态变量,但self.numberLabel是对UILabel的引用):

The code looks like this (there are some state variables included, but self.numberLabel is the reference to the UILabel):

NSTimeInterval duration = 0.6f;

if (self.numberIsVisible) {
    [UIView animateWithDuration:duration
                     animations:^{
                         self.numberLabel.alpha = 0.0f;
                     }
                     completion:^(BOOL done) {
                         self.numberLabel.textColor = [UIColor blackColor];
                         self.numberLabel.alpha = 1.0f;
                     }
    ];
    self.numberIsVisible = NO;
}
else {
    UIColor *rightColor = [UIColor whiteColor];

    if ([GameState sharedGameState].haveMatch) {
        rightColor = [UIColor colorWithRed:0.0/255.0 green:127.0/255.0 blue:255.0/255.0 alpha:1.0];
    }

    self.numberLabel.alpha = 0.0f;
    self.numberLabel.textColor = rightColor;

    [UIView animateWithDuration:duration
                     animations:^{
                         self.numberLabel.alpha = 1.0f;
                     }
    ];

    self.numberIsVisible = YES;
}

它有效,但有点笨重.

那么问题是,为什么设置 UILabel 的透明度会使其失去用户交互?这是设计使然吗,它是否记录在某处?我在 UIGestureRecognizer 文档中找不到任何关于此的信息.

So the question is, why does setting the transparency of the UILabel make it lose user interaction? Is this by design, and is it documented somewhere? I can't find anything about this in the UIGestureRecognizer docs.

推荐答案

来自官方 doc(调节触摸事件传递部分)

关闭传递触摸事件.默认情况下,视图接收触摸事件,但您可以将其 userInteractionEnabled 属性设置为NO 关闭触摸事件的传递.一个视图也没有收到这些事件是隐藏的还是透明的.

Turning off delivery of touch events. By default, a view receives touch events, but you can set its userInteractionEnabled property to NO to turn off delivery of touch events. A view also does not receive these events if it’s hidden or if it’s transparent.

在视图上具有完全透明度 (alpha = 0) 被认为与隐藏视图类似,因此在这种情况下没有理由处理用户交互.您可以尝试使用近乎透明的 UILabel 代替.0.1 的 alpha 似乎是极限.

Having full transparency (alpha = 0) on a view is consider to be similar as having a view hidden so there is no reason the user interaction should he handled in that case. You could try to have nearly-transparent UILabel instead. An alpha of 0.1 seems to be the limit.

这篇关于为什么将 UILabel 设置为完全透明会丢失点击手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 04:24:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   手势   透明   UILabel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!