将平移手势识别器限制为imageview

暴徒

我有一个在内绘制圆形渐变的函数ImageView,我有一个panGestureRecognizer充当颜色选择器的人。

如何限制用户的触摸输入仅在其触摸在ImageView的边界内时才适用

我尝试了此代码,但没有结果:

@interface testViewController ()

@end

@implementation testViewController

- (void)viewDidLoad {
    [super viewDidLoad];      

    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];;
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    [self.view addGestureRecognizer: panGesture];
    colorView = [[UIView alloc] init];
    colorView.frame = CGRectMake(0, 00, 50, 50);

    rText.textAlignment = NSTextAlignmentCenter;
    gText.textAlignment = NSTextAlignmentCenter;
    bText.textAlignment = NSTextAlignmentCenter;
    saturationText.textAlignment = NSTextAlignmentCenter;
    alphaText.textAlignment = NSTextAlignmentCenter;
    brightnessText.textAlignment = NSTextAlignmentCenter;

    rText.keyboardType = UIKeyboardTypeNumberPad;
    gText.keyboardType = UIKeyboardTypeNumberPad;
    bText.keyboardType = UIKeyboardTypeNumberPad;

    saturationText.keyboardType = UIKeyboardTypeNumberPad;
    alphaText.keyboardType = UIKeyboardTypeNumberPad;
    brightnessText.keyboardType = UIKeyboardTypeNumberPad;

    [self.view addSubview:gradientView];
    [self.view addSubview:colorView];
    gradientView.frame = CGRectMake(0,0,size.width,size.height);
}

- (void)didReceiveMemoryWarning  {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)handlePan:(UIPanGestureRecognizer *)sender {
    if (sender.numberOfTouches)  {
        CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height/2);

        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];
        }
    }
}

-(void)blueSlider:(id)sender {
    bSlider = (UISlider *)sender;

    bText.text = [NSString stringWithFormat:@"%.0f",bSlider.value];
    bSlider.value = bText.text.floatValue;
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
    colorView.backgroundColor = newColor;
}

-(void)greenSlider:(id)sender  {
    gSlider = (UISlider *)sender;

    gText.text = [NSString stringWithFormat:@"%.0f",gSlider.value];
    gSlider.value = gText.text.floatValue;
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
    colorView.backgroundColor = newColor;
}

-(void)redSlider:(id)sender  {
    rSlider = (UISlider *)sender;

    rText.text = [NSString stringWithFormat:@"%.0f",rSlider.value];
    rSlider.value = rText.text.floatValue;
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
    colorView.backgroundColor = newColor;
}

-(void)saturate:(id)sender  {
    saturationSlider = (UISlider *)sender;

    UIColor *currentColor = colorView.backgroundColor;
    CGFloat hue, saturation, brightness, alpha;
    BOOL success = [currentColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
    UIColor *newColor = [UIColor colorWithHue:hue saturation:saturationSlider.value brightness:hellSlider.value alpha:alphaSlider.value];

    colorView.backgroundColor = newColor;
    alphaText.text = [NSString stringWithFormat:@"%.2f",alphaSlider.value];
    brightnessText.text = [NSString stringWithFormat:@"%.2f",hellSlider.value];
    saturationText.text = [NSString stringWithFormat:@"%.2f",saturationSlider.value];
}

- (void)changeOpacity:(id)sender {
    alphaSlider = (UISlider *)sender;
    float red = r;
    float green = g;
    float blue = b;
    UIColor *color2 = [UIColor colorWithRed:red green:green blue:blue alpha: alphaSlider.value];
    colorView.backgroundColor = color2;
    alphaText.text = [NSString stringWithFormat:@"%.2f",alphaSlider.value];
    brightnessText.text = [NSString stringWithFormat:@"%.2f",hellSlider.value];
    saturationText.text = [NSString stringWithFormat:@"%.2f",saturationSlider.value];
}

- (void)changeBrightness:(id)sender {
    hellSlider = (UISlider *)sender;

    UIColor *currentColor = colorView.backgroundColor;
    CGFloat hue, saturation, brightness, alpha;
    BOOL success = [currentColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
    brightness = hellSlider.value;
    UIColor *newColor = [UIColor colorWithHue:hue saturation:saturationSlider.value brightness:hellSlider.value alpha:alphaSlider.value];

    colorView.backgroundColor = newColor;
    alphaText.text = [NSString stringWithFormat:@"%.2f",alphaSlider.value];
    brightnessText.text = [NSString stringWithFormat:@"%.2f",hellSlider.value];
    saturationText.text = [NSString stringWithFormat:@"%.2f",saturationSlider.value];  
}

@end
埃利·加内姆(Eli Ganem)

替换此行

[self.view addGestureRecognizer: panGesture];

有了这个

[gradientView addGestureRecognizer:panGesture];

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将平移手势限制为一个方向

来自分类Dev

使用平移手势识别器获得平稳的旋转

来自分类Dev

抓住22将平移手势识别器创建为常量?

来自分类Dev

用捏手势限制平移手势

来自分类Dev

用捏手势限制平移手势

来自分类Dev

将多个参数传递给手势识别器

来自分类Dev

UIScrollView覆盖我的子视图的平移手势识别器

来自分类Dev

解决两个平移手势识别器之间的冲突

来自分类Dev

将UIPageViewController(带有TransitionStyleScroll)的平移手势限制到某个区域

来自分类Dev

SpriteKit手势识别器

来自分类Dev

禁用手势识别器

来自分类Dev

UIImage的手势识别器

来自分类Dev

庞手势识别器停止

来自分类Dev

订购手势识别器

来自分类Dev

如何将每个手势的ViewPager页面更改限制为一个

来自分类Dev

在拖动手势时将矩形限制为屏幕边缘

来自分类Dev

垂直平移手势

来自分类Dev

限制手势

来自分类Dev

控制器上的滑动和平移手势之间的iOS手势冲突

来自分类Dev

使用屏幕边缘平移手势识别器的弹出视图控制器未跟随拇指

来自分类Dev

如何在平移手势识别器中访问我的总 x 翻译并从 XAML 访问它

来自分类Dev

UIView平移手势与系统手势冲突

来自分类Dev

将手势识别器放在视图或控制器类中

来自分类Dev

当手势已经处于活动状态时,如何启动手势识别器?

来自分类Dev

我可以将手势识别器的代码放在视图的子类中吗?

来自分类Dev

如何将手势识别器添加到ViewController及其子级?

来自分类Dev

如何将多个手势识别器添加到TextSpan?

来自分类Dev

如何将手势识别器分配给集合?

来自分类Dev

将手势识别器添加到UIImage,而不是UIImageView

Related 相关文章

热门标签

归档