Changing Background color of Picker View - ios

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

Related

Random SpriteKit texture switch statement not working correctly

I'm currently working in SpriteKit in Objective C, and I'm trying to set a number of random textures in a switch statement to 9 different SKSpriteNodes. Currently, this section of my code looks like this:
int randomCard = arc4random_uniform(13);
switch (randomCard)
{
case 0:
staveCard = [SKTexture textureWithImageNamed:#"Card_C.png"];
break;
case 1:
staveCard = [SKTexture textureWithImageNamed:#"Card_D.png"];
break;
case 2:
staveCard = [SKTexture textureWithImageNamed:#"Card_E.png"];
break;
case 3:
staveCard = [SKTexture textureWithImageNamed:#"Card_F.png"];
break;
case 4:
staveCard = [SKTexture textureWithImageNamed:#"Card_G.png"];
break;
case 5:
staveCard = [SKTexture textureWithImageNamed:#"Card_A.png"];
break;
case 6:
staveCard = [SKTexture textureWithImageNamed:#"Card_B.png"];
break;
case 7:
staveCard = [SKTexture textureWithImageNamed:#"Card_C2.png"];
break;
case 8:
staveCard = [SKTexture textureWithImageNamed:#"Card_D2.png"];
break;
case 9:
staveCard = [SKTexture textureWithImageNamed:#"Card_E2.png"];
break;
case 10:
staveCard = [SKTexture textureWithImageNamed:#"Card_F2.png"];
break;
case 11:
staveCard = [SKTexture textureWithImageNamed:#"Card_G2.png"];
break;
case 12:
staveCard = [SKTexture textureWithImageNamed:#"Card_A2.png"];
break;
}
faceDownStaveCard1 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard2 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard3 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard4 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard5 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard6 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard7 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard8 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard9 = [SKSpriteNode spriteNodeWithTexture:staveCard];
When I run this on the simulator, all of the sprite nodes display the same random texture. However, I want the textures to be random for each sprite node, picking a different node each time.
Something like:
pick random texture from switch statement
add to first sprite node
pick random texture from switch statement
add to second sprite node
etc.
Is anyone able to help with this?
Your program's flow now is:
Create random number
Pick 1 texture
Set that texture to every node
It should loop the first 2 steps the same amount of times as nodes present. Try something like:
for (int i = 0; i<9; i++) {
int randomCard = arc4random_uniform(13);
switch (randomCard)
{
case 0:
staveCard = [SKTexture textureWithImageNamed:#"Card_C.png"];
break;
case 1:
staveCard = [SKTexture textureWithImageNamed:#"Card_D.png"];
break;
case 2:
staveCard = [SKTexture textureWithImageNamed:#"Card_E.png"];
break;
case 3:
staveCard = [SKTexture textureWithImageNamed:#"Card_F.png"];
break;
case 4:
staveCard = [SKTexture textureWithImageNamed:#"Card_G.png"];
break;
case 5:
staveCard = [SKTexture textureWithImageNamed:#"Card_A.png"];
break;
case 6:
staveCard = [SKTexture textureWithImageNamed:#"Card_B.png"];
break;
case 7:
staveCard = [SKTexture textureWithImageNamed:#"Card_C2.png"];
break;
case 8:
staveCard = [SKTexture textureWithImageNamed:#"Card_D2.png"];
break;
case 9:
staveCard = [SKTexture textureWithImageNamed:#"Card_E2.png"];
break;
case 10:
staveCard = [SKTexture textureWithImageNamed:#"Card_F2.png"];
break;
case 11:
staveCard = [SKTexture textureWithImageNamed:#"Card_G2.png"];
break;
case 12:
staveCard = [SKTexture textureWithImageNamed:#"Card_A2.png"];
break;
}
switch (i)
{
case 0:
faceDownStaveCard1 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 1:
faceDownStaveCard2 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 2:
faceDownStaveCard3 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 3:
faceDownStaveCard4 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 4:
faceDownStaveCard5 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 5:
faceDownStaveCard6 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 6:
faceDownStaveCard7 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 7:
faceDownStaveCard8 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 8:
faceDownStaveCard9 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
}
}
This is just an idea of how the flow should be, please optimize the code. Good luck!

IOS drawing App, change buttoncolor

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.

Randomizing Square colors [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have four squares on my screen and I want each of the squares to display a different image every time for each square. I have got it to work, but as the code repeats sometimes there is a delay. I'm not sure what I'm doing wrong.
Here is my main code
#import "HomeScreenViewController.h"
#interface HomeScreenViewController ()
#end
#implementation HomeScreenViewController
-(void)startRandomSquares
{
self.greenImage = [UIImage imageNamed:#"GreenSquare.png"];
self.redImage = [UIImage imageNamed:#"RedSquare"];
self.blueImage = [UIImage imageNamed:#"BlueSquare"];
self.purpleImage = [UIImage imageNamed:#"PurpleSquare"];
self.arrayOfColors = [NSMutableArray arrayWithObjects:self.greenImage,self.redImage, self.blueImage, self.purpleImage, nil];
}
-(IBAction)Button1:(id)sender
{
self.startLabel.hidden = YES;
}
-(IBAction)Button2:(id)sender
{
}
-(IBAction)Button3:(id)sender
{
}
-(IBAction)Button4:(id)sender
{
}
-(void)randomizeAllSquares
{
[self randomizedSquare1];
[self randomizedSquare2];
[self randomizedSquare3];
[self randomizedSquare4];
}
-(void)randomizedSquare1
{
[self startRandomSquares];
randomColor = arc4random() % [self.arrayOfColors count];
square1RandomNumber = randomColor;
// NSLog(#"Square 1 : %d", square1RandomNumber);
//NSLog(#"Square 1 colors : %#", self.arrayOfColors);
switch (square1RandomNumber) {
case 0:
self.square1.image = self.arrayOfColors[0];
break;
case 1:
self.square1.image = self.arrayOfColors[1];
break;
case 2:
self.square1.image = self.arrayOfColors[2];
break;
case 3:
self.square1.image = self.arrayOfColors[3];
break;
default:
break;
}
if (square1RandomNumber == square2RandomNumber ) {
[self randomizedSquare1];
NSLog(#"Square 1-2 MATCH!!!");
}
if (square1RandomNumber == square4RandomNumber) {
[self randomizedSquare1];
NSLog(#"Square 1-4 Match!!!");
}
}
-(void)randomizedSquare2
{
[self startRandomSquares];
randomColor = arc4random() % [self.arrayOfColors count];
square2RandomNumber = randomColor;
//NSLog(#"Square 2 : %d", square2RandomNumber);
//NSLog(#"Square 2 colors : %#", self.arrayOfColors);
switch (square2RandomNumber) {
case 0:
self.square2.image = self.arrayOfColors[0];
break;
case 1:
self.square2.image = self.arrayOfColors[1];
break;
case 2:
self.square2.image = self.arrayOfColors[2];
break;
case 3:
self.square2.image = self.arrayOfColors[3];
break;
default:
break;
}
if (square2RandomNumber == square1RandomNumber ) {
[self randomizedSquare2];
NSLog(#"Square 2-1 Match!!!");
}
if (square2RandomNumber == square4RandomNumber) {
[self randomizedSquare2];
NSLog(#"Square 2-4 Match!!!");
}
}
-(void)randomizedSquare3
{
[self startRandomSquares];
randomColor = arc4random() % [self.arrayOfColors count];
square3RandomNumber = randomColor;
//NSLog(#"Square 3 : %d", square3RandomNumber);
//NSLog(#"Square 3 colors : %#", self.arrayOfColors);
switch (square3RandomNumber) {
case 0:
self.square3.image = self.arrayOfColors[0];
break;
case 1:
self.square3.image = self.arrayOfColors[1];
break;
case 2:
self.square3.image = self.arrayOfColors[2];
break;
case 3:
self.square3.image = self.arrayOfColors[3];
break;
default:
break;
}
if (square3RandomNumber == square2RandomNumber ){
[self randomizedSquare3];
NSLog(#"Square 3 - 2 MATCH!!!");
}
if (square3RandomNumber == square1RandomNumber) {
[self randomizedSquare3];
NSLog(#"Square 3- 1 MATCH!!");
}
}
-(void)randomizedSquare4
{
[self startRandomSquares];
randomColor = arc4random() % [self.arrayOfColors count];
square4RandomNumber = randomColor;
//NSLog(#"Square 4 : %d", square4RandomNumber);
//NSLog(#"Square 4 colors : %#", self.arrayOfColors);
switch (square4RandomNumber) {
case 0:
self.square4.image = self.arrayOfColors[0];
break;
case 1:
self.square4.image = self.arrayOfColors[1];
break;
case 2:
self.square4.image = self.arrayOfColors[2];
break;
case 3:
self.square4.image = self.arrayOfColors[3];
break;
default:
break;
}
if (square4RandomNumber == square2RandomNumber ) {
[self randomizedSquare4];
NSLog(#"Square 4-2 MATCH!!");
}
if (square4RandomNumber == square1RandomNumber) {
[self randomizedSquare4];
NSLog(#"SQuare 4-1 MATCH!!");
}
if (square4RandomNumber == square3RandomNumber) {
[self randomizedSquare4];
NSLog(#"Square 4-3 Match");
}
}
Ouch, that's a lot of code duplication. In general if you find yourself writing a similar piece of code multiple times it's not just best practice to generalize it, but you'll likely end up with a better solution.
A simple shuffle function on your array along with a simple reassign would work, and will give a runtime of N (ie the loop will only go through the array once per call). What you have now will technically try to create the color for a square multiple times in the case that it finds a match which is the reason for your slowness. Square 4 has a 3/4 chance that it will get the same color as another square so it likely runs at least 3 times before stopping.
- (void)shuffleImages
{
//uses the Fisher-Yates shuffle algorithm
for (NSUInteger i = self.arrayOfColors.count - 1; i >= 1; i--)
{
int index = arc4random_uniform(i+1);
[self.arrayOfColors exchangeObjectAtIndex:index withObjectAtIndex:i];
}
}
- (void)updateImages
{
[self shuffleImages];
self.square1.image = self.arrayOfColors[0];
self.square2.image = self.arrayOfColors[1];
self.square3.image = self.arrayOfColors[2];
self.square4.image = self.arrayOfColors[3];
}
All you need to do when you want your colors to change is call the updateImages function.
The delay comes from the check at the end of each randomisation. There's a 75% chance the number generated is in one of the other 3 squares, so it will loop and loop until it generates the only number it can be.
A better solution would be to shuffle the array of images, and then assign each image to each view in order.
e.g. (pseudo-code)
shuffle array of images
view 1 = array[0]
view 2 = array[1]
...
view 4 = array[3]
Brandon is right about the insane amount of code duplication, and using the fischer-yates algorithm is definitely the right way to go, but it sounds like you also want this to repeat, so you should use an NSTimer.
#property (strong, nonatomic) NSTimer *randomizeColorTimer;
- (void)startRandomizing
{
_randomizeColorTimer = [NSTimer scheduledTimerWithTimeInterval:0.25
target:self
selector:#selector(shuffleImages)
userInfo:nil
repeats:YES];
}
- (void)stopRandomizing
{
[_randomizeColorTimer invalidate];
_randomizeColorTimer = 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];

Resources