Can you please suggest sample code for collision detection of two imageviews.
Thanks in advance.
You can Detect Collision between two images by making rect for those image views.
Consider my image-views being named: img_view1 and img_view2.
Image-views creation:
//For img_view1 rect
//parameters are x,y,width,height
CGRect image_rect1 = CGRectMake(img_view1.position.x,img_view1.position.y,100,100);
//For img_view2 rect
//parameters are x,y,width,height
CGRect image_rect2 = CGRectMake(img_view2.position.x,img_view2.position.y,100,100);
Collision detection:
if(CGRectIntersectsRect(image_rect1, image_rect2))
{
NSLog(#"Rect is Intersecting");
}
Nice answere #Anish, however you dont really need to create a new CGRect for the views as you can simply use their respective frame properties.
If you want to put that logic in a method it would look like this:
-(BOOL)viewsDoCollide:(UIView *)view1 :(UIView *)view2{
if(CGRectIntersectsRect(view1.frame, view2.frame))
{
return YES;
}
return NO;
}
Simply pass the two views you want to test into this method, and check the output result.
Related
Hi in one of my application i had created a circle using drawrect method on UIView object.Now my concern i want draw a highlight border around the circle for that actually i used
myView.layer.borderWidth =3.0;
myView.layer.borderColor=[UIColor colorWithRed:myView.patternRed green:myView.patternGreen blue:myView.patternBlue alpha:1.0].CGColor;
But due to this code what is happening is a border is creating around the view and it's looks a rectangle, but i want to create a border around the circle itself. So if anyone know how to implement this functionality please let me know. Thanks in advance.
Try this
myView.layer.cornerRadius = 80.0f;
It'll curve your view into a circle.
Thanks.
you have to set some radius for the corners of that view so add this line
myView.layer.cornerRadius=20;
play with the numeric value to match your requirements
I hope it helps
This Works for me:
UIView *myView =[[UIView alloc]initWithFrame:CGRectMake(0,0,100,100)];
[self createRoundUIView:myView:80];
-(void)createRoundUIView:(UIView *)inputView sizeDiameter:(float)diameterSize;
{
CGPoint saveCenter = inputView.center;
CGRect frame = CGRectMake(inputView.frame.origin.x, inputView.frame.origin.y, diameterSize, diameterSize);
roundedView.frame = frame;
roundedView.layer.cornerRadius = diameterSize / 2.0;
roundedView.center = saveCenter;
}
I'm using a piece of code I found to pass touch events through transparent parts of a UIImageView. I'm subclassing UIImageView and adding this:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
unsigned char pixel[1] = {0};
CGContextRef context = CGBitmapContextCreate(pixel,
1, 1, 8, 1, NULL,
kCGImageAlphaOnly);
UIGraphicsPushContext(context);
[self.image drawAtPoint:CGPointMake(-point.x, -point.y)];
UIGraphicsPopContext();
CGContextRelease(context);
CGFloat alpha = pixel[0]/255.0;
BOOL transparent = alpha < 0.01;
if(transparent){
return NO;
} else {
return YES;
}
}
So far its working great in my iPad version. However, in my iPhone version, I set the image frames to be half of the image's actual size (self.image.frame.size.width/2). This code is interfering with my pan gestures in an odd way, where if I touch the top left of the image I cannot access my pan gesture, but I can access it to the far bottom right, past the actual image into a transparent zone.
Removing the code returns the pan gesture to normal. However, I would still like to keep the ability to ignore touches on transparent parts. Does anyone know what part of this code is messing with my touch points or any other reason its acting like this?
Thanks
I know this question is very old, but I just had to use this on a project and my problem was that I was setting the imageView frame manually. What helped was changing the code so resizing was only done through transforms.
If the imageView is initially the same size as the image inside it, you can use this code and it will work with any transform applied to the imageView afterwards.
So in your iPhone version, you could set the frame of the imageView to the size of the image inside it and then apply a scaling transform to it so it's half the size.
i want to add a magnifier in cocos2d game. here is what i found online:
http://coffeeshopped.com/2010/03/a-simpler-magnifying-glass-loupe-view-for-the-iphone
I've changed the code a bit:(since i don't want to let the loupe follow our touch)
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:magnifier_rect])) {
// make the circle-shape outline with a nice border.
self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.layer.borderWidth = 3;
self.layer.cornerRadius = 250;
self.layer.masksToBounds = YES;
touchPoint = CGPointMake(CGRectGetMidX(magnifier_rect), CGRectGetMidY(magnifier_rect));
}
return self;
}
Then i want to add it in one of my scene init method:
loop = [[MagnifierView alloc] init];
[loop setNeedsDisplay];
loop.viewToMagnify = [CCDirector sharedDirector].openGLView;
[[CCDirector sharedDirector].openGLView.superview addSubview:loop];
But the result is: the area inside the loupe is black.
Also this loupe just magnify images with the same scale, how can i change it to magnify more near the center and less near the edge? (just like real magnifier)
Thank you !!!
Here I assume that you want to magnify the center of the screen.
You have to change dynamically size attribute to your wishes according to your app needs.
CGSize size = [[CCDirector sharedDirector] winSize];
id lens = [CCLens3D actionWithPosition:ccp(size.width/2,size.height/2) radius:240 grid:ccg(15,10) duration:0.0f];
[self runAction:lens];
Cocos2d draws using OpenGL, not CoreAnimation/Quartz. The CALayer you are drawing is empty, so you see nothing. You will either have to use OpenGL graphics code to perform the loupe effect or sample the pixels and alter them appropriately to achieve the magnification effect, as was done in the Christmann article referenced from the article you linked to. That code also relies on CoreAnimation/Quartz, so you will need to work out another way to get your hands on the image data you wish to magnify.
I need a way to create a collide effect, without the actual collision between the two images. Let me explain in more detail... If I have one fixed image and one image that is moving both images are on the same x coordinate at different positions. Basically I want the images appear to be colliding.I would want to run a check on the moving image like this...
If (front of moving image is clear)
{[move forward];}
else
{[stop];}
How would I implement this in code??? So that right before the moving image collides into the fixed image it stops, so that they appear to have collided.This check would also be running on a 1/60 NSTimer.
Any advice is welcome. Thank you.
Assuming the object is moving from the left
#define PIXELS_PER_FRAME 1
-(CGFloat) getCurrentX:(UIImage*)image
{
return image.frame.origin.x + image.frame.size.width;
}
-(void) moveImageToStableImage
{
CGFloat xTarget = stableImage.frame.origin.x;
if([self getCurrentX:movingImage] < xTarget)
{
movingImage.frame.origin.x += PIXELS_PER_FRAME;
[self performSelector:#selector(moveImageToStableImage) withObject:nil afterDelay:1.0/60];
}
}
But truth be told in this situation you would probably just be better off using an animation
In -drawRect: I want to check if the provided rect intersects with anotherRect.
I'm not good at this geometry math stuff and especially at english so what I want to say is:
If you have two sheets of paper on the desk, and one of them covers the other either completely or just a small portion, it is rectsIntersect = YES.
How to check that out for rect and anotherRect?
Use bool CGRectIntersectsRect (CGRect rect1,CGRect rect2);
Swift 3:
public func intersects(_ rect2: CGRect) -> Bool
Example: rect1.intersects(rect2)
Try to use this..
While animating imageview you can get its actual frame by this:
[[obstacle1.layer presentationLayer] frame];
So, it will easily to make collision of two imageview
if (CGRectIntersectsRect([[obstacle1.layer presentationLayer] frame], [[bgImageView.layer
presentationLayer] frame]))
{
NSLog(#"Collision 1");
}