xcode resize and position coordinates - ios

I would like my code (ipad app) to handle resizing of an image by touching it.
I am little confused with how xcode handle points coordinates.
I wrote my code considering that the origin of a frame (frame.origin) is located at the top left (as I read it somewhere), that touches location is the coordinates of the finger touching the screen and that the positive directions are left (x) and up (y). If I am wrong somewhere there that would explain why my code isn't working properly.
Here is my code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGFloat kResizeThumbSize = 45.0f;
touchStart = [[touches anyObject] locationInView:self.view];
CGFloat xO = _ImageView.frame.origin.x;
CGFloat yO = _ImageView.frame.origin.y;
CGFloat width = _ImageView.frame.size.width;
CGFloat height = _ImageView.frame.size.height;
isResizingUD = (xO - kResizeThumbSize < touchStart.x < xO + width + kResizeThumbSize &&
(yO - kResizeThumbSize < touchStart.y < yO + kResizeThumbSize ||
yO - height - kResizeThumbSize < touchStart.y < yO - height + kResizeThumbSize) );
isResizingLR = (yO + kResizeThumbSize < touchStart.x < xO + width - kResizeThumbSize &&
(xO - kResizeThumbSize < touchStart.x < xO + kResizeThumbSize ||
xO - width - kResizeThumbSize < touchStart.x < xO - width + kResizeThumbSize));
}
//moving the image
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
CGPoint previous = [[touches anyObject] previousLocationInView:self.view];
CGFloat deltaWidth = touchPoint.x - previous.x;
CGFloat deltaHeight = touchPoint.y - previous.y;
// get the frame values so we can calculate changes below
CGFloat x = _ImageView.frame.origin.x;
CGFloat y = _ImageView.frame.origin.y;
CGFloat width = _ImageView.frame.size.width;
CGFloat height = _ImageView.frame.size.height;
if (isResizingLR) {
_ImageView.frame = CGRectMake(x, y, width+deltaWidth, height);
} else if (isResizingUD) {
_ImageView.frame = CGRectMake(x, y, width, height+deltaHeight);
} else {
// not dragging from a corner -- move the view
_ImageView.center = CGPointMake(self.view.center.x + touchPoint.x - touchStart.x,
self.view.center.y + touchPoint.y - touchStart.y);
}
}
I would expect the image to be resize in the up/down direction only if the user touch the bottom or the top of the image within kResizeThumbSize (which actually, I am not sure of the unit it is expressed in) and in the right/left direction if the user touch the left or right border of the image within kResizeThumbSize. Unfortunately, what's happen is that the image can be resize in the right/left direction only and wherever I put my finger, it will be resized.
Any help would be greatly welcome.

Fixed!
For anyone needing help for this, I post my code.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGFloat kResizeThumbSize = 10.0f;
touchStart = [[touches anyObject] locationInView:self.view];
CGFloat xO = _ImageView.frame.origin.x;
CGFloat yO = _ImageView.frame.origin.y;
CGFloat width = _ImageView.frame.size.width;
CGFloat height = _ImageView.frame.size.height;
isResizingUD = (xO - kResizeThumbSize < touchStart.x &&
touchStart.x < xO + width + kResizeThumbSize &&
yO + height - kResizeThumbSize < touchStart.y);
isResizingLR = (yO + kResizeThumbSize < touchStart.y &&
touchStart.y< yO + height - kResizeThumbSize &&
xO + width - kResizeThumbSize < touchStart.x);
isMoving = (xO+kResizeThumbSize< touchStart.x &&
touchStart.x < xO + width - kResizeThumbSize &&
yO + kResizeThumbSize < touchStart.y &&
touchStart.y< yO + height - kResizeThumbSize);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
CGPoint previous = [[touches anyObject] previousLocationInView:self.view];
CGFloat deltaWidth = touchPoint.x - previous.x;
CGFloat deltaHeight = touchPoint.y - previous.y;
// get the frame values so we can calculate changes below
CGFloat x = _ImageView.frame.origin.x;
CGFloat y = _ImageView.frame.origin.y;
CGFloat width = _ImageView.frame.size.width;
CGFloat height = _ImageView.frame.size.height;
if (isResizingUD) {
_ImageView.frame = CGRectMake(x, y, width, height+deltaHeight);
} else if (isResizingLR) {
_ImageView.frame = CGRectMake(x, y, width+deltaWidth, height);
} else if (isMoving) {
// not dragging from a corner -- move the view
_ImageView.frame = CGRectMake(x+deltaWidth, y+deltaHeight, width, height);
}
}

Related

touchesmoved screen bounds, how can I restrict the movement of the rect out of the screen bounds

I'am trying to restrict the movement of the rect by the screen bounds. The logic is simple, you tap inside the rect (doesn't metter where) and drag it. It follows your finger, and it has to be stoped when the border of the rect reached the bounds of the screen.
- (void)viewDidLoad {
[super viewDidLoad];
rect.backgroundColor = [UIColor redColor];
}
then I try to figure out where the tap is and if its inside the rect I change the color of the rect
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//NSLog(#"%s", __PRETTY_FUNCTION__);
UITouch *touch = touches.anyObject;
if (touch.view == rect) {
rect.backgroundColor = [UIColor greenColor];
isSelectedRect = YES;
CGPoint point = [touch locationInView:self.view];
CGPoint center = rect.center;
delta = CGPointMake(point.x - center.x,
point.y - center.y);
} else {
rect.backgroundColor = [UIColor redColor];
isSelectedRect = NO;
}
}
after that I move this rect
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//NSLog(#"%s", __PRETTY_FUNCTION__);
if (isSelectedRect) {
UITouch *touch = touches.anyObject;
CGPoint point = [touch locationInView:self.view];
rect.center = CGPointMake(point.x - delta.x,
point.y - delta.y);
}
}
Eventualy, I need to write one line of the code which will limit the movement the rect by bounds of the screeen. I and it would be perfect to make it with ternary operator. I would appreciate any help.
Replace your touchesMoved method with the code below. Hope it can help you ;)
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//NSLog(#"%s", __PRETTY_FUNCTION__);
if (isSelectedRect) {
UITouch *touch = touches.anyObject;
CGPoint point = [touch locationInView:self.view];
CGPoint newCenter = CGPointMake(point.x - delta.x,
point.y - delta.y);
CGFloat rectHeight = rect.frame.size.height;
CGFloat rectWidth = rect.frame.size.width;
CGFloat screenWidth = self.view.frame.size.width;
CGFloat screenHeight = self.view.frame.size.height;
if (newCenter.x + rectWidth / 2 > screenWidth) {
newCenter.x = screenWidth - rectWidth / 2;
}
if (newCenter.x - rectWidth / 2 < 0) {
newCenter.x = rectWidth / 2;
}
if (newCenter.y + rectHeight / 2 > screenHeight) {
newCenter.y = screenHeight - rectHeight / 2;
}
if (newCenter.y - rectHeight / 2 >= 0) {
newCenter.y = rectHeight / 2;
}
rect.center = newCenter;
}
}
if (CGRectContainsPoint(boundsRect, point)) {
rect.center = CGPointMake(point.x - delta.x,
point.y - delta.y);
}
Here, boundsRect will be the Rectangle of device.
You have to note that the point which you are moving in touchesMoved function is the center point of the view. In order to restrict them from going outside the viewBounds you must check whether the bounds of the draggable view is exceeding your translation point.
to get the actual bounds
CGPoint center = rect.center;
CGSize size = rect.size
CGRect originalRect = CGRectMake(center.x - size.width/2, center.y - size.height/2, size.width, size.height);
Now check whether the given rect is within the desired rect.
if (CGRectContainsRect(boundsRect, originalRect)) {
// Here change the center of the view
}
I Hope this helps.

how dragged image with a min and max value?

I have this code for increase height of a image.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
touchStart = [[touches anyObject] locationInView:self.image];
touchStart2 = [[touches anyObject] locationInView:self.tableView];
isResizingLR = ( self.image.bounds.size.height - touchStart.y < kResizeThumbSize );
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:self.image];
CGPoint previous=[[touches anyObject]previousLocationInView:self.image];
float deltaWidth = touchPoint.x-previous.x;
float deltaHeight = touchPoint.y-previous.y;
if (isResizingLR) {
self.image.frame = CGRectMake(self.image.frame.origin.x, self.image.frame.origin.y, self.image.frame.size.width, touchPoint.y + deltaWidth);
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x ,self.tableView.frame.origin.y + deltaHeight, self.tableView.frame.size.width, self.tableView.frame.size.height - deltaHeight );
self.viewTitle.frame = CGRectMake(self.viewTitle.frame.origin.x ,self.viewTitle.frame.origin.y + deltaHeight, self.viewTitle.frame.size.width, self.viewTitle.frame.size.height);
}
}
I want put a condition for a minimHeight =100 and maximHeight =300;
How can i do that?
I already try this :
if (isResizingLR) {
if (self.image.frame.size.height >100 && self.image.frame.size.height <300) {
self.image.frame = CGRectMake(self.image.frame.origin.x, self.image.frame.origin.y, self.image.frame.size.width, touchPoint.y + deltaWidth);
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x ,self.tableView.frame.origin.y + deltaHeight, self.tableView.frame.size.width, self.tableView.frame.size.height - deltaHeight );
self.viewTitle.frame = CGRectMake(self.viewTitle.frame.origin.x ,self.viewTitle.frame.origin.y + deltaHeight, self.viewTitle.frame.size.width, self.viewTitle.frame.size.height);
}
}
but every time after dragged image at: self.image.frame.size.height =100 or 300, i can't resize image. He need to be draggable all time!
Thank you!
EDIT:
I don't want resize image when heightImage is 100.
My original height of image is 200. My user can click and resize image how he want, but no over 300 and less than 100. I just put a minimum and maximum height. I hope you understand now.
Have you try to test <= and >=?
if (self.image.frame.size.height >= 100 && self.image.frame.size.height <= 300) {
//...
}
I solved the problem.
int aux=touchPoint.y + deltaWidth;
if (aux>300) {
aux=300;
}
else if(aux<100){
aux=100;
}
self.image.frame = CGRectMake(self.image.frame.origin.x, self.image.frame.origin.y, self.image.frame.size.width, aux);

Setting the Min and Max width and height for resizable UIView

I wanted to make a resizable UIView using touches from corners, and I've found this answer here and it worked well. But how can I add the ability to stop resizing when the width/height is 200 for example?
This is the code:
CGFloat kResizeThumbSize = 45.0f;
#interface MY_CLASS_NAME : UIView {
BOOL isResizingLR;
BOOL isResizingUL;
BOOL isResizingUR;
BOOL isResizingLL;
CGPoint touchStart;
}
And..
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
touchStart = [[touches anyObject] locationInView:self];
isResizingLR = (self.bounds.size.width - touchStart.x < kResizeThumbSize && self.bounds.size.height - touchStart.y < kResizeThumbSize);
isResizingUL = (touchStart.x <kResizeThumbSize && touchStart.y <kResizeThumbSize);
isResizingUR = (self.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
isResizingLL = (touchStart.x <kResizeThumbSize && self.bounds.size.height -touchStart.y <kResizeThumbSize);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self];
CGPoint previous = [[touches anyObject] previousLocationInView:self];
CGFloat deltaWidth = touchPoint.x - previous.x;
CGFloat deltaHeight = touchPoint.y - previous.y;
// get the frame values so we can calculate changes below
CGFloat x = self.frame.origin.x;
CGFloat y = self.frame.origin.y;
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
if (isResizingLR) {
self.frame = CGRectMake(x, y, touchPoint.x+deltaWidth, touchPoint.y+deltaWidth);
} else if (isResizingUL) {
self.frame = CGRectMake(x+deltaWidth, y+deltaHeight, width-deltaWidth, height-deltaHeight);
} else if (isResizingUR) {
self.frame = CGRectMake(x, y+deltaHeight, width+deltaWidth, height-deltaHeight);
} else if (isResizingLL) {
self.frame = CGRectMake(x+deltaWidth, y, width-deltaWidth, height+deltaHeight);
} else {
// not dragging from a corner -- move the view
self.center = CGPointMake(self.center.x + touchPoint.x - touchStart.x,
self.center.y + touchPoint.y - touchStart.y);
}
}
Thanks
if (isResizingLR) {
if(touchPoint.x+deltaWidth < 200 && touchPoint.y+deltaWidth < 200)
self.frame = CGRectMake(x, y, touchPoint.x+deltaWidth, touchPoint.y+deltaWidth);
} else if (isResizingUL) {
if(width-deltaWidth < 200 && height-deltaHeight < 200)
self.frame = CGRectMake(x+deltaWidth, y+deltaHeight, width-deltaWidth, height-deltaHeight);
} else if (isResizingUR) {
if(width+deltaWidth < 200 && height-deltaHeight < 200)
self.frame = CGRectMake(x, y+deltaHeight, width+deltaWidth, height-deltaHeight);
} else if (isResizingLL) {
if(width-deltaWidth < 200 && height+deltaHeight < 200)
self.frame = CGRectMake(x+deltaWidth, y, width-deltaWidth, height+deltaHeight);
}
This should work.
#property(nonatomic) CGRect frame
Description The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.
Before setting self.frame with CGRect, check for width and height!
CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);
Use width and height for actual size and delta for transformation.

How to limit a view panning just inside an other View?

I have a subview (the red bar) laid inside a view. I want to limit the panning area inside the parent view and only absolutely horizontal or vertical. And, of course not outside of the parent view.
I did try some code that handle the panning event, but sometime I can drag the red bar out. Any suggestion?
-(void)handleMoveLineView:(UIPanGestureRecognizer *)recognizer{
CGPoint sPoint= [recognizer locationInView:self.groundView];
CGPoint newCenter = [recognizer translationInView:self.groundView];
NSLog(#"X0 = %f, Y0 = %f", newCenter.x, newCenter.y);
if([recognizer state] == UIGestureRecognizerStateBegan) {
beginX = recognizer.view.center.x;
beginY = recognizer.view.center.y;
}
for (UIView *view in viewArrayGroundView) {
if (CGRectContainsPoint(view.frame, sPoint)) {
//Limit not out side parent view
if (beginX + newCenter.x != view.frame.size.width /2) {
newCenter = CGPointMake(view.frame.size.width /2, beginY + newCenter.y);
}
//Limit top - since the redbar height is 3
else if((beginY + newCenter.y < view.frame.origin.y + 3)){
newCenter.y = view.frame.origin.y + 3;
}
//Limit bottom
else if((beginY + newCenter.y > view.frame.size.height - 3)){
newCenter.y = view.frame.origin.y - 3;
}
}
}
[recognizer.view setCenter:newCenter];
NSLog(#"X = %f, Y = %f", newCenter.x, newCenter.y);
}

How to resize the UIView when CGAffineTransformIdentity

I am doing an app which has a feature to rotate and re size a view. i have implemented this feature but i do face an issue.
My problem
The View wil be resized when dragging its four corners, after resizing it i can rotate the view in both directions.
Once the rotation is done, if i try again to resize the view by dragging its corner, the view's size gone to unpredictable value and its moving all around the screen.
I googled lot finally i got the following solution
The frame property is undefined when transform != CGAffineTransformIdentity, as per the docs on UIView
I saw one app which has implemented the feature exactly what i wish to implement.
How can i resize the UIView after rotation of UIView
My code for resize the view
Touches Began
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
NSLog(#"[touch view]:::%#",[touch view]);
touchStart = [[touches anyObject] locationInView:testVw];
isResizingLR = (testVw.bounds.size.width - touchStart.x < kResizeThumbSize && testVw.bounds.size.height - touchStart.y < kResizeThumbSize);
isResizingUL = (touchStart.x <kResizeThumbSize && touchStart.y <kResizeThumbSize);
isResizingUR = (testVw.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
isResizingLL = (touchStart.x <kResizeThumbSize && testVw.bounds.size.height -touchStart.y <kResizeThumbSize);
}
Touches Moved
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:testVw];
CGPoint previous=[[touches anyObject]previousLocationInView:testVw];
float deltaWidth = touchPoint.x-previous.x;
float deltaHeight = touchPoint.y-previous.y;
NSLog(#"CVTM:%#",NSStringFromCGRect(testVw.frame));
if (isResizingLR) {
testVw.frame = CGRectMake(testVw.frame.origin.x, testVw.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
}
if (isResizingUL) {
testVw.frame = CGRectMake(testVw.frame.origin.x + deltaWidth, testVw.frame.origin.y + deltaHeight, testVw.frame.size.width - deltaWidth, testVw.frame.size.height - deltaHeight);
}
if (isResizingUR) {
testVw.frame = CGRectMake(testVw.frame.origin.x ,testVw.frame.origin.y + deltaHeight, testVw.frame.size.width + deltaWidth, testVw.frame.size.height - deltaHeight);
}
if (isResizingLL) {
testVw.frame = CGRectMake(testVw.frame.origin.x + deltaWidth ,testVw.frame.origin.y , testVw.frame.size.width - deltaWidth, testVw.frame.size.height + deltaHeight);
}
if (!isResizingUL && !isResizingLR && !isResizingUR && !isResizingLL) {
testVw.center = CGPointMake(testVw.center.x + touchPoint.x - touchStart.x,testVw.center.y + touchPoint.y - touchStart.y);
}
}
Since you don't use UIView animations you can save current view's transform, set view's transform to identity, resize the view and reapply saved transform:
UIView *v;
CGAffineTransform t = v.transform;
v.transform = CGAffineTransformIdentity;
CGFloat scale = 2.0f;
v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, v.frame.size.width*scale , v.frame.size.height*scale);
v.transform = t;
(EDIT)About your resizing:
If you define your rectangle with 4 vectors (points) A,B,C,D where (A+C)*.5 = (B+D)*.5 = rectangle_center Then for moving point C to position C' would also move B and D to B' and D':
A' = A
C' = C' //touch input
B' = A + (normalized(B-A) * dotProduct((C'-A), normalized(B-A)))
D' = A + (normalized(D-A) * dotProduct((C'-A), normalized(D-A)))
After that:
transform your view to identity
set frame to (.0f, .0f, length(A-D), length(A-C))
set center to (A+D)*.5f
get rotation to create view's transform through atanf(height/width) and few "if's" OR fill/create transform with base vectors that are normalized(D-A) and normalized(B-A)
You can do that for any point in a rectangle.

Resources