iOS - 如何计算特定视图内的点击次数?

发布于 2024-10-10 22:58:38 字数 231 浏览 0 评论 0原文

我正在尝试从 iOS 4.2 示例“Touches”演变,但我做不到(我是 iOS 新手): 我想计算每个不同 UIImageView 上的点击次数。目前,无论我在视图中、UIImageView 外部等位置按下,样本计数都会点击。我想要的是显示我在特定 UIImageView 内点击了多少次点击。
输出将是一个标签,上面写着 7 Taps on the red Button;轻按2次黄色按钮;轻按 3 次绿色

I'm trying to evolve from the iOS 4.2 sample "Touches" but I can't do it (I'm new to the iOS):
I'd like to count taps on each of the different UIImageViews. Currently the sample count taps no matter where I press, in the views, outside the UIImageView(s), etc. What I want is to show how many taps I'm tapping inside a specific UIImageView.
The output would be a label saying 7 taps on the red button; 2 taps on the yellow button; 3 taps on the green.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2024-10-17 22:58:38

好的,我明白了:

NSUInteger touchCount = 0;
for (UITouch *touch in touches) {
    if(numTaps >= 2) {
        CGPoint touchPoint = [touch locationInView:self];
        if (CGRectContainsPoint([firstTapView frame], touchPoint)) {
            firstTapView.text = [NSString stringWithFormat:@"%d",numTaps];
        } else if (CGRectContainsPoint([secondTapView frame], touchPoint)) {
            secondTapView.text = [NSString stringWithFormat:@"%d",numTaps];
        } else if (CGRectContainsPoint([thirdTapView frame], touchPoint)) {
            thirdTapView.text = [NSString stringWithFormat:@"%d",numTaps];
        } 

    }

    touchCount++;  
}   

其中firstTapView、secondTapView 和thirdTapView 是我的UILabels,显示在屏幕上。 Touches 示例使用 UIImageView,但我将其更改为 UILabel,这样我就可以在触摸屏幕的同时进行书写。

OK I got it:

NSUInteger touchCount = 0;
for (UITouch *touch in touches) {
    if(numTaps >= 2) {
        CGPoint touchPoint = [touch locationInView:self];
        if (CGRectContainsPoint([firstTapView frame], touchPoint)) {
            firstTapView.text = [NSString stringWithFormat:@"%d",numTaps];
        } else if (CGRectContainsPoint([secondTapView frame], touchPoint)) {
            secondTapView.text = [NSString stringWithFormat:@"%d",numTaps];
        } else if (CGRectContainsPoint([thirdTapView frame], touchPoint)) {
            thirdTapView.text = [NSString stringWithFormat:@"%d",numTaps];
        } 

    }

    touchCount++;  
}   

where firstTapView, secondTapView and thirdTapView are my UILabels, shown on the screen. Touches sample uses UIImageView, but I changed it to UILabel, so I can write whilst touching the screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文