how to make margin - iphone

in my app i want to move objects (from touch using the code below) inside the red rectangle how to make this
code :
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:[touch view]];
if (insidethepaddle) {
object1.center = CGPointMake(location.x, location.y - 50);
}

Just use CGRectContainsPoint(CGRect rect, CGPoint point) or NSPointInRect(NSPoint aPoint, NSRect aRect) to test if the center is still inside, otherwise just don't update the objects center.
The specified CGRect/NSRect represents the frame of the red rectangle.

Related

how to find out labels which are detected using touch or gesture in ios.?

I am developing a college project on game in iPhone which is based on finding words in grid view of labels..
I have to find out labels(actually they are alphabets) which are detected while draging finger.
can anyone help me to know what controls that i can use to find appropriate labels detected while dragging..
Thanks in Advance..
You can get your label using this.
NOTE You have to set userinteractionEnabled = YES for all your UILabel
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([touch.view isKindOfClass:[UILabel class]])
{
UILabel *label=(UILabel*)touch.view;
NSLog(#"%#",label.text);
}
}
//To know exact Label from grid of Label use Tag property of tag
//To drag selected Label
//use this code
UITouch *touch = [[event touchesForView:Label] anyObject];
// get delta
CGPoint previousLocation = [touch previousLocationInView:Label];
CGPoint location = [touch locationInView:Label];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;
// move button
Label.center = CGPointMake(Label.center.x + delta_x,
Label.center.y + delta_y);
//Alphabet on label
NSString *alphabet = Label.text;

Restrict position of imageView within other imageview in iphone

I have 2 image views namely mainImageView & smallImageView. My smallImageView is added as subview to mainImageView. My smallImageView is draggable (i.e moving around the view). I want to restrict the movement of smallImageView within mainImageView (my smallImageView should not go outside of mainImageView).My smallImageView contains circle as image. Here is my code
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
smallImageView.center = location;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x < mainImageView.frame.origin.x || location.y < mainImageView.frame.origin.y) {
[self touchesBegan:touches withEvent:event];
}
}
How can I fixed this problem. Thanks.
In both touch methods,
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(mainImageView.frame, location)) {
smallImageView.center = location;
}
The disadvantage to this approach is that the small view moves to wherever you touch even if your touch doesn't start on the small view.
If you want to avoid this, set a 'dragging' boolean state only if your touchesBegan starts in the small view, and only respond to the touchesMoved if the dragging boolean is YES. Set dragging to NO on touchesEnded.
EDIT: Main view is a circle.
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
CGFloat xdifference = mainImageView.center.x - location.x;
CGFloat ydifference = mainImageView.center.y - location.y;
CGFloat distance = sqrt(xdifference * xdifference + ydifference * ydifference);
CGFloat radius = mainImageView.frame.size.width / 2;
if(distance < radius) {
smallImageView.center = location;
}

Moving CCLayer with "ccTouchesMoved" works but it needs some tweaks I can't figure it out

my code:
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint touchLocation = [touch locationInView: [touch view]];
CGPoint prevLocation = [touch previousLocationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];
CGPoint diff = ccpSub(touchLocation,prevLocation);
[self setPosition: ccpAdd(self.position, diff)];
}
}
This code lets me move the layer with my fingers. That works fine. But now I want to let the user only move the layer within a predefined CGRect. How to do that?
For example:
CGRect rect = CGRectMake(0,0,600,320);
Now the player should be only allowed to move the layer within this rect. In that example he could only move it (on an ipod touch) to the left and right. (till 600px).
What do I need to change to achieve that?
Thank you for any help.
Have a nice day :)
Keep a variable to check.. eg..
distmoved = distmoved + touchlocation.x - prevlocation.x;
if(distmoved<600)
//move code
note that distmoved is a float..
You should make your own check. It's not hard in your case:
[self setPosition: ccpAdd(self.position, diff)];
if (self.position.x < minX)
{
//correct x position
}
if (...)
// ...
// ans so on

Setting anchor point for UIView layer

I have a UIView subclass that I want to be able to move around within it's superview. When the user touches the UIView somewhere outside self.center but within self.bounds it "jumps" because I add the new location to self.center to achieve the actual move. To avoid this behavior I'm trying to set an anchor point that lets the user grab and drag the view anywhere within it's bounds.
My problem is that when I calculate the new anchor point (as shown in the code below) nothing happens, the view doesn't change position at all. On the other hand, if I set the anchor point to a precalculated point I can move the view (but then of course it "jumps" to the precalculated point). How come this doesn't work as expected?
Thanks.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Only support single touches, anyObject retrieves only one touch
UITouch *touch = [touches anyObject];
CGPoint locationInView = [touch locationInView:self];
// New location is somewhere within the superview
CGPoint locationInSuperview = [touch locationInView:self.superview];
// Set an anchorpoint that acts as starting point for the move
// Doesn't work!
self.layer.anchorPoint = CGPointMake(locationInView.x / self.bounds.size.width, locationInView.y / self.bounds.size.height);
// Does work!
self.layer.anchorPoint = CGPointMake(0.01, 0.0181818);
// Move to new location
self.center = locationInSuperview;
}
As Kris Van Bael pointed out, your going to need to do the anchor point calculations in the touchsBegan:withEvent: method in order not to negate the movement. Additionally, since changing the layer's anchorPoint will move the view's initial position, you have to add an offset to the view's center point to avoid a 'jump' after the first touch.
You can do this by calculating (and adding to your view's center point) the offset based on the difference between the initial and final anchorPoints (multiplied by your view's width/height) or you could set the view's center to the initial touch point.
Something like this perhaps:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint locationInView = [touch locationInView:self];
CGPoint locationInSuperview = [touch locationInView:self.superview];
self.layer.anchorPoint = CGPointMake(locationInView.x / self.frame.size.width, locationInView.y / self.frame.size.height);
self.center = locationInSuperview;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint locationInSuperview = [touch locationInView:self.superview];
self.center = locationInSuperview;
}
More info on anchorPoint's from apple's docs here and a similar SO question I referenced here.
You should update the anchorpoint only on TouchBegin. If you recalculate it all the time (TouchMoved), it is logical that the subview doesn't move.

get touch coordinates (in the form of 0-480 and 0-320)

i dont get how to translate the coordinates of the iphone.
if i make a single touch i get the coordinate.
if i make a touch and keep it and release it it shows the difference of the starting and end point.
how do i get the absolute position of the touch?
thanks!
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
NSLog(#"X: %f",location.x);
NSLog(#"Y: %f",location.y);
}
I want to resize an image with touch and drag(only the height)
The position of a touch is calculated relative to a view.
If you want the position relative to the screen do:
UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
NSLog(#"Position of touch: %.3f, %.3f", pos.x, pos.y);
(dry coded, might give errors)