gpt4 book ai didi

ios - 在 Objective-C 中定义一个位置矩形

转载 作者:行者123 更新时间:2023-11-29 03:06:25 24 4
gpt4 key购买 nike

我在 Iphone 应用程序中有一个图像,我希望当用户点击定义区域(位置范围或矩形定义的区域)内的位置时,它会触发另一个事件。我知道如何获取单个位置,但不知道如何定义矩形区域。我正在寻找一种简单的方法来实现它。谢谢

最佳答案

如果您想让矩形可见,您可以将 ImageView 添加到您的 View 并设置点击识别器。但是如果你不想让矩形可见,你可以覆盖 touchesBegan:withEvent: 方法并使用 CGRectContainsPoint:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];

CGRect rect = CGRectMake(0.0, 0.0, 100, 100); //<- this is the rectangle you do check on
if (CGRectContainsPoint(rect, touchLocation)) {
NSLog(@"You tapped inside rectangle");
}
else {
NSLog(@"You missed rectangle");
}
}

关于ios - 在 Objective-C 中定义一个位置矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22716753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com