Cancel touches if it moves out CGPoint - ios

I have a 400x400 UIViewController(lets call it ViewB) in center of my RootController. Inside the ViewB I have few UIButton (Customised UIButton Class with touch UIResponder methods).
I'm able to move the buttons around, but when the touch goes out of the ViewB, the button does cancel the touch!
What I really want is to totally cancel the touch and leave the button near the edge of ViewB.

Try this,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
....
CGRect viewBFrame = ViewB.view.frame;
CGRect buttonToRect = //calculated button frame
CGPoint buttonOrigin = buttonToRect.origin;
CGFloat xMax = CGRectGetWidth(viewBFrame) - CGRectGetWidth(buttonToRect);
CGFloat yMax = CGRectGetHeight(viewBFrame) - CGRectGetHeight(buttonToRect);
buttonOrigin.x = MAX(0, buttonOrigin.x);
buttonOrigin.x = MIN(xMax, buttonOrigin.x);
buttonOrigin.y = MAX(0, buttonOrigin.y);
buttonOrigin.y = MIN(yMax, buttonOrigin.y);
//set buttonToRect to Button
}
Note : Assuming button is subView of ViewB

Related

Transfer touches from uiview to uiscrollview

I have a UIScrollview (With image) in UIView (Red color).
UIView - {0,0, 320, 236} UIScrollview - {0, 8, 300, 220}
In my uiview, I got touches.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}
I need to transfer touch from UIView to UIScrollview. (e.g if user swipe from right side of uiview to left, uiscrollview should scroll to left in sync with user touch). May I know how to do?
The idea is to get a reference to your scrollView inside your UIView, calculate the x touches delta and adjust the contentOffset property of the scrollView accordingly.
So, in your UIView class:
#implementation MyView{
CGPoint _startPosition;
CGPoint _previousPosition;
}
Initialize the above variables in your
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
_startPosition = [touch locationInView:self];
_previousPosition = _startPosition;
}
Then in your touchesMoved:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.scrollView];
if(!CGPointEqualToPoint(_previousPosition, _startPosition)){
CGFloat deltaX = _previousPosition.x-currentPosition.x;
[UIView animateWithDuration:0.1 animations:^{
//
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x +
deltaX, self.scrollView.contentOffset.y);
}];
}
_previousPosition = currentPosition;
}
The scrollView.contentOffset is adjusted in an animation block to make the scroll smooth.
Link to a working project: https://github.com/sliaquat/stack_overlfow_answer_28036976

Displaying a view over an imageview which is moveable

I currently have an application where a user takes a photo or chooses from their library.
On the next controller it will show the image that was selected/taken.
What I would like to do now is show a view of some sort on top of the image view. The view would be translucent round the edges and have a circle which would show the image beneath (not transulcent). Basically this is selected a part of the image.
I then need to save some how where the view is on screen, as it should also be moveable by the user.
What is the best way to approach this?
I need an overlay view which can be moved. The circle would be a fixed size always the inside shows the imageview beneath and the outside would be a 0.5 translucency so you can still see the image but not completely. Then to save the location of the moved around circle?
---- EDIT -----
This is the example image I have created.
I have a view which has a photo as the UIImageView (bottom layer). On top of this I am trying to add a view (like the picture above). Note, the picture above is actually a png as suggested. However, this overlay is moveable. The circle is transparent (0) so you can completely see the photo below it. The outer (grey) is transparent partially (0.5) so you can still see just not completely.
The top view (circle part) would be moved around on the photo to mark a specific point on the photo. In this example if the circle is moved the side (grey) ends on screen, therefore I would need to make a huge image which takes into account the moving of the view -- which is not the best way to do this surely?
EDIT 2 ----
I now have one UIImageView over the top of the photoView (another UIImageView). The overlay is 4 times the screen with a circle in the middle of it.
When the item is moved I have a gesture recognizer that runs:
-(void)handlePan:(UIPanGestureRecognizer *)gesture {
NSLog(#"Pan Gesture");
gesture.view.center = [gesture locationInView:self.view];
}
At present from the above the UIImageView is moved from the middle point, that way the circle is what looks to be moving when the finger moves.
That is all great, but how do I implement the suggested answers into my handlePan method. So I need to check that the middle point is not too close the edge. Ideally I would like a 50 margin around the screen so the circle does not look to go completely (or mostly) off screen?
I understand from the question that you know how to do the movement of the overlay, so what I meant is just that you can simply use image views big enough to make sure that their border isn't visible.
Let's say the UIImageView would be called
UIImageView *photoImageView;
And the overlay with translucent and semi-translucent areas would be
UIImageView *imageOverlayView;
The way I see it, you need 3 different images for that imageOverlayView, depending on the device it's running on - 1 for iPad, 1 for iPhone 5 and 1 for other iPhones.
Width of that image should be equal to (screen width - circle radius) * 2, height should
CGFloat circleRadius; //this would be the radius of translucent circle
So if you set the frame of the overlay properly:
CGRect imageFrame = photoImageView.frame;
imageOverlayView.frame = CGRectMake(circleRadius - imageFrame.size.width/2, circleRadius - imageFrame.size.height/2, (imageFrame.size.width - circleRadius)*2, (imageFrame.size.height - circleRadius)*2); //origin is set to the middle of the image.
you'd never see the edge of the grey area. MZimmerman6's answer for implementation of the movement is good, but you should also make sure you block the circle from getting out of the borders of underlying image.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint new = [touch locationInView:imageOverlayView];
delX = new.x - prevPoint.x;
delY = new.y - prevPoint.y;
CGRect overFrame = imageOverlayView.frame;
overFrame.origin.x += delX;
if (overFrame.origin.x < -imageFrame.size.width) {
overFrame.origin.x = -imageFrame.size.width;
}
else if (overFrame.origin.x > 0) {
overFrame.origin.x = 0;
}
overFrame.origin.y += delY;
if (overFrame.origin.y < -imageFrame.size.height) {
overFrame.origin.y = -imageFrame.size.height;
}
else if (overFrame.origin.y > 0) {
overFrame.origin.y = 0;
}
[overlayView setFrame:overFrame];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint new = [touch locationInView:imageOverlayView];
delX = new.x - prevPoint.x;
delY = new.y - prevPoint.y;
CGRect overFrame = imageOverlayView.frame;
overFrame.origin.x += delX;
if (overFrame.origin.x < -imageFrame.size.width) {
overFrame.origin.x = -imageFrame.size.width;
}
else if (overFrame.origin.x > 0) {
overFrame.origin.x = 0;
}
overFrame.origin.y += delY;
if (overFrame.origin.y < -imageFrame.size.height) {
overFrame.origin.y = -imageFrame.size.height;
}
else if (overFrame.origin.y > 0) {
overFrame.origin.y = 0;
}
[overlayView setFrame:overFrame];
}
EDIT
With your pan gesture recognizer, checking if you aren't going too far needs a little change to the handlePan: method.
-(void)handlePan:(UIPanGestureRecognizer *)gesture {
CGPoint newCenter = [gesture locationInView:self.view];
if (newCenter.x < 50) {
newCenter.x = 50;
}
else if (newCenter.x > self.view.frame.size.width - 50) {
newCenter.x = self.view.frame.size.width - 50;
}
if (newCenter.y < 50) {
newCenter.y = 50;
}
else if (newCenter.y > self.view.frame.size.height - 50) {
newCenter.y = self.view.frame.size.height - 50;
}
gesture.view.center = newCenter;
}
If that 50 points margin is equal to circle radius, this will make sure your circle able to touch the edges of the screen, but unable to go beyond them.
I would first initialize some UIImageView with a png image in it that will easily handle this moving frame with a hole in the center. You can the add this to your screen. Once this is done, use the touchesBegan, touchesMoved and other touch commands to find whether a user is touching that box and from that determine how far the user has moved from the previous point. Use this difference to then add or subtract values from the images current frame origin or center, and voila you have a moving box.
EDIT
In View.h
CGPoint prevPoint;
float delX;
float delY;
UIView *overlayView;
In View.m
-(void) viewDidLoad {
overlayView = [UIView alloc] initWithFrame:CGRectMake(100,100,100,100)];
[overlayView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:overlayView];
UIImageView *circImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,overlayView.frame.width,overlayView.frame.height)];
[circImage setImage:[UIImage imageNamed:#"SomeImage.png"]];
[overlayView addSubview:circImage];
[self.view setNeedsDisplay];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
prevPoint = [touch locationInView:overlayView];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint new = [touch locationInView:overlayView];
delX = new.x - prevPoint.x;
delY = new.y - prevPoint.y;
CGRect overFrame = overlayView.frame;
overFrame.x += delX;
overFrame.y += delY;
[overlayView setFrame:overFrame];
[self.view setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint new = [touch locationInView:overlayView];
delX = new.x - prevPoint.x;
delY = new.y - prevPoint.y;
CGRect overFrame = overlayView.frame;
overFrame.x += delX;
overFrame.y += delY;
[overlayView setFrame:overFrame];
[self.view setNeedsDisplay];
}

Why a subView won't stay in the parent frame?

I have a view and inside it there's a UIImage. The image isn't static and I can move it around if I drag my finger around (using drag events) The problem is that sometimes the picture moves outside of the UIView frame. What's the appropriate way to keep it inside the parent frame bounds?
--UIViewA
--------UIViewB
--------------UIImage
--------------UIButton
I want to keep UIImage inside UIViewB
- (IBAction)myButtonSingleTap:(UIButton *)sender {
imDragging = YES;
[_myButton addTarget:self action:#selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
}
- (IBAction)myButtonDragInside:(UIButton *)sender
{
[_myButton addTarget:self action:#selector(draging:withEvent:) forControlEvents: UIControlEventTouchDragInside];
}
- (void)dragBegan:(UIControl *)c withEvent:ev {
UITouch *touch = [[ev allTouches] anyObject];
startingTouchPoint = [touch locationInView:self.view];
}
- (void)draging:(UIControl *)c withEvent:ev {
UITouch *touch = [[ev allTouches] anyObject];
currentTouchPoint = [touch locationInView:self.view];
_movingPic.frame = CGRectMake(currentTouchPoint.x, currentTouchPoint.y, 28, 23);
}
You will need to check the location of the view during the dragging.
At some point you will be setting the frame of the image depending on the user's drag direction, etc...
During this you should have a logic check like...
If new location x value is less than 0 then set new location x = 0.
If new location x value plus image width is greater than view width then set new location x = view width - image width.
etc...
Then use the new location as the point to move the image to.
Try adding the touch recogniser to the parent view and not the entire view
Before setting the new frame, make sure it is contained by the moving view's superview's bounds.
- (void)draging:(UIControl *)c withEvent:ev
{
UITouch *touch = [[ev allTouches] anyObject];
currentTouchPoint = [touch locationInView:self.view];
CGRect newFrame = CGRectMake(currentTouchPoint.x, currentTouchPoint.y, 28, 23);
newFrame.x = MAX(newFrame.x, 0);
newFrame.y = MAX(newFrame.y, 0);
newFrame.x = MIN(newFrame.x, _movingPic.superview.bounds.size.width - 28);
newFrame.y = MIN(newFrame.y, _movingPic.superview.bounds.size.height - 23);
_movingPic.frame = newFrame;
}

How to drag an image view after applying CGAffineTransformMakeRotation

I am developing an app, Which has feature that dragging and rotating the image view.
I have implemented to drag and rotate successfully by using below code
In touchesBegan Method
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
touchStart = [[touches anyObject] locationInView:imageView];
isResizingLR = (containerVw.bounds.size.width - touchStart.x < kResizeThumbSize && containerVw.bounds.size.height - touchStart.y < kResizeThumbSize);
}
In touchesMoved Method
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:containerVw];
CGPoint previous=[[touches anyObject]previousLocationInView:containerVw];
UITouch *touch = [[event allTouches] anyObject];
if (isResizingLR) {
CGFloat currA = [self pointToAngleFromCenter:[touch locationInView:containerVw.superview]] ;
CGFloat prevA = [self pointToAngleFromCenter:[touch previousLocationInView:containerVw.superview]] ;
// the current value of the rotation angle
CGFloat tranA = atan2(containerVw.transform.b, containerVw.transform.a) ;
// the angle difference between last touch and the current touch
CGFloat diffA = currA - prevA ;
// the new angle resulting from applying the difference
CGFloat angle1 = tranA - diffA ;
CGAffineTransform t = CGAffineTransformMakeRotation(angle1) ;
containerVw.transform =t;
}
if (!isResizingLR) {
containerVw.center = CGPointMake(containerVw.center.x + touchPoint.x - touchStart.x, containerVw.center.y + touchPoint.y - touchStart.y);
}
}
My Problem
Above code is working fine until rotating the image view after dragging. In case if i drag the image view after rotating it. image view has gone away from screen.
In touchesEnd method i tried to print the image view frame. for example my image view frame at the beginning is (100,100,150,149) after rotating it the image view 160ยบ frame has changed to (103,31,182,183) up to now its working fine.
After rotating it if try to move an image view, the image view has gone away from screen now the frame has changed to (615,-212,182,183)
Please guide me how do i resolve it.
Have a look at this project. This may help you. But here, the movement is handled by gestures and not by touch. It may solve your problem.

Superview does not receive the touch action

I have one base view A with size of (0,0,320,280), it contain another view B with size of
(100,100,50,50) and View B has one button and one image view(C) as sub view. image view frame is same as view B, button has added in B's top left corner.
My requirement is when we drag the B's bottom right corner its size has to increased or decreased.
if we drag the B from any other place except bottom right corner it has to move. view size should not be modified.
My problem is view B does not receive the touch action.
i added the code below. please guide me.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
//baseVw-------> view B//
if ([touch view]==baseVw)
{
touchStart = [[touches anyObject] locationInView:baseVw];
isResizingLR = (baseVw.bounds.size.width - touchStart.x < kResizeThumbSize && baseVw.bounds.size.height - touchStart.y < kResizeThumbSize);
isResizingUL = (touchStart.x <kResizeThumbSize && touchStart.y <kResizeThumbSize);
isResizingUR = (baseVw.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
isResizingLL = (touchStart.x <kResizeThumbSize && baseVw.bounds.size.height -touchStart.y <kResizeThumbSize);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [[touches anyObject] locationInView:baseVw];
CGPoint previous=[[touches anyObject]previousLocationInView:baseVw];
float deltaWidth = touchPoint.x-previous.x;
float deltaHeight = touchPoint.y-previous.y;
if ([touch view]==baseVw)
{
if (isResizingLR)
{
baseVw.frame = CGRectMake(baseVw.frame.origin.x, baseVw.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
}
else
{
CGPoint activePoint = [[touches anyObject] locationInView:baseVw];
// Determine new point based on where the touch is now located
CGPoint newPoint = CGPointMake(baseVw.center.x + (activePoint.x - touchStart.x),
baseVw.center.y + (activePoint.y - touchStart.y));
//--------------------------------------------------------
// Make sure we stay within the bounds of the parent view
//--------------------------------------------------------
float midPointX = CGRectGetMidX(baseVw.bounds);
// If too far right...
if (newPoint.x > baseVw.superview.bounds.size.width - midPointX)
newPoint.x = baseVw.superview.bounds.size.width - midPointX;
else if (newPoint.x < midPointX) // If too far left...
newPoint.x = midPointX;
float midPointY = CGRectGetMidY(baseVw.bounds);
// If too far down...
if (newPoint.y > baseVw.superview.bounds.size.height - midPointY)
newPoint.y = baseVw.superview.bounds.size.height - midPointY;
else if (newPoint.y < midPointY) // If too far up...
newPoint.y = midPointY;
// Set new center location
baseVw.center = newPoint;
}
}
}
View B probably does not receive any touch events because these are absorbed by the subview C. Assuming you implemented the touch methods in B you should also make sure that you set B as the firstResponder. Implement this:
-(BOOL)canBecomeFirstResponder
{
return YES;
}
And then call [self becomeFirstResponder]; after you add C as the subview. The code inside the touchesBegan/Moved doesn't matter if they are not being called (touch events not received).

Resources