Switches and Ifs - ios

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?

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.

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.

Related

Programatically change custom UiButton background color

Within my custom UIButton class called "ComingHomeButton", I'm not able to change the backgroundcolor
I would like to change it without creating images, but if it is the only way I will have to.
I also want to change the background color every time the button is tapped.
Here is what I have that doesnt work:
UIColor *color = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
[self setBackgroundColor:color];
self refers to my "ComingHomeButton"
Thank you so much :-)
You can create a counter, and then loop through colors when a button is tapped:
-(IBAction)buttonTapped:(id)sender {
if (!counter) {
counter = 0
}
if (counter%3 == 0) {
self.backgroundColor = [UIColor redColor]; //or whatever custom color you want to use
} else if (counter%3 == 1) {
self.backgroundColor = [UIColor blueColor];
} else if (counter%3 == 2) {
self.backgroundColor = [UIColor greenColor];
}
counter++
}
and you can add as many colors as you want to loop through. The % denotes the mod function, making sure that when counter is bigger than 2, it will still return a number between 0 and 2.
The number after the % is the number of colors to loop through.
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
btn.backgroundColor = [UIColor blueColor];
[self.view addSubview:btn];
The following code works fine for adding a button the a given view with a background button. Try using the dot notation instead of the message pass.
-(IBAction)buttonTapped:(id)sender {
sender.backgroundColor = [self randomColor];
}
-(UIColor *)randomColor
{
CGFloat red = arc4random() % 255 / 255.0;
CGFloat green= arc4random() % 255 / 255.0;
CGFloat blue= arc4random() % 255 / 255.0;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
return color;
}
I did not test the code I am not on my mac but it should work.

Sometimes category method return nil after UIImagePickerController

I use simple UIColor category for random color.
+ (UIColor *)randomColor {
return [UIColor colorWithRed:arc4random_uniform(255)/255.0
green:arc4random_uniform(255)/255.0
blue:arc4random_uniform(255)/255.0
alpha:1.0];
}
And I tested this code in viewDidAppear: method.
Method method = class_getClassMethod([UIColor class], #selector(randomColor));
NSLog(#"color=%#, method=%d", [UIColor randomColor], (method != NULL));
UIColor * color = [UIColor colorWithRed:arc4random_uniform(255)/255.0
green:arc4random_uniform(255)/255.0
blue:arc4random_uniform(255)/255.0
alpha:1.0];
NSLog(#"color=%#", color);
First time when viewC did appear, it works very well.
color=UIDeviceRGBColorSpace 0.0509804 0.0862745 0.160784 1, method=1
color=UIDeviceRGBColorSpace 0.784314 0.509804 0.964706 1
But after I present UIPickerImageController and cancel it,
it shows different result.
color=(null), method=1
color=UIDeviceRGBColorSpace 0.254902 0.576471 0.937255 1
Why this situation happen?
It is really weird. :(

iOS - Text displayed on animation

I am animating a set of frames in iOS and would like to have a text displayed at the end of it. At this point I'm only able to animate the images. The text I'm trying to display on it using CATextLayer does not show up. I simply don't know how to make the layer a subview of the view. Also the initWithFrame method I was trying to use does not seem to work with UIViewController. Could anyone help out?
1) Here's the animation code that works perfectly:
//setup animation
NSArray *theFrames = [NSArray array];
theFrames = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"frame1.png"],
[UIImage imageNamed:#"frame2.png"],
[UIImage imageNamed:#"frame3.png"],
[UIImage imageNamed:#"frame4.png"],
nil];
sceneFrames.animationImages = theFrames;
sceneFrames.animationDuration = 1;
sceneFrames.animationRepeatCount = 1;
sceneFrames.image = [theFrames objectAtIndex:theFrames.count - 1];
[sceneFrames startAnimating];
2) Here's how I'm trying to display a layer containing text in it. As you can see I'm not even at the point to display it at the end. Still trying to have it there in the first place and already running into issues:
// Create the new layer object
boxLayer = [[CATextLayer alloc] init];
// Give it a size
[boxLayer setBounds:CGRectMake(0.0, 0.0, 300.0, 85.0)];
// Give it a location
[boxLayer setPosition:CGPointMake(160.0, 350.0)];
// Make half-transparent red the background color for the layer
UIColor *reddish = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
// Get CGColor object with the same color values
CGColorRef cgReddish = [reddish CGColor];
[boxLayer setBackgroundColor:cgReddish];
// Make it a sublayer on the view's layer
// THIS LINE DOES NOT WORK AND GIVES ME HEADACHES. It does not work without initWithFrame, which does not seem to work with UIViewController...
[[self layer] addSublayer:boxLayer];
// Create string
NSString *text2 = #"You are me.";
// Set font
[boxLayer setFont:#"Times New Roman"];
// Set font size
[boxLayer setFontSize:20.0];
[boxLayer setAlignmentMode:kCAAlignmentCenter];
// Assign string to layer
[boxLayer setString:text2];
replace
[[self layer] addSublayer:boxLayer];
with
[self.view.layer addSublayer:boxLayer];
or
[[self.view layer] addSublayer:boxLayer];
they're both the same
Edit
after testing, it worked perfect for me

Font Size and Type not applying with CATextLayer

I am trying to modify the font properties of a text within a layer but it does not happen. Could anyone help out? Please find code below:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// All HypnosisViews start with a clear background color
[self setBackgroundColor:[UIColor clearColor]];
[self setCircleColor:[UIColor lightGrayColor]];
// Create the new layer object
boxLayer = [[CATextLayer alloc] init];
// Give it a size
[boxLayer setBounds:CGRectMake(0.0, 0.0, 300.0, 85.0)];
// Give it a location
[boxLayer setPosition:CGPointMake(160.0, 350.0)];
// Make half-transparent red the background color for the layer
UIColor *reddish = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
// Get CGColor object with the same color values
CGColorRef cgReddish = [reddish CGColor];
[boxLayer setBackgroundColor:cgReddish];
// Make it a sublayer on the view's layer
[[self layer] addSublayer:boxLayer];
NSString *text2 = #"You are me.";
UIFont *font2 = [UIFont fontWithName:#"Times New Roman" size:10.0];
[text2 sizeWithFont:font2];
[boxLayer setString:text2];
}
return self;
}
To change the font / font size of a CATextLayer, you have to assign values to the "font" and "fontSize" properties of the layer.
Or you have to use an NSAttributedString in which case the values of that string object are used.
The "sizeWithFont" call you use is an NSString addition that does nothing but calculate and return a CSSize with the width and height of the text you give it in the font you give it. As you don't use the returned CGSize in your code, it does absolutely nothing.
Reference in the Apple docs.

This objective C code does not change the color of label text

Debugger show some value in color . Please what am I doing wrong
#synthesize textLabel;
#synthesize textField;
#synthesize sliderRed;
#synthesize sliderGreen;
#synthesize sliderBlue;
- (void)dealloc
{
[textLabel release];
[super dealloc];
}
- (IBAction)updateLabel
{
NSString * textValue = [textField text];
float red = [sliderRed value]/255.f;
float green = [sliderGreen value]/255.f;
float blue = [sliderBlue value]/255.f;
UIColor *textColour = [[UIColor alloc]initWithRed:red green:green blue:blue alpha:1.0];
[textLabel setText:textValue];
[textLabel setTextColor:textColour];
}
I guess sliderRed, sliderGreen and sliderBlue are UISlider instances? What are their min/max values? If you left it at default 0.0 to 1.0 then this code would give you some really low values:
float red = [sliderRed value]/255.f;
float green = [sliderGreen value]/255.f;
float blue = [sliderBlue value]/255.f;
UIColor's method you use takes float parameters from 0.0 to 1.0 so simply passing it the slider values without dividing them would work.
And don't forget to put [textColour release]; at the end of that method or you will be leaking a new UIColor instance every time the method gets called.
Nothing wrong on your codes. maybe something wrong in other places.
Try to replace this line:
UIColor *textColour = [[UIColor alloc]initWithRed:red green:green blue:blue alpha:1.0];
to
UIColor *textColour = [UIColor redColor];
And check if the redColor works. if not, so it might something wrong in your other codes.
Try adding [textLabel setNeeedsDisplay] after you've changed the drawing attributes.

Resources