我使用ImageView
渲染圆形渐变来选择颜色。
问题是,只要我的panGestureRecognizer
保留在矩形 ImageView
内,即使在圆形>之外,他也会继续返回颜色渐变。
有没有办法强制执行循环边界?
以下是将渐变添加到ImageView
:
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];;
答案 0 :(得分:0)
你应该检查你的触摸是否在想象的圆圈内。
你需要的只是非常基本的三角学。您需要计算触摸点和视图中心之间的距离。如果该距离大于您想要返回的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
}