Edgar
Edgar

Reputation: 927

Switches and Ifs

I've got a slider that changes blur radius:

- (IBAction)sliderValueChangesFinderUp:(id)sender {

_sliderValue = round(self.blurSlider.value);

_effectImage = nil;
_effectImage = [BlurFilter imageByApplyingClearEffectToImage:self.myImage
      withRadius:_sliderValue color:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];

self.imageView.image = _effectImage; 
}

also I've got a button which should change blur color (the part - [UIColor colorWith..])

- (IBAction)setColorGreen:(id)sender {

_effectImage = nil;
_effectImage = [BlurFilter imageByApplyingClearEffectToImage:self.myImage 
withRadius:_sliderValue color:[UIColor colorWithRed:0 green:1 blue:0 alpha:0.15]];

self.imageView.image = _effectImage;
}

This button change the color, but when I want to change blur radius the color is resetted , I know that this is because of the code in - (IBAction)sliderValueChangesFinderUp:(id)sender.

But how should I create a switch or if correctly so when the button green is pressed blur color changes and I may change blur radius without resseting a color?

Upvotes: 0

Views: 47

Answers (2)

greg
greg

Reputation: 4953

I believe what you're asking is that you'd like to change the color of the blur passed to imageByApplyingClearEffectToImage. A way to do this might be to move the code from the the control actions into a separate message. Both actions would call this message, but you could alter the color. Consider something like the following:

- (IBAction)sliderValueChangesFinderUp:(id)sender
{
    _effectImage = [self blurImage:self.myImage
                         withColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
    self.imageView.image = _effectImage;
}

- (IBAction)setColorGreen:(id)sender
{
    _effectImage = [self blurImage:self.myImage
                         withColor:[UIColor colorWithRed:0 green:1 blue:0 alpha:0.15]];
    self.imageView.image = _effectImage;
}

- (UIImage *)blurImage:(UIImage *)image withColor:(UIColor *)color
{
    return ([BlurFilter imageByApplyingClearEffectToImage:image
                                               withRadius:_sliderValue
                                                    color:color]);
}

You're reusing code here by making the color a parameter instead of hardcoding it into a separate function.

Upvotes: 0

Shamas S
Shamas S

Reputation: 7549

Keep a class level variable for UIColor, for example named colorObject. In your function - (IBAction)sliderValueChangesFinderUp:(id)sender access that variable and set it in your line

_effectImage = [BlurFilter imageByApplyingClearEffectToImage:self.myImage
      withRadius:_sliderValue color:colorObject];

instead of creating a new one from scratch.

In you function - (IBAction)setColorGreen:(id)sender , modify that colorObject variable if you need to.

Upvotes: 1

Related Questions