Ios 给出ImageView循环边界?

Ios 给出ImageView循环边界?,ios,uiimageview,Ios,Uiimageview,我正在使用ImageView渲染圆形渐变以选择颜色 问题是,只要我的pangestrerecognizer仍在矩形

我正在使用
ImageView
渲染圆形渐变以选择颜色

问题是,只要我的
pangestrerecognizer
仍在矩形
内,即使在圆形渐变外,他也会继续返回颜色

有没有办法强制执行循环边界

下面是将渐变添加到
图像视图中的代码:

CGSize size = CGSizeMake(self.view.bounds.size.width, ((self.view.bounds.size.height)/2));
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), YES, 0.0);
[[UIColor whiteColor] setFill];
UIRectFill(CGRectMake(0, 0,size.width,size.height));


int sectors = 180;
float radius = MIN(size.width, size.height)/2;
float angle = 2 * M_PI/sectors;
UIBezierPath *bezierPath;
for ( int i = 0; i < sectors; i++)
{
    CGPoint center = CGPointMake(((size.width)/2), ((size.height)/2));
    bezierPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:i * angle endAngle:(i + 1) * angle clockwise:YES];
    [bezierPath addLineToPoint:center];
    [bezierPath closePath];
    UIColor *color = [UIColor colorWithHue:((float)i)/sectors saturation:1. brightness:1. alpha:1];
    [color setFill];
    [color setStroke];
    [bezierPath fill];
    [bezierPath stroke];
}
img = UIGraphicsGetImageFromCurrentImageContext();
gradientView = [[UIImageView alloc]initWithImage:img];;
CGSize size=CGSizeMake(self.view.bounds.size.width,((self.view.bounds.size.height)/2);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width,size.height),YES,0.0);
[[UIColor whiteColor]setFill];
UIRectFill(CGRectMake(0,0,size.width,size.height));
int扇区=180;
浮动半径=最小值(尺寸.宽度,尺寸.高度)/2;
浮动角度=2*M_PI/扇区;
UIBezierPath*bezierPath;
对于(int i=0;i
您应该检查您的触摸是否在假想的圆圈内

你所需要的只是基本的三角函数。您需要计算接触点与视图中心之间的距离。如果该距离大于colorView的半径,则希望返回而不更改为新颜色(或设置colorView边缘的值)

我用这样的东西作为色调和饱和度选择器

- (void)gestureRecognizerDidPan:(UIPanGestureRecognizer *)gesture {
    CGFloat saturation = 0.0f;
    CGFloat hue = 0.0f;

    UIView *colorView = gesture.view;
    CGPoint location = [gesture locationInView:colorView];

    // we assume that the colored area is a circle inside of a square rect
    CGPoint colorViewCenter = CGPointMake(ceilf(CGRectGetWidth(colorView.bounds)/2.0f), ceilf(CGRectGetHeight(colorView.bounds)/2.0f));
    CGFloat colorViewRadius = floorf(CGRectGetWidth(colorView.bounds)/2.0f);

    CGFloat dx = location.x - colorViewCenter.x;
    CGFloat dy = location.y - colorViewCenter.y;
    CGFloat touchRadius = sqrtf(powf(dx, 2)+powf(dy, 2));
    if (touchRadius > colorViewRadius) {
        // touch was outside of circular area

        // set saturation to max
        saturation = 1.0f;

        // or: 
        // return;
    }
    else {
        saturation = touchRadius / colorViewRadius;
    }

    CGFloat angleRad = atan2f(dx, dy);
    CGFloat angleDeg = (angleRad * (180.0f/M_PI) - 90);
    if (angleDeg < 0.f) {
        angleDeg += 360.f;
    }
    hue = angleDeg / 360.0f;

    // tell your target
}
-(void)手势识别器didpan:(uipangestureerecognizer*)手势{
CGFloat饱和=0.0f;
CGFloat色调=0.0f;
UIView*colorView=signature.view;
CGPoint位置=[手势位置视图:颜色视图];
//我们假设彩色区域是正方形矩形内的一个圆
CGPoint colorViewCenter=CGPointMake(ceilf(CGRectGetWidth(colorView.bounds)/2.0f)、ceilf(CGRectGetHeight(colorView.bounds)/2.0f);
CGFloat colorViewRadius=floorf(CGRectGetWidth(colorView.bounds)/2.0f);
CGFloat dx=location.x-colorViewCenter.x;
CGFloat dy=location.y-colorViewCenter.y;
CGFloat-touchRadius=sqrtf(功率(dx,2)+功率(dy,2));
如果(touchRadius>colorViewRadius){
//触摸在圆形区域之外
//将饱和度设置为最大值
饱和度=1.0f;
//或:
//返回;
}
否则{
饱和度=触摸半径/彩色视半径;
}
CGFloat angleRad=atan2f(dx,dy);
CGFloat angleDeg=(angleRad*(180.0f/M_-PI)-90);
如果(角度度<0.f){
角度度+=360.f;
}
色调=角度度/360.0f;
//告诉你的目标
}

检查您的平底锅的位置。如果它在想象的圆之外,就不返回颜色。人们会怎么做呢?