【发布时间】:2016-07-25 19:47:14
【问题描述】:
我有一个带有色轮的应用程序,我正在尝试在色轮中选择一种随机颜色。但是,我在验证随机点是否在色轮内时遇到问题。
这是当前的代码:
CGPoint randomPoint = CGPointMake(arc4random() % (int)colorWheel.bounds.size.width, arc4random() % (int)colorWheel.bounds.size.height);
UIColor *randomColor = [self colorOfPoint:randomPoint];
CGPoint pointInView = [colorWheel convertPoint:randomPoint fromView:colorWheel.window];
if (CGRectContainsPoint(colorWheel.bounds, pointInView)) {
NSLog(@"%@", randomColor);
}
else {
NSLog(@"out of bounds");
}
我尝试过的验证点的其他几种方法都没有成功:
if (CGRectContainsPoint(colorWheel.frame, randomPoint)) {
NSLog(@"%@", randomColor);
}
if ([colorWheel pointInside:[self.view convertPoint:randomPoint toView: colorWheel] withEvent: nil]) {
NSLog(@"%@", randomColor);
}
有时它会输出“超出范围”,有时它只会输出颜色为白色(色轮周围的背景当前为白色,但色轮图像中没有白色)。
色轮图像是一个圆圈,所以我不确定这是否会导致测试失败,尽管白色似乎过于频繁地弹出,以至于它只是图像周围的透明方形轮廓,呈现白色.
【问题讨论】:
-
你的 colorWheel 是一个自定义的 UIView?那为什么不实现
func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)并检查你的 colorWheel 的哪一块被触摸了? -
@ReinierMelian 你发布了
touchesBegan:的Swift 版本。 OP 正在寻找一种 Objective-C 解决方案。可能会令人困惑。 -
@jsondwyer 你是对的,对不起这是objective-c版本
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event -
我已经有办法触摸屏幕并得到点,想法是它随机生成点而不需要交互
标签: ios objective-c cocoa-touch uiview geometry