Detect Collision with Programmatically Created ImageView - ios

In viewDidLoad I'm programmatically creating 13 blocks using the code:
for (int X = 1; X <= 13; X++) {
UIImageView *iceBlockX = [[UIImageView alloc] initWithFrame:CGRectMake((28 * X - 4),52,28,28)];
iceBlockX.image = [UIImage imageNamed:#"iceBlock.png"];
iceBlockX.tag = X;
[self.view insertSubview:iceBlockX belowSubview:_topPenguinCollisionTarget];
}
I need to detect a collision between an imageView created on the storyboard (penguin) and any one programmatically-created imageView block. If this is possible, what is the code used for the detection? I've tried the following code, but it's not working:
if (((CGRectIntersectsRect(_penguin.frame, _iceBlock1.frame)) && (_iceBlock1.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock2.frame)) &&(_iceBlock2.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock3.frame)) && (_iceBlock3.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock4.frame)) && (_iceBlock4.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock5.frame)) &&(_iceBlock5.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock6.frame)) && (_iceBlock6.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock7.frame)) && (_iceBlock7.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock8.frame)) && (_iceBlock8.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock9.frame)) && (_iceBlock9.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock10.frame)) && (_iceBlock10.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock11.frame)) && (_iceBlock11.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock12.frame)) && (_iceBlock12.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock13.frame)) &&(_iceBlock13.hidden == NO)))
{
NSLog(#"A collision was detected");
}

CGRectIntersectsRect return true if "rect1" intersects "rect2", else false.
- (BOOL)didUIImageCollided{
for (int X = 1; X <= 13; X++)
{
UIImageView *iceBlockX = (UIImageView*)[self.view viewWithTag:X];
if(CGRectIntersectsRect(_penguin.frame, iceBlockX.frame))
{
return YES;
}
}
return NO;
}

Related

How to check for <object returned empty description check> in uitextfield?

I have a uitextfield and i have got a search option using this uitextfield.When i enter the text it works well but when i clear the text i get "object returned empty description check" when i print it using po command in console. For checking this i have used the following code:
if(_nameTextField.text !=nil || _nameTextField.text.length !=0 || ![_nameTextField.text isEqual:#""]|| _nameTextField.text != NULL || ![_nameTextField.text isEqualToString:#""]){
}
But still it enters into the loop
use && instead of ||
if(_nameTextField.text !=nil && _nameTextField.text.length !=0 && ![_nameTextField.text isEqual:#""]&& _nameTextField.text != NULL && ![_nameTextField.text isEqualToString:#""]){
}
Please use this..it help you:
if (_nameTextField.text && _nameTextField.text.length!=0)
{
}
Use like this, it will work
if(_nameTextField.text !=nil && _nameTextField.text.length > 0 ){
//...
}

Find location on image use as an map

I have to find my location on image .
http://www.mallorcaresidencia.com/layout/map_230_0.png
touch on North area , tap on center,tap on south etc. find location particular area then event generate.
if (((touchLocation.x <219 && touchLocation.y>19) && (touchLocation.x <221 && touchLocation.y<97)) || ((touchLocation.x <143 && touchLocation.y<78) && (touchLocation.x >129 && touchLocation.y<58))) {
cellimgmap.image = [UIImage imageNamed:#"map_north.png"];
}else if (((touchLocation.x >218 && touchLocation.y<94) && (touchLocation.x <270 && touchLocation.y>165)) || ((touchLocation.x <298 && touchLocation.y>111) && (touchLocation.x >257 && touchLocation.y>84))) {
cellimgmap.image = [UIImage imageNamed:#"map_northeast.png"];
}else if (((touchLocation.x >131 && touchLocation.y>55) && (touchLocation.x <135 && touchLocation.y<111)) || (touchLocation.x <102 && touchLocation.y<135) || ((touchLocation.x <69 && touchLocation.y<150) && (touchLocation.x >42 && touchLocation.y<133))) {
cellimgmap.image = [UIImage imageNamed:#"map_westnorthwest.png"];
}

How to get distinct value from the list

I have a List generated from Linq to Entities query. In which, I need to get a unique records based on BibId. I have tried changing the query but no help to get the unique records based on BibId.
Query
aa.NewBibContentsModel = (from x in db.BibContents
where (x.TagNo == "245" && x.NormValue == aa.CurrentTitle) || (x.TagNo == "020" && x.NormValue == aa.CurrentISBN) || (x.TagNo == "022" && x.NormValue == aa.CurrentISBN)
select new
{
BibId = x.BibId,
Title = (from a in db.BibContents where a.BibId == x.BibId && a.TagNo == "245" orderby a.Id ascending select a.NormValue),
//Tit = (from a in db.BibContents where a.BibId == line.BibId && a.TagNo == "245" && a.Sfld == "a" select a.NormValue).FirstOrDefault(),
Author = (from a in db.BibContents where a.BibId == x.BibId && splitted.Contains(a.TagNo) && a.NormValue != null select a.TagNo).FirstOrDefault(),
ISBN = (from a in db.BibContents where a.BibId == x.BibId && a.NormValue != null && (a.TagNo == "020" || a.TagNo == "022") orderby a.Id ascending select a.NormValue)
}).AsEnumerable().Select(x => new BibContentsModel
{
BibId = x.BibId,
Title = string.Join(" ", x.Title),
Author = string.Join(" ", (from a in db.BibContents where a.BibId == x.BibId && a.TagNo == x.Author orderby a.Id select a.NormValue)),
ISBN = string.Join(" ", x.ISBN)
}).ToList();
Any help to this problem will be appreciated.
Thanks
What you're trying to achieve is know as Distinct By. MoreLinq has a function for it. The syntax would look like:
(from x in db.BibContentsNo == "022")
... // your query
}).AsEnumerable()
.DistinctBy(x => x.BibId) // <= MoreLinq
What is does is group the records by BibId and take the first element of each group.
You can download MoreLinq as a NuGet package.

Convert a list of selected days to its hyponym

I have list of days (from monday till sunday).
When a user selects monday till friday or saturday and sunday I want my app to show it's hyponym (weekdays/weekends).
Does iOS provide some default functionality for this (If i'm not mistaken the iOS Alarm app does the same) or do I have to write this functionality myself?
Provided that you have a NSArray with the selected days as numbers, named selectedDays, and that 0 and 6 are weekend days:
NSString *name = nil; // Fill this with the default values (concatenate all names of selected days)
bool days[] = {0,0,0,0,0,0,0};
for (NSNumber *day in selectedDays)
{
int d = [day intValue];
if (d >= 0 && d <= 6) days[d] = true;
}
if ( days[0] == true
&& days[1] == false
&& days[2] == false
&& days[3] == false
&& days[4] == false
&& days[5] == false
&& days[6] == true)
{
name = #"Weekend";
}
if ( days[0] == false
&& days[1] == true
&& days[2] == true
&& days[3] == true
&& days[4] == true
&& days[5] == true
&& days[6] == false)
{
name = #"Weekdays";
}

asp.net mvc datettime linq check for empty

var postsidebar = from post in postrepository.GetAllPosts()
join pstmt in postrepository.GetAllPostMetas()
on post.int_PostId equals pstmt.int_PostId
where (post.int_PostTypeId == 4
&& post.int_PostStatusId == 2
&& post.int_OrganizationId == layoutrep.GetSidebarDetailById(SidebarDetailsId).int_OrganizationId)
&& (pstmt.vcr_MetaKey.Contains(filter) && pstmt.vcr_MetaValue.Contains("true")
&& (System.DateTime.Now >=
Convert.ToDateTime(pstmt.Post.PostMetas.FirstOrDefault(m =>
m.vcr_MetaKey == "Publish Date").vcr_MetaValue)))
select post;
how can i check for empty in this part in Date(it is giving error)
&& (System.DateTime.Now >= Convert.ToDateTime(pstmt.Post.PostMetas.FirstOrDefault(m =>
m.vcr_MetaKey == "Publish Date").vcr_MetaValue)))
You could try eliminated the possibility of an empty value first and then try your cast afterward.
&& pstmt.Post.PostMetas.FirstOrDefault(m =>
m.vcr_MetaKey == "Publish Date"
&& !string.IsNullOrEmpty(m.vcr_MetaValue))
&& (System.DateTime.Now >=
Convert.ToDateTime(pstmt.Post.PostMetas.FirstOrDefault(m =>
m.vcr_MetaKey == "Publish Date").vcr_MetaValue)))
Try this:
// declare the action for re-use
Func<PostMeta,bool> action = m => m.vcr_MetaKey == "Publish Date";
// then test for Any() before comparing anything
&& (pstmt.Post.PostMetas.Any(action) && System.DateTime.Now >= Convert.ToDateTime(pstmt.Post.PostMetas.First(action).vcr_MetaValue)))

Resources