IOS drawing App, change buttoncolor - ios

i made a simple drawing App, with a toolpanel which contains a pencil, a paint brush and an eraser. There a 12 different colors to draw with.
Now, i would like the pencil and the paint brush image, change in the color of the active color from the colorpanel. How do I do that?
Something like;
When toolbutton:pencil is pressed, color pencil (image) is color:changecolor.
How to translate this in code?
This is my code for the buttons.
- (IBAction)colorChange:(id)sender
{
UIButton *PressedButton = (UIButton*)sender;
switch (PressedButton.tag) {
case 0:
self.drawingView.lineColor = RGB(32, 152, 188);
break;
case 1:
self.drawingView.lineColor = RGB(33, 114, 177);
break;
case 2:
self.drawingView.lineColor = RGB(65, 79, 155);
break;
case 3:
self.drawingView.lineColor = RGB(110, 58, 141);
break;
case 4:
self.drawingView.lineColor = RGB(195, 26, 126);
break;
case 5:
self.drawingView.lineColor = RGB(228, 36, 40);
break;
case 6:
self.drawingView.lineColor = RGB(234, 98, 36);
break;
case 7:
self.drawingView.lineColor = RGB(241, 141, 33);
break;
case 8:
self.drawingView.lineColor = RGB(252, 199, 19);
break;
case 9:
self.drawingView.lineColor = RGB(255, 240, 79);
break;
case 10:
self.drawingView.lineColor = RGB(139, 188, 63);
break;
case 11:
self.drawingView.lineColor = RGB(0, 144, 92);
break;
}
}
- (IBAction)toolChange:(id)sender
{
UIButton *toolButton = (UIButton*)sender;
switch (toolButton.tag){
case 0:
self.drawingView.drawTool = ToolTypePen;
self.drawingView.lineWidth = 7;
self.drawingView.lineAlpha = 1.0;
self.potlood02.hidden=NO;
self.kwast02.hidden=YES;
self.gum02.hidden=YES;
break;
case 1:
self.drawingView.drawTool = ToolTypePen;
self.drawingView.lineWidth = 20;
self.drawingView.lineAlpha = 0.67;
self.potlood02.hidden=YES;
self.kwast02.hidden=NO;
self.gum02.hidden=YES;
break;
case 6:
self.drawingView.drawTool = ToolTypeEraser;
self.drawingView.lineWidth = 20;
self.potlood02.hidden=YES;
self.kwast02.hidden=YES;
self.gum02.hidden=NO;
break;
}
}

To change the colour of your pencil image you could do something like this
UIImage *originalImage = [UIImage imageNamed:#"PencilImage.png"];
UIImage *newImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)]; // your pencil image size
imageView.tintColor = [UIColor redColor]; // or whatever color that has been selected
imageView.image = newImage;
Hope this helps.

Related

iPad Size and Coordinates scale to iPhone

I'm working on this objective c game app where the screen is populated with pictures of animals.
The problem is that the size and x/y of the animal is hard coded in the app and works with the iPad Air screen dimensions. My question is how can I scale it to show up in the same position and size on an iPhone 7 plus? Is there a formula to scale the size and x/y coordinates?
I looked for answers on stackoverflow with an answer saying to divide the dimensions but when i tried that the x/y coordinates were off.
Here is the code
-(void)viewDidLoad{
[super viewDidLoad];
NSString *sonidoPathBell1 = [[NSBundle mainBundle] pathForResource:#"bellTransition" ofType:#"aif"];
NSURL *sonidoUrlBell1 = [[NSURL alloc] initFileURLWithPath:sonidoPathBell1];
AudioServicesCreateSystemSoundID((CFURLRef)sonidoUrlBell1, &sonTrans);
btnAnimales = [[NSMutableArray alloc] initWithCapacity:0];
for(int i = 0; i < 20; i++){
OBShapedButton *tempbtn = [[OBShapedButton alloc] init];
[tempbtn setTag:i+1];
opcion = [[NSUserDefaults standardUserDefaults] integerForKey:[NSString stringWithFormat:#"bluebaymobAmazingMixAndMatchObjeto%i",i + 1]];
if (opcion == 0) {
[tempbtn setImage:[UIImage imageNamed:[NSString stringWithFormat:#"btnAni%i.png",i+1]] forState:UIControlStateNormal];
}else{
[tempbtn setImage:[UIImage imageNamed:[NSString stringWithFormat:#"btnAni%iColor.png",i+1]] forState:UIControlStateNormal];
}
[tempbtn addTarget:self action:#selector(startPuzzle:) forControlEvents:UIControlEventTouchUpInside];
CGRect cuadro = CGRectMake(0, 0, 0, 0);
switch(i+1){
case 1:
cuadro = CGRectMake(131, 85, 95, 120);
//cuadro = CGRectMake(94, 48, 65, 60);
//37, 37, 30, 60
break;
case 2:
cuadro = CGRectMake(52, 246, 147, 157);
//cuadro = CGRectMake(15, 209, 117, 97);
break;
case 3:
cuadro = CGRectMake(-5, 418, 166, 158);
break;
case 4:
cuadro = CGRectMake(301, 64, 156, 151);
break;
case 5:
cuadro = CGRectMake(214, 213, 129, 190);
break;
case 6:
cuadro = CGRectMake(165, 384, 162, 169);
break;
case 7:
cuadro = CGRectMake(204, 615, 139, 153);
break;
case 8:
cuadro = CGRectMake(458, 115, 109, 90);
break;
case 9:
cuadro = CGRectMake(367, 222, 207, 130);
break;
case 10:
cuadro = CGRectMake(361, 344, 408, 276);
break;
case 11:
cuadro = CGRectMake(387, 591, 234, 173);
break;
case 12:
cuadro = CGRectMake(605, 104, 92, 131);
break;
case 13:
cuadro = CGRectMake(671, 220, 152, 155);
break;
case 14:
cuadro = CGRectMake(760, 450, 137, 174);
break;
case 15:
cuadro = CGRectMake(701, 613, 101, 161);
break;
case 16:
cuadro = CGRectMake(660, 26, 132, 88);
break;
case 17:
cuadro = CGRectMake(828, 36, 112, 107);
break;
case 18:
cuadro = CGRectMake(820, 288, 153, 157);
break;
case 19:
cuadro = CGRectMake(909, 441, 115, 162);
break;
case 20:
cuadro = CGRectMake(862, 629, 151, 135);
break;
}
//1920 x1080 iphone 7plus
//2048 x 1536 ipad
//.9375
//cuadro.origin.x = cuadro.origin.x * .9375;
//cuadro.origin.y = cuadro.origin.y * .703125;
//cuadro.origin.x = cuadro.origin.x - 37;
//cuadro.origin.y = cuadro.origin.y - 37;
//cuadro.size.width = cuadro.size.width - 30;
//cuadro.size.height = cuadro.size.height - 60;
tempbtn.frame = cuadro;
[btnAnimales addObject:tempbtn];
[self.view addSubview:[btnAnimales objectAtIndex:i]];
tempbtn = nil;
}
}

Ios image collision with one imageview

I am making an app that has 2 UIImageViews but in each image view the image changes 3 times. How can I make it so that if image A collides with image B or C action happens but not with image A and so on. Here is the code for the image changing
ImageView Changing 1
This is a random change
Trap.center = CGPointMake(350,220);
Trap1 = rand() %3;
switch (Trap1) {
case 0:
Trap.image = [UIImage imageNamed:#"Image1.png"];
break;
case 1:
Trap.image = [UIImage imageNamed:#"Image2.png"];
break;
case 2:
Trap.image = [UIImage imageNamed:#"Image3.png"];
break;
default:
break;
}
ImageView Changing 2
This is set with a swipe and is copied for the other 2 changes.
-(IBAction)Change3:(id)sender{
UIImage *img = [UIImage imageNamed:#"Image4.tif"];
[Change3 setImage:img];
}
You can add a tag to your Trap object. Compare the tags and if they are different, do something.
Set tag:
switch (Trap1) {
case 0:
Trap.image = [UIImage imageNamed:#"Image1.png"];
Trap.tag = 1;
break;
case 1:
Trap.image = [UIImage imageNamed:#"Image2.png"];
Trap.tag = 2;
break;
case 2:
Trap.image = [UIImage imageNamed:#"Image3.png"];
Trap.tag = 3;
break;
default:
break;
}
Compare tags
If (trap1.tag != trap2.tag){
//Do something
}

Fixed numbers in a switch statement (Random)

I'm new to Xcode and i'm really stuck.
I have a UIProgressView which fills up with a UISwipeGestureRecognizer. I also have a switch statement. Now I want that only 7 ways to run (from the 242) while progressTrue is false. If progressTrue is true, so the UIProgressView is on 1.0, it should select new 7 cases randomly. Is something like that possible?
Thanks for any help!
-(void)vorgangDev {
int number = arc4random() % 242;
switch (number) {
case 0:
question.text = #"Anlieferung/anliefern";
answer.text = #"delivery, supply/deliver, supply";
answer.hidden = YES;
break;
case 1:
question.text = #"Artikel";
answer.text = #"article, item";
answer.hidden = YES;
break;
case 2:
question.text = #"Artikelanzahl";
answer.text = #"number of articles/ ~items";
answer.hidden = YES;
break;
//And so on...
}
Here I call vorgangDev.
if (progressTrue.progress == 1.0) {
// The UIProgressView is full
// if this is true do the question.
CABasicAnimation* fade = [CABasicAnimation animationWithKeyPath:#"backgroundColor"];
fade.fromValue = (id)[UIColor blackColor].CGColor;
fade.toValue = (id)[UIColor colorWithRed:50/255.0f green:109/255.0f blue:43/255.0f alpha:1.0f].CGColor;
[fade setDuration:1];
[self.view.layer addAnimation:fade forKey:#"fadeAnimation"];
progressTrue.progress = 0.0;
[self vorgangDev];

Changing Background color of Picker View

How can i change the Color of my Picker view depending on the background color of the TextField? e.g i have 2 text fields, The one is red and the other one is blue, If i clicked the red one the picker background is red also, And if i click the blue one the picker background will be blue also? im using this code for the text field to show the picker view.
int tag = [(UITextField*)sender tag];
self.myPicker.hidden = NO;
selectedTable = tag;
[sender resignFirstResponder];
float yy = 10;
switch (tag) {
case 0: yy = self.txtTgfeet.frame.origin.y + self.myPicker.frame.size.height;
break;
case 1: yy = self.txtTginches.frame.origin.y + self.myPicker.frame.size.height;
break;
case 2: yy = self.txtTgfraction.frame.origin.y + self.myPicker.frame.size.height;
break;
case 3: yy = self.txtBgfeet.frame.origin.y - self.myPicker.frame.size.height;
break;
case 4: yy = self.txtBginches.frame.origin.y - self.myPicker.frame.size.height;
break;
case 5: yy = self.txtBgfraction.frame.origin.y - self.myPicker.frame.size.height;
break;
case 6: yy = self.txtGravity.frame.origin.y - self.myPicker.frame.size.height;
break;
case 7: yy = self.txtBsw.frame.origin.y - self.myPicker.frame.size.height;
break;
case 8: yy = self.txtTemp.frame.origin.y - self.myPicker.frame.size.height;
break;
default:
break;
}
thank you!
use self.picker.backgroundColor = [UIColor redColor]; in textFieldDidBeginEditing: UITextFieldDelegate method and change color using by using tag property.
if(textField.tag == 1){
self.picker.backgroundColor = [UIColor redColor];
}
else if(textField.tag == 2){
self.picker.backgroundColor = [UIColor redColor];
}
else{
//use default value
}
Edit:
switch (tag) {
case 0: yy = self.txtTgfeet.frame.origin.y + self.myPicker.frame.size.height;
self.picker.backgroundColor = [UIColor redColor];
break;
case 1: yy = self.txtTginches.frame.origin.y + self.myPicker.frame.size.height;
self.picker.backgroundColor = [UIColor bluecolor];
break;
case 2: yy = self.txtTgfraction.frame.origin.y + self.myPicker.frame.size.height;
self.picker.backgroundColor = [UIColor greenColor];
break;
case 3: yy = self.txtBgfeet.frame.origin.y - self.myPicker.frame.size.height;
break;
case 4: yy = self.txtBginches.frame.origin.y - self.myPicker.frame.size.height;
break;
case 5: yy = self.txtBgfraction.frame.origin.y - self.myPicker.frame.size.height;
break;
case 6: yy = self.txtGravity.frame.origin.y - self.myPicker.frame.size.height;
break;
case 7: yy = self.txtBsw.frame.origin.y - self.myPicker.frame.size.height;
break;
case 8: yy = self.txtTemp.frame.origin.y - self.myPicker.frame.size.height;
break;
default:
break;
}
add different background colour for each case

Statement requires expression of integer type (int invalid) error with switch statement

I am trying to load an image and label corresponding to the table view row/cell selected by the user. The code appears in my DetailViewController implementation file as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
//Create a couple of UIImages
UIImage *jenImage = [UIImage imageNamed:#"102_2262.jpg"];
UIImage *jen2Image = [UIImage imageNamed:#"102_2288.jpg"];
UIImage *paigeImage = [UIImage imageNamed:#"102_2332.jpg"];
UIImage *parkerImage = [UIImage imageNamed:#"102_2379.jpg"];
//Switch UIImages and UILabel based on characterNumber
switch (characterNumber) {
case 0:
characterName.text = #"Casual Jen";
characterPic.image = jenImage;
self.title = #"Casual Jen";
break;
case 1:
characterName.text = #"Dressed Up Jen";
characterPic.image = jen2Image;
self.title = #"Dressed Up Jen";
break;
case 2:
characterName.text = #"Paige";
characterPic.image = paigeImage;
self.title = #"Paige";
break;
case 3:
characterName.text = #"Parker";
characterPic.image = parkerImage;
self.title = #"Parker";
break;
default:
characterName.text = #"Parker";
characterPic.image = parkerImage;
self.title = #"Parker";
break;
}
}
The switch statement produces the error as described. I have searched for the solution here and elsewhere without success. Any help is greatly appreciated!
Make the data type of characterNumber as int.

Resources