Not let UIButton drag outside Circle - iphone

I am new to ios.
I have a view in which i had drawn a circle using Quarts core.
I had put one UIButton in that circle and given the Funcnality to DRag and drop that button.
Now i want constraint that the button cant be draged out of that circle area.
The TouchDragOutSide Event of Button is
- (void) draggedOut: (UIControl *) c withEvent: (UIEvent *) ev
{
if([viewCanvas pointInside:[[[ev allTouches] anyObject] locationInView:viewCanvas ] withEvent:ev])
c.center = [[[ev allTouches] anyObject] locationInView:viewCanvas ];
}
At this point the button cant be drag out side of rectangle View area.
Thanks for Help

try this..
if([viewCanvas pointInside:[[[ev allTouches] anyObject] locationInView:viewCanvas ] withEvent:ev])
{
UITouch *touch = [[ev touchesForView:c] anyObject];
CGPoint location = [touch locationInView:c];
if((location.x<(viewCanvas.frame.origin.x+viewCanvas.frame.size.width))&&(location.y<(viewCanvas.frame.origin.y+viewCanvas.frame.size.height)))
{
c.center = [[[ev allTouches] anyObject] locationInView:viewCanvas ];
}
}

I had make solution by the equation by using this equation.
(x-center_x)^2 + (y - center_y)^2 < radius^2

you have the center (x,y) of circle and radius. take the center of the button and check whether it is inside of your x+ radius - width/2 if it moves in x direction and y+ radius - height/2 if it in a y direction. check for the 4 directions and give proper + / -

Related

drag UIView like apps in the home screen iPhone

I have a view control and inside I plan to place some controls like buttons textbox etc... I can drag my view along the x axis like:
1)
2)
with the following code:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
displaceX = location.x - ViewMain.center.x;
displaceY = ViewMain.center.y;
startPosX = location.x - displaceX;
}
CurrentTime = [[NSDate date] timeIntervalSince1970];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
location.x =location.x - displaceX;
location.y = displaceY;
ViewMain.center = location;
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
double time = [[NSDate date] timeIntervalSince1970]-CurrentTime;
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
location.x =location.x - displaceX;
location.y = displaceY;
ViewMain.center = location;
double speed = (ViewMain.center.x-startPosX)/(time*2);
NSLog(#"speed: %f", speed);
}
}
not that I have to add the global variables:
float displaceX = 0;
float displaceY = 0;
float startPosX = 0;
float startPosY = 0;
double CurrentTime;
the reason why I created those variables is so that when I start dragging the view the view moves from the point where I touch it instead of from the middle.
Anyways if I touch a button or image the view will not drag even though the images have transparency on the background. I want to be able to still be able to drag the view regardless if there is an image on top of the view. I where thinking that maybe I need to place a large transparent view on top of everything but I need to have buttons, images etc. I want to be able to drag a view just like you can with:
note that I was able to drag the view regardless of wither I first touched an app/image or text. How could I do that?
I think your problem is that if you touch a UIButton or a UIImageView with interaction enabled, it doesn't pass the touch along.
For the images, uncheck the User Interaction Enabledproperty in IB.
For the buttons that are causing touchesBegan:withEvent:, etc. to not get called, then look at the following link: Is there a way to pass touches through on the iPhone?.
You may want to consider a different approach to this problem. Rather than trying to manually manage the content scrolling yourself you would probably be better off using a UIScrollView with the pagingEnabled property set to YES. This is the method Apple recommends (and it's probably the method used by Springboard.app in your last screenshot). If you are a member of the iOS developer program check out the WWDC 2010 session on UIScrollView for an example of this. I think they may have also posted sample code on developer.apple.com.

Multiple objects in touchesbegan?

I am trying to have multiple objects in a touchesbegan method (2 UIImageViews)
I am using the below code but isn't working. No errors but the location is just messed up. What should I do instead?
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
if (image1.image == [UIImage imageNamed:#"ball.png"]){
CGPoint location = [touch locationInView:touch.view];
image1.center = location;
}
if (image1.image == [UIImage imageNamed:#"ball2.png"]){
CGPoint location = [touch locationInView:touch.view];
image2.center = location;
}
}
If you want to identify both image view in touchesbegen try this
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event //here enable the touch
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(image1.frame, touchLocation))
{
NSLog(#"image1 touched");
//Your logic
}
if (CGRectContainsPoint(image2.frame, touchLocation))
{
NSLog(#"image2 touched");
//your logic
}
}
Hope this help
I guess, image1.image in your second if condition needs to image2.image if you need to over lap image1 and image2 center. But if you need to move the images you have to do the follow -
Check whether the touch point belongs to both the objects.
If yes, move both the images relatively.( i.e., add the moved amount to the earlier image center ). Not making the touch point as the image center location.
Ex: If image1 center is at (x1, y1) and image2 center is at (x2, y2). Now the touch point (x3, y3) belongs both to image image1 and image2. If the new dragged is at (x4,y4), the drag amount is x4-x3 and y4-y3 along x,y directions respectively. Add this drag amount to both the image's center for the image to appear at the new location.
Pseudo Code
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
float touchXBeginPoint = [touch locationInView:touch.view].x;
float touchYBeginPoint = [touch locationInView:touch.view].y;
// Now Check touchXBeginPoint, touchYBeginPoint lies in image1, image2
// Calculate the offset distance.
if( /* both are true */ )
{
// Add the offset amount to the image1, image2 center.
}
else if( /* image1 touch is true */ )
{
// Add the offset amount to the image1 center
}
else if( /* image2 touch is true */ )
{
// Add the offset amount to the image2 center
}
}
Download the source code of iPhone Game Dev chapter 3 and see how images are being moved.

Double Tap for certain part of view

I am bit stacked with a functionality that should perform a certain task after double taping on a certain place on UIView. I know how to count number of taps, but do not know how to determinate which place has been tapped and I guess to compare with CGRect of view which was specified for doing this action.
thanx in advance
We can detect with touchesBegan
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger numTaps = [[touches anyObject] tapCount];
UITouch* t;
if([[event allTouches] count]==2)//double tap
{
t=[[[event allTouches] allObjects] objectAtIndex:0];
CGPoint p1=[t locationInView:self.view];
}
numTaps gives the nuber of taps .
P1 has the point where it is tapped.
All the best.
use
Point point = [touch locationInView:self.view];

CGrectContainPoints & Touches queries

Im getting straight to the point. I have static coordinates stored as array and i want to compare this coordinates with user touch.
//touch handling
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:touch.view];
//comparing touches
if (CGRectContainsPoint((CGRectMake(x1, y1, w, h)) , touchPoint)) {
// do something
// this is where i got stuck coz i got 2 more sets of x & y. (x2-y2 & x3-y3)
but right now im stuck here coz i dont know how to structure my codes and i want to compare 3 save location touches to user touches so that when they hit the right spot points/score will be added but when they hit the wrong spot life will be deducted. Thanks.
If you had points stored like this . . .
CGPoint p1 = CGPointMake(100,100);
CGPoint p2 = CGPointMake(200,200);
try something like this :
// Get the location of the user's touch
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:touch.view];
float maxDistance = 10;
// Is it in the right place?
if (distanceBetween(touchPoint, p1) < maxDistance)
NSLog(#"touched point 1");
else
if (distanceBetween(touchPoint, p2) < maxDistance)
NSLog(#"touched point 2");
where distanceBetween is a function that looks something like (some maths)
// Distance between two CGPoints
float distanceBetween(CGPoint p1, CGPoint p2) {
float dx = p1.x-p2.x;
float dy = p1.y-p2.y;
return sqrt( dx*dx + dy*dy);
}
Hope that helps,
Sam

iPhone drag/drop

Trying to get some basic drag/drop functionality happening for an iPhone application.
My current code for trying to do this is as follows:
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
self.center = location;
}
This code has the result of a the touched UIView flickering while it follows the touch around. After playing around with it a bit, I also noticed that the UIView seemed to flicker from the 0,0 position on the screen to the currently touched location.
Any ideas what I'm doing wrong?
The center property needs to be specified in the coordinate system of the superview, but you've asked the touch event for the location in terms of your subview. Instead ask for them based on the superview's coordinates, like this:
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview]; // <--- note self.superview
self.center = location;
}