2

我正在使用在图像视图中绘制的圆形渐变作为我的颜色选择器的色轮。我想将平移手势识别器限制在shouldReceiveTouch:渐变的位置,以便视图的其余部分不受平移手势识别器的影响。

如何指定与渐变半径匹配的圆形边界?

编辑:我尝试了以下代码但没有效果:

if (sender.numberOfTouches)
    {
        CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height/3);

        float radius = MIN(size.width, size.height)/2;

        [alphaSlider addTarget:self action:@selector(changeOpacity:) forControlEvents:UIControlEventValueChanged];
        [hellSlider addTarget:self action:@selector(changeBrightness:) forControlEvents:UIControlEventValueChanged];
        [saturationSlider addTarget:self action:@selector(saturate:) forControlEvents:UIControlEventValueChanged];
        [rSlider addTarget:self action:@selector(redSlider:) forControlEvents:UIControlEventValueChanged];
        [gSlider addTarget:self action:@selector(greenSlider:) forControlEvents:UIControlEventValueChanged];
        [bSlider addTarget:self action:@selector(blueSlider:) forControlEvents:UIControlEventValueChanged];




        CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: gradientView];
        CGPoint center = CGPointMake((size.width/2), (size.height /2));
        CGPoint delta = CGPointMake(lastPoint.x - center.x,  lastPoint.y - center.y);
        CGFloat angle = (delta.y == 0 ? delta.x >= 0 ? 0 : M_PI : atan2(delta.y, delta.x));
        angle = fmod(angle,  M_PI * 2.0);
        angle += angle >= 0 ? 0 : M_PI * 2.0;
        if((lastPoint.x - center.x) + (lastPoint.y - center.y)/2 < radius)
        {
            UIColor *color = [UIColor colorWithHue: angle / (M_PI * 2.0) saturation:saturationSlider.value brightness:hellSlider.value alpha:alphaSlider.value];
            if ([color getRed: &r green: &g blue:&b alpha: &a])
            {
                NSLog(@"Color value - R : %g G : %g : B %g", r*255, g*255, b*255);
            }
            float red = r;
            float green = g;
            float blue = b;
            rText.text = [NSString stringWithFormat:@"%.0f",rSlider.value];
            gText.text = [NSString stringWithFormat:@"%.0f",green*255];
            bText.text = [NSString stringWithFormat:@"%.0f",blue*255];


            colorView.backgroundColor = [UIColor colorWithRed:(rText.text.floatValue/255) green:(gText.text.floatValue/255) blue:(bText.text.floatValue/255) alpha:alphaSlider.value];
            rSlider.value = red*255;
            gSlider.value = green*255;
            bSlider.value = blue*255;
            alphaText.text = [NSString stringWithFormat:@"%.2f",alphaSlider.value];
            brightnessText.text = [NSString stringWithFormat:@"%.2f",hellSlider.value];
            saturationText.text = [NSString stringWithFormat:@"%.2f",saturationSlider.value];

        }





    }

}

它似乎在渐变的右侧工作并停止接收颜色更新,左侧和下侧仍在更新。我需要panGesture适当地限制它以停止干扰我的UISliders.

有任何想法吗?

4

1 回答 1

1

您只需要对触摸事件进行命中测试以查看它是否在圆圈的区域内。为此,您需要知道圆的中心点及其半径。

这个现有问题涵盖了这样做的一般方程式:

测试点是否在圆内的方程

于 2013-07-10T10:26:47.397 回答