Add effect for buttons - ios

I have an app like Hangman, the difference is that in my app the letters chosen are in order, not on the right space .
-(void)MatchedWordClick:(id)sender{
UIButton *bt = (UIButton*)sender;
[bt setTitle:#"" forState:UIControlStateNormal];
((UIButton*)[PuzzleView viewWithTag:[[[lstWordToMatch objectAtIndex:nCurrentWord]objectAtIndex:bt.tag-200] integerValue]+100]).hidden = FALSE;
[[lstWordToMatch objectAtIndex:nCurrentWord]replaceObjectAtIndex:bt.tag-200 withObject:[NSString stringWithFormat:#"%d",-1]];
}
-(void)PuzzleWordClick:(id)sender{
//AudioServicesPlaySystemSound(sound1);
UIButton *bt = (UIButton*)sender;
int nIndex = -1;
for (int i = 0; i< [[lst_word objectAtIndex:nCurrentWord]length]; i++) {
UIButton *bt1 = (UIButton*)[self.view viewWithTag:200+i];
if ([bt1.currentTitle isEqualToString:#""]||bt1.currentTitle == nil) {
nIndex = i;
break;
}
}
if (nIndex == -1) {
NSLog(#"You Macthed Already");
}else{
bt.hidden = TRUE;
[[lstWordToMatch objectAtIndex:nCurrentWord]replaceObjectAtIndex:nIndex withObject:[NSString stringWithFormat:#"%d",bt.tag-100]];
[((UIButton*)[self.view viewWithTag:200+nIndex])setTitle:bt.titleLabel.text forState:UIControlStateNormal];
}
int nCount = 0;
for (int i = 0; i< [[lst_word objectAtIndex:nCurrentWord]length]; i++) {
UIButton *bt1 = (UIButton*)[self.view viewWithTag:200+i];
if ([bt1.currentTitle isEqualToString:#""]||bt1.currentTitle == nil) {
nCount++;
}
}
if (nCount == 0) {
NSString *strWord = #"";
for (int i = 0; i<[[lst_word objectAtIndex:nCurrentWord]length]; i++) {
UIButton *bt1 = (UIButton*)[self.view viewWithTag:200+i];
strWord = [NSString stringWithFormat:#"%#%#",strWord,bt1.currentTitle];
}
if (![strWord isEqualToString:[lst_word objectAtIndex:nCurrentWord]]) {
MatchView.userInteractionEnabled = FALSE;
PuzzleView.userInteractionEnabled = FALSE;
ErrorAlertView.hidden = FALSE;
// AudioServicesPlaySystemSound(sound3);
}else{
if (nCurrentWord == 0) {
//// Change Points Value
if ([[lst_word objectAtIndex:nCurrentWord] length] == 3) {
[lblPts setText:[NSString stringWithFormat:#"%d", nPoint+5]];//point value
}else if ([[lst_word objectAtIndex:nCurrentWord] length] == 4) {
[lblPts setText:[NSString stringWithFormat:#"%d", nPoint+10]];
}else if ([[lst_word objectAtIndex:nCurrentWord] length] >= 5) {
[lblPts setText:[NSString stringWithFormat:#"%d", nPoint+15]];
}
[self drawSuccessView];
}else{
nCurrentWord++;
[self drawPuzzle:nCurrentWord];
}
}
}
}
This code is working to select a letter and complete the word, it also works if I want to erase the letter and put it back .
I want that when I select a letter (from puzzle or from word) to add an effect like fly when a letter is put back or in the word.
I want something like this effect but I don't know how to do it in this case.
int z=40 + random() % 9;
int w=40+ random() % 9;
[UIView animateWithDuration:0.5 animations:^{
self.label13.frame = CGRectMake(-z,-200, 25, 25);
NSLog(#"aaa");
}];

Related

How to remove data from an array comparing using string not an index in objective-C

I have an array of dictionary like below
self->arrField: (
{
fieldname = "Have you ever had any one of the following:";
fieldvalue = (
Diabetes,
Hypertension
);
tagvalue = 0;
},
{
fieldname = "By continuing with this survey, you agree to our terms of use and Parsons Information Governance and Privacy Policies, Procedures and Standards, available on the Corporate Policy Center.";
fieldvalue = Agree;
tagvalue = 1;
},
{
fieldname = "Have you tested positive for COVID-19 in the last 10 days?";
fieldvalue = No;
tagvalue = 4;
},
{
fieldname = "Do you have any known heart conditions or on any medications that elevate your heart rate";
fieldvalue = Yes;
tagvalue = 6;
},
{
fieldname = "Have you received a COVID-19 test in the past 48 hours";
fieldvalue = Yes;
tagvalue = 7;
},
{
fieldname = "In the last 48 hours have you experienced any of these symptoms";
fieldvalue = (
cough,
fever
);
tagvalue = 10;
}
)
So if the user changes any of the fieldvalue I need to replace that array, I have tried using replaceObjectAtIndex but it will not work out because I need compare the string.
Below is my implementation:
-(void)ChkUnChk:(UIButton*)sender {
dispatch_async(dispatch_get_main_queue(), ^{
UIButton *btn=(UIButton *)sender;
NSString *strValue = btn.accessibilityHint;
NSString *Str=[NSString stringWithFormat:#"%d",(int)btn.tag];
int iTag = [[sender.layer valueForKey:#"anyKey"] intValue];
// new Implementation
NSMutableDictionary *dictFields = [[NSMutableDictionary alloc] init];
NSMutableArray *arrFieldvalue = [[NSMutableArray alloc] init];
[dictFields setObject:[[self->dictCustomFieldGroups valueForKey:#"name"] objectAtIndex:iTag] forKey:#"fieldname"];
[dictFields setObject:[NSString stringWithFormat:#"%ld",(long)iTag] forKey:#"tagvalue"];
if ([self isTagIDAvailable:iTag]) {
arrFieldvalue = [[[self->arrTempCheckboxFields objectAtIndex:self->checkboxIndex] valueForKey:#"fieldvalue"] mutableCopy];
BOOL isTheObjectThere = [arrFieldvalue containsObject: strValue];
if (isTheObjectThere) {
NSLog(#"string contains");
[btn setBackgroundImage:[UIImage systemImageNamed:#"square"] forState:UIControlStateNormal];
for(id item in arrFieldvalue) {
if([item isEqual:strValue]) {
[arrFieldvalue removeObject:item];
[dictFields setObject:arrFieldvalue forKey:#"fieldvalue"];
break;
}
}
} else {
NSLog(#"string does not contain!");
[btn setBackgroundImage:[UIImage systemImageNamed:#"checkmark.square"] forState:UIControlStateNormal];
[arrFieldvalue addObject:strValue];
[dictFields setObject:arrFieldvalue forKey:#"fieldvalue"];
}
NSInteger count = [self->arrFields count];
for (NSInteger index = (count - 1); index >= 0; index--) {
NSArray *p = self->arrFields[index];
if ([[self->arrFields valueForKey:#"tagvalue"] isEqual:[NSNumber numberWithInt:iTag]]) {
[self->arrFields removeObjectAtIndex:index];
}
}
[self->arrFields replaceObjectAtIndex:iTag withObject:dictFields];
[self->arrTempCheckboxFields replaceObjectAtIndex:self->checkboxIndex withObject:dictFields];
} else {
[btn setBackgroundImage:[UIImage systemImageNamed:#"checkmark.square"] forState:UIControlStateNormal];
[arrFieldvalue addObject:strValue];
[dictFields setObject:arrFieldvalue forKey:#"fieldvalue"];
[self->arrFields addObject:dictFields];
[self->arrTempCheckboxFields addObject:dictFields];
}
NSLog(#"self->arrField: %#",self->arrFields);
});
}
-(BOOL) isTagIDAvailable:(int)tagID {
BOOL retVal = FALSE;
BOOL tagIdFound = FALSE;
if ([arrTagID count] > 0) {
for (int i = 0; i < [arrTagID count]; i++) {
if ([[NSNumber numberWithInteger:tagID] isEqualToNumber:[arrTagID objectAtIndex:i]]) {
tagIdFound = TRUE;
checkboxIndex = i;
retVal = TRUE;
break;
}
}
if (!tagIdFound) {
[arrTagID addObject:[NSNumber numberWithInteger:tagID]];
checkboxIndex = (int)[arrTagID count];
retVal = FALSE;
}
} else {
[arrTagID addObject:[NSNumber numberWithInteger:tagID]];
retVal = FALSE;
}
return retVal;
}
I have done something from line no 32, like I will remove that array value and add it again.But I am not sure how to do that. If more detail require I can provide.
Thanks in Advance.

Addition of two random number , with four options on swipe viewcontroller is not work perfectly

I am developing application of addition of two random number with their four options , On Swipe of View-controller the two numbers and their options will change, for change options I use array to marge that number & print it again. But on swipe of view it will not work perfectly, smooth swipe is not happen, here is my code please help me.
-(void)makeAddition
{
number1 = (arc4random()%100)+1; //Generates Number from 1 to 100.
number2 = (arc4random()%100)+1;
addition = number1+number2;
wrongOne = (arc4random()%100)+1;
wrongTwo = (arc4random()%100)+1;
wrongThree = (arc4random()%100)+1;
NSString *w1 = [NSString stringWithFormat:#"%d",wrongOne];
NSString *w2 = [NSString stringWithFormat:#"%d",wrongTwo];
NSString *w3 = [NSString stringWithFormat:#"%d",wrongThree];
answer = [NSString stringWithFormat:#"%d",addition];
// add randam values in array
[_AnswerArray addObject:answer];
[_AnswerArray addObject:w1];
[_AnswerArray addObject:w2];
[_AnswerArray addObject:w3];
NSLog(#"%# add objects in array",_AnswerArray);
// mearge arrays object here
NSUInteger count = [_AnswerArray count];
for (NSUInteger i = 0; i < count; ++i)
{
NSUInteger nElements = count - i;
NSUInteger n = (arc4random() % nElements) + i;
[_AnswerArray exchangeObjectAtIndex:i withObjectAtIndex:n];
NSLog(#"%# exchange array objects",_AnswerArray);
} // if addition is less than 100 then print number & options
if (addition <= 100)
{
_numberOne.text = [NSString stringWithFormat:#"%d",number1];
_numberTwo.text=[NSString stringWithFormat:#"%d",number2];
// checking all four buttons value with array value
if (_AnswerOne.currentTitle != _AnswerTwo.currentTitle ||
_AnswerOne.currentTitle != _AnswerThree.currentTitle ||
_AnswerOne.currentTitle != _AnswerFour.currentTitle) {
[_AnswerOne setTitle:[_AnswerArray objectAtIndex:0] forState:UIControlStateNormal];
}
if (_AnswerTwo.currentTitle != _AnswerOne.currentTitle ||
_AnswerTwo.currentTitle != _AnswerThree.currentTitle ||
_AnswerTwo.currentTitle != _AnswerFour.currentTitle) {
[_AnswerTwo setTitle:[_AnswerArray objectAtIndex:1] forState:UIControlStateNormal];
}
if (_AnswerThree.currentTitle != _AnswerOne.currentTitle ||
_AnswerThree.currentTitle != _AnswerTwo.currentTitle ||
_AnswerThree.currentTitle != _AnswerFour.currentTitle) {
[_AnswerThree setTitle:[_AnswerArray objectAtIndex:2] forState:UIControlStateNormal];
}
if (_AnswerFour.currentTitle != _AnswerOne.currentTitle ||
_AnswerFour.currentTitle != _AnswerTwo.currentTitle ||
_AnswerFour.currentTitle != _AnswerThree.currentTitle) {
[_AnswerFour setTitle:[_AnswerArray objectAtIndex:3] forState:UIControlStateNormal];
}
}
[_AnswerArray removeAllObjects];
}
-(void)slideToLeftWithGestureRecognizer : (UISwipeGestureRecognizer *)gestureRecognizer
{
[self makeAddition];
}

how to get the uibutton state value in test.m file

In .m file i have methods like
-(void) textdata
{
long i = search.text.length;
if (i > 0) {
searchButton.enabled = YES;
}
else
{
searchButton.enabled = NO;
}
[self buttonstate];
}
-(int) buttonstate
{
if ([searchButton isEnabled]) {
j = 1;
}
else
j = 0;
NSLog(#" j value is %d",j);
return j;
}
- (void) textFieldDidChange
{
[self textdata];
NSLog(#"test data is %#",search.text);
}
And in the tests.m file i have test like
-(void) testwithoutData
{
myapiViewController *apiViw = [[myapiViewController alloc]init];
[apiViw textdata];
int kn = [apiViw buttonstate];
NSLog(#"the value is %ld",(long)kn);
}
You have to do alloc init because due to this instance variable getting nil. Try this -
In ViewController.m -
-(int) buttonstate {
long i = self.searchTxt.text.length;
if (i > 0) {
self.searchBtn.enabled = YES;
}
else
{
self.searchBtn.enabled = NO;
}
if ([self.searchBtn isEnabled]) {
j = 1;
}
else
j = 0;
NSLog(#" j value is %d",j);
return j;
}
In ViewControllerTests.m -
- (void)testExample {
// This is an example of a functional test case.
XCTAssert(YES, #"Pass");
self.vcToTest.searchTxt = [[UITextField alloc] init];
self.vcToTest.searchBtn = [[UIButton alloc] init];
self.vcToTest.searchTxt.text = #"test";
int kn = [self.vcToTest buttonstate];
NSLog(#"the value is %ld",(long)kn);
}
Check the attached screenshot output -

Randomizing position of correct answer in a quiz application for ios(Objective -C)

I am trying to develop a simple quiz application for IOS. I need to pick up 10 random images out of 50 , and display each image and 4 options with one correct answer. Presently I am able to randomize images , but each time, the correct option is displayed only at one position. Can anyone help me for randomizing the position of correct answer for different questions (i.e, images)?
- (void)viewDidAppear:(BOOL)animated
{
myData = nil;
[super viewDidAppear:animated];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"WoodTexture"]]];
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"Images" ofType:#"plist"];
myData = [[NSMutableArray alloc] initWithContentsOfFile:bundle];
[self setImageswithOptions];
count = 0;
score = 0;
[self resetScreen];
}
The method definition goes below.
- (void)setImageswithOptions
{
NSInteger number[4],i;
for ( i = 0; i <4 ; ++i ) {
number[i] = arc4random() % myData.count ;
for (int j = 0; j < i; ++j) {
if ((number[i] < 0) || (number[i] == number[j] )) {
--i;
}
}
}
self.submit.hidden =YES;
self.displayImage.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",myData[number[0]]]];
NSInteger random = arc4random() % 4;
if (random == 0) {
random += 1;
}
[(UILabel *)[self.view viewWithTag:random] setText:[NSString stringWithFormat:#"%#",myData[number[0]]]];
[(WQZOptionButton *)[self.view viewWithTag:random + 4] setIsCorrect:YES];
NSInteger imageCount = 0;
for (NSInteger index = 1; index <= 4; index++) {
if (index != random) {
imageCount++;
[(UILabel *)[self.view viewWithTag:index] setText:[NSString stringWithFormat:#"%#",myData[number[imageCount]]]];
[(WQZOptionButton *)[self.view viewWithTag:index + 4] setIsCorrect:NO];
}
}
[myData removeObjectAtIndex:number[0]];
self.scoreLabel.text = [NSString stringWithFormat:#"Score: %ld", (long)score];
}
There are several problems.
One problem:
NSInteger random = arc4random() % 4;
if (random == 0) {
random += 1;
}
random will be 1, 2 or 3 and 1 will occur twice as often as 2 or 3.
Also it is better to use arc4random_uniform() than the modular operator:
Instead of arc4random() % 4
use arc4random_uniform(4)

iOS: Setting variables on several custom view objects

Okay, so I'm creating X number of a custom UIView, that I've created in IB...
I create them in a grid-like formation and need to set their individual properties based on a response from a web service call...
The part I'm having trouble with is how to iterate through the different UIViews and set the variables...
I'm pretty sure the solution is really simple, but I've been staring blindly at this for some time now...
It's the part after:
if([theStatus.groupName isEqualToString:groupEntry.groupNameLabel.text])
{
Here is the entire method:
- (void)receivedGroups
{
int rows, columns;
if([groupConnection.groupsArray count] <= 4)
{
rows = 1;
columns = [groupConnection.groupsArray count];
} else if([groupConnection.groupsArray count] >= 5 && [groupConnection.groupsArray count] <= 8)
{
rows = 2;
columns = ([groupConnection.groupsArray count] + 1 )/ 2;
} else
{
rows = 3;
columns = ([groupConnection.groupsArray count] + 2 )/ 3;
}
int number = 0;
for(int j=1; j < columns+1; j++)
{
for(int k=0; k < rows; k++)
{
// Only create the number of groups that match the number of entries in our array
if(number < [groupConnection.groupsArray count])
{
// Create an instance of the group view
GroupEntry *groupEntry = [[GroupEntry alloc] initWithFrame:CGRectMake(230*j, 250*k, 180, 233)];
// Add it to the view
[self.view addSubview:groupEntry];
// Get the group
GetGroupsActive *theGroups = [groupConnection.groupsArray objectAtIndex:number];
groupEntry.groupNameLabel.text = theGroups.groupName;
for(int i=0; i<[statusConnection.statusArray count]; i++)
{
CurrentStatus *theStatus = [statusConnection.statusArray objectAtIndex:i];
if([theStatus.groupName isEqualToString:groupEntry.groupNameLabel.text])
{
//allChildren++;
switch(theStatus.currentStatus)
{
case 0:
//childrenSick++;
break;
case 1:
//childrenVacation++;
break;
case 2:
//childrenPresent++;
break;
case 3:
//childrenOut++;
break;
case 4:
//childrenTour++;
break;
default:
break;
}
}
}
NSString *allLabelText = [NSString stringWithFormat:#"%i", allChildren];
NSString *sickLabelText = [NSString stringWithFormat:#"%i", childrenSick];
NSString *vacationLabelText = [NSString stringWithFormat:#"%i", childrenVacation];
NSString *presentLabelText = [NSString stringWithFormat:#"%i", childrenPresent];
NSString *outLabelText = [NSString stringWithFormat:#"%i", childrenOut];
NSString *tripLabelText = [NSString stringWithFormat:#"%i", childrenTour];
groupEntry.sickLabelNumber.text = sickLabelText;
groupEntry.presentLabelNumber.text = presentLabelText;
groupEntry.numberLabelNumber.text = allLabelText;
groupEntry.tripLabelNumber.text = tripLabelText;
groupEntry.outLabelNumber.text = outLabelText;
groupEntry.vacationLabelNumber.text = vacationLabelText;
// Create the buttons to handle button press
UIButton *childButton = [UIButton buttonWithType:UIButtonTypeCustom];
childButton.frame = CGRectMake(230*j, 250*k, 180, 233);
// Set an identity tag, so we can recognize it during button press
childButton.tag = theGroups.ID;
// When EventTouchUpInside, send an action to groupSelected:
[childButton addTarget:self action:#selector(groupSelected:) forControlEvents:UIControlEventTouchUpInside];
// Add it to the view
[self.view addSubview:childButton];
}
number++;
}
}
}
If you added all the views in a parent view. You can get all the subviews using,
NSAarry *subviews = [base subviews];
for(UIView *subview in subviews)
{
subview.property = yourVaule;
}
You can differentiate between subviews using its tag or another property.

Resources