how dragged image with a min and max value? - ios

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);

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 to keep imageview inside UIView when moving imageview?

I have an UIView inside main view controller which contain 1 imageview.i moved imageview with this method.
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
if (aTouch.view == self.sub_View) {
CGPoint location = [aTouch locationInView:self.sub_View];
CGPoint previousLocation = [aTouch previousLocationInView:self.sub_View];
self.imageView.frame = CGRectOffset(self.imageView.frame, (location.x - previousLocation.x), (location.y - previousLocation.y));
}
}
Imageview move perfect. But i have to keep imageview inside UIView when i moved. Problem with this code is when i move imageview it is also moved outside UIView. I have to keep imageview inside UIView and movable only inside UIView.
Please help me to solve this. How to set boundary for imageview so it is movable only inside UIView.
Try below code.
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
if (aTouch.view == self.sub_View) {
CGPoint location = [aTouch locationInView:self.sub_View];
CGPoint previousLocation = [aTouch previousLocationInView:self.sub_View];
CGRect newFrame = CGRectOffset(self.imageView.frame, (location.x - previousLocation.x), (location.y - previousLocation.y));
if(newFrame.origin.x < 0)
newFrame.origin.x = 0;
if(newFrame.origin.y < 0)
newFrame.origin.y = 0;
if(newFrame.origin.x + newFrame.size.width > self.frame.size.width)
newFrame.origin.x = self.frame.size.width - self.imageView.frame.size.width;
if(newFrame.origin.y + newFrame.size.height > self.frame.size.height)
newFrame.origin.y = self.frame.size.height - self.imageView.frame.size.height;
self.imageView.frame = newFrame;
}
}
a) You can set the clipsToBounds property of your UIView to YES
b) You can use the fallowing code for moving your UIImageView
self.imageView.center = CGPointMake(MAX(0.0, MIN(location.x - previousLocation.x, self.sub_View.bounds.size.width)), MAX(0.0, MIN(location.y - previousLocation.y, self.sub_View.bounds.size.height)));

Having issue with UITextView drag and drop

I am trying to drag a UITextView object with touch ! but I have a problem , I need when user touches the custom view , the touching event begins, but touching methods works only on self.view . here is my code :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touch began ");
UITouch *touch = [touches anyObject];
startingX = [touch locationInView:_captureView].x;
startingY = [touch locationInView:_captureView].y;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint currentPoint = _mText.frame.origin;
float xForView, yForView;
UITouch *touch = [touches anyObject];
float newX = [touch locationInView:_captureView].x;
float deltaX;
if(startingX > newX){
deltaX = startingX - newX;
xForView = currentPoint.x - deltaX;
} else if(newX > startingX){
deltaX = newX - startingX;
xForView = currentPoint.x + deltaX;
} else xForView = currentPoint.x;
float newY = [touch locationInView:_captureView].y;
float deltaY;
if(startingY > newY){
deltaY = startingY - newY;
yForView = currentPoint.y - deltaY;
} else if(newY > startingY){
deltaY = newY - startingY;
yForView = currentPoint.y + deltaY;
} else yForView = currentPoint.y;
CGRect newFrame = CGRectMake(xForView, yForView, _mText.frame.size.width, _mText.frame.size.height);
_mText.frame = newFrame;
startingX = newX;
startingY = newY;
}
my textView is not editable all views are userInteractionEnabled
Write your touches began method and touches moved method inside the your custom text view and try to adjust the frame.

Boundary for draggable image ios

I'm trying to create a draggable image, but i'm trying to confine it's dragging to within a small square rather than the full screen. Could someone tell me where i'm going wrong?. I've placed the code that I have so far below:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if([touch view] == dot) {
CGPoint location = [touch locationInView:self.view];
dot.center = location;
if (location.x >10) {
location.x =10;
} else if (location.x <10) {
location.x = 10;
}
if (location.y >20) {
location.y =20;
} else if (location.y < 20) {
location.y = 20;
}
}
}
You are assigning location before you are making changes to it.
Apply your limits to location first then assign it to dot.
Also, your limits you are showing would lock your position to 10,20 since you are not allowing it to be more than 10 or less than 10. Likewise with 20.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if([touch view] == dot) {
CGPoint location = [touch locationInView:self.view];
location.x = MIN(MAX(location.x, 0),10);
location.y = MIN(MAX(location.y, 0),20);
dot.center = location;
}
}
I have implemented an image dragging function recently like this. I use the PAN Gesture to move the image which results in two CGFloats "endPointX and endPointY". In the code below between the comments "Stay on Screen Check" and "End Stay on Screen check", I check if these are on the screen. If not I adjust them to prevent the image moving off the screen.
I hope that helps. If you want to move the image within a small part of the total screen then I would add the image to a holder subview and then check the holder view .bounds.size.width/height above instead.
CGFloat endPointX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender
velocityInView:self.view].x);
CGFloat endPointY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
// Stay on the screen check
if(endPointX < 0) {
endPointX = 0;
} else if(endPointX > self.view.bounds.size.width) {
endPointX = self.view.bounds.size.width;
}
if(endPointY < 0) {
endPointY = 0;
} else if(endPointY > self.view.bounds.size.height) {
endPointY = self.view.bounds.size.height;
}
// End of the Stay on Screen check
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(endPointX, endPointY)];
[UIView commitAnimations];

IOS: drag uiimageview only in one direction

I want to drag an uiimageview only in horizontal from a point1 to a point2; how can I do it?
you can use somting like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
touchStart = [[touches anyObject] locationInView:/*your main view*/];
point1 = CGPointMake(140, 140);
oldrect = imgview.rect;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:/*your main view*/];
imgview.center = CGPointMake(imgview.center.x + point.x - touchStart.x, imgview.center.y);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:/*your main view*/];
[UIView animateWithDuration:0.25
animations:^{
if (((point1.x -15) > point.x) && ((point1.x + 15 ) < point.x) {
Imgview.rect = CGRectMake(point.x, point.x, imgview.frame.size.width,imgview.frame.size.height);
}
else {Imgview.rect = oldrect;}
completion:^(BOOL finished){ }];} }
Simple, check your imageView x position and your touch x position
float lastX = myimageview.center.x;
if the touch point x is bigger than lastX (or smaller, depends on the direction you want)
{
// Do the move
} else {
// Do nothing
}
Your code should look something like that :
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Move relative to the original touch point
CGPoint pt = [[touches anyObject] locationInView:self];
float lastX = myimageview.center.x;
if (pt.x > lastX){
// move myimageview.x to pt.x
}
}

Resources