Change selector while a pan gesture is going on - objective-c

I have a UIView C which is a subview of UIView B wich is a subview of UIView A. I add a UIPanGestureRecogizer to UIView C with a selector called "selectorX" and I want that when UIView C goes out of UIView A frame, then its superview changes to UIView A and I want also to change its UIPanGestureRecognizer with another selector "selectorY".
This is my code:
-(void)selectorX:(UIPanGestureRecognizer*)sender{
CGPoint translation = [sender translationInView:self.view];
sender.view.center = CGPointMake(sender.view.center.x, sender.view.center.y +translation.y);
[sender setTranslation:CGPointMake(0, 0) inView:sender.view];
UIView *uiViewC=(ImagenFichaView *)sender.view;
if (sender.view.center.y+translation.y<-50) {
[uiViewC removeFromSuperView]
CGPoint newCenter=[sender locationInView:uiViewA];
[uiViewA addSubview:uiViewC];
uiViewC.center=newCenter;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(selectorY:)];
panRecognizer.delegate=self;
[uiViewC removeGestureRecognizer:sender];
[uiViewC addGestureRecognizer:panRecognizer];
}
}
Everything goes well but the transition between the 2 UIPanGestureRecognizers is not continuous. When uiViewC changes its superView, the drag action stops. I have to take off my finger from the screen and start the movement again. What can I do to make the movement continuos?
Thank you very much

To get a continuous movement I didn't remove uiViewC from uiViewB, I just added it to uiViewA and it solved the problem.

Related

Touch UITableView background while scrolling (Kind of an easter egg)

I need to do a hidden button at the bottom of the table view, not in a cell, in the background of the table view itself.
I saw something like this in a game "Where is my water", if you scroll out of bounds you can find a hidden button.
first, I created a simple button, and placed in the bottom of the table view
self.tableView.backgroundColor = [UIColor purpleColor]; //just to see better
UIButton *venom = [[UIButton alloc] initWithFrame:CGRectMake(50.0, self.tableView.contentSize.height+80.0, 100.0, 40.0)];
venom.backgroundColor = [UIColor redColor];
[venom addTarget:self action:#selector(venomAction) forControlEvents:UIControlEventTouchDown];
[self.tableView addSubview:venom];
Because of the self.tableView.contentSize.height+80.0, I need to scroll out of the bounds of the table view to see the button, having something like this:
The result is correct, I want this to be sort of hidden, but the problem is, I cannot click on the button, to see the button, I need to be scrolling and I cannot multitouch.
Can anyone assist me with that? or point me to the right direction
I'd make the button not the tableView's subview, but rather a sibling view above the tableView.
For example, your code could look like this
- (void)viewDidLoad {
[super viewDidLoad];
// Just creating a simple UITableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tableView.dataSource = self;
tableView.delegate = self;
self.tableView = tableView;
[self.view addSubview:tableView];
// Creating the Easter Egg Button
UIButton *easterEggButton = [UIButton buttonWithType:UIButtonTypeSystem];
[easterEggButton setTitle:#"Easter Egg Button" forState:UIControlStateNormal];
[easterEggButton addTarget:self
action:#selector(easterEggButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
self.easterEggButton = easterEggButton;
// NOTE that we add the Easter Egg Button to self.view over the Table View, not into Table View
[self.view addSubview:easterEggButton];
}
So we have the button placed over the tableView. This detaches it from interfering with tableView's gestureRecognizer.
Now you need some code to adjust the button's frame to be placed somewhere under the tableView's last cell. It may look something like this:
- (void)updateButtonFrame {
UIButton *button = self.easterEggButton;
// Just the easiest way calculate the width and height of the button depending on it's content
[button sizeToFit];
CGRect frame = button.bounds;
// Now, we want to place the button under all the cells
frame.origin.y = self.tableView.contentSize.height;
frame.origin.y += 60.0f; // add some padding to place it even lower
/* We have calculated button's frame in tableView's coordinate space, so we need to convert it to
* button superview's coordinate space. */
frame = [self.tableView convertRect:frame toView:button.superview];
// And finally apply the frame to the button
button.frame = frame;
}
And of course you need to call this layout method in appropriate moments, that is, whenever tableView reloads it's data (because contentSize may change), and whenever tableView scrolls.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
/* Button resides in self.view and is unaware of Table View scrolling, so we need to update
* it's frame whenever Table View's content offset changes */
[self updateButtonFrame];
}
You may also call it viewDidLayoutSubviews to be safe if tableView's cell height depends on screen orientation and such.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// It's safe to recalculate button's frame whenever self.view layout updates
[self updateButtonFrame];
}
You may have a look at this example implementation
https://gist.github.com/bartekchlebek/429906e05fcbd976291d

ScrollRectToVisible function

I have an UIScrollview and which contains UIView as a subview like,
here i can able to drag the UIView inside the ScrollView, when the uiview goes to at END RIGHT or END LEFT the uiview disappears inside scrollview, after that i has to scroll the scrollview then i can able to see the uiview, here i need to SCROLL THE SCROLLVIEW when uiview comes at the rightside or left side, for that i used this code but not working.
[myscrollview scrollRectToVisible:myview.frame animated:YES];
Here I have used on UIButton (as it is already used in one of my application I dont need to write whole code again)
[cropRectangleButton addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
The cropRectangleButton is one UIButton and the imageMoved:withEvent: method is as below
- (IBAction) imageMoved:(id)sender withEvent:(UIEvent *)event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
CGPoint prev = lastTouchDownPoint;
lastTouchDownPoint = point;
CGFloat diffX = point.x - prev.x;
CGFloat diffY = point.y - prev.y;
UIControl *button = sender;
CGRect newFrame = button.frame;
newFrame.origin.x += diffX;
newFrame.origin.y += diffY;
scrollView.contentSize = CGSizeMake(2*scrollView.frame.size.width, scrollView.frame.size.height);
[scrollView scrollRectToVisible:newFrame animated:YES];
button.frame = newFrame;
}
Here I change my contentSize of scrollView as it is very narrow to test that the scrollView is scrolling or not you don't need that line of code.
And when I drag the button left the scrollView is also automatically scrolled to the button frame to show the whole button try to implement this using UIView and if not possible just put one transparant UIButton on your view fully covered the view and you it to do the drag :)

UIPanGestureRecognizer beyondBounds

I have this code that retrieves the coordinates of an object when it is panned:
UITapGestureRecognizer *moveBuildingTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(moveobject:)];
Method moveobject contents:
CGPoint tapPoint=[recognizer locationOfTouch:0 inView:self.view];
I use this to change the frame of - move it -an imageview using these coordinates.
However, upon dragging the image around - triggering the uipangesturerecognizer action, I found that when I drag it to the absolute bottom, I get an error that -[UIPanGestureRecognizer locationOfTouch:inView:]: index (0) beyond bounds (0).
How can I solve this exception and prevent the user from dragging past this point?
Thanks
It's weird that your moveobject: method gets called even though the private touches array of the gesture recognizer seems to be empty.
Anyway, in general, if you don't handle multitouch gestures within gesture recognizer, I would suggest to use [recognizer locationInView:] rather then locationOfTouch:inView:.
Btw:
Your talking about a UIPanGestureRecognizer while in the code you're using a UITapGestureRecognizer.
The code I would recommend to handle dragging of a particular view looks like this:
//...
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[someView addGestureRecognizer:panGR];
//...
- (void)handlePan:(UIPanGestureRecognizer *)gr
{
CGPoint translation = [gr translationInView:gr.view];
gr.view.frame = CGRectOffset(gr.view.frame, translation.x, translation.y);
[gr setTranslation:CGPointZero inView:gr.view];
}
You should check the numberOfTouches in your UIGestureRecognizer like:
if (recognizer.numberOfTouches) {
CGPoint tapPoint = [recognizer locationOfTouch:0 inView:self.view];
}

How can I get touch offset when using UITouch

How can I get touch offset when using UITouch? I am doing a project like a remote control. I have a view set to multi-touched and I want the view to act like a touch pad for Mac, so I need the touch offsets when people move to control the mouse. Any ideas?
This can be accomplished with a UIPanGestureRecognizer by measuring the diagonal distance from the center of the screen to the current touch location.
#import <QuartzCore/QuartzCore.h>
Declare the gesture and hook it to self.view so that the entire screen responds to touch events.
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(myPanRecognizerMethod:)];
[pan setDelegate:self];
[pan setMaximumNumberOfTouches:2];
[pan setMinimumNumberOfTouches:1];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:pan];
Then in this method, we use the gesture recognizers state: UIGestureRecognizerStateChanged to update and integer that measures the diagonal distance between touch location and screen center as the touch location changes.
-(void)myPanRecognizerMethod:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged) {
CGPoint touchLocation = [sender locationOfTouch:0 inView:self.view];
NSNumber *distanceToTouchLocation = #(sqrtf(fabsf(powf(self.view.center.x - touchLocation.x, 2) + powf(self.view.center.y - touchLocation.y, 2))));
NSLog(#"Distance from center screen to touch location is == %#",distanceToTouchLocation);
}
}

MKMapView and the responder chain

I have an MKMapView with a single subview:
MKMapView *mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 200, 200)];
subView.backgroundColor = [UIColor grayColor];
[mapView addSubview:subView];
I would expect that because the subview does not handle any touch events, all touch events would be passed along to the parent map view (via the responder chain). I would then expect that panning and pinching in the subview would pan and pinch the map.
This, unfortunately, does not appear to be the case. Does anyone know of a way to get the map view in to the responder chain?
I realize overriding hitTest in my subview can achieve what I'm expecting here, but I can't use that approach because I have other gestures I need to respond to in the subview.
How about handling all gestures with UIGestureRecognizers (properly setup to ignore other gesture recognizer or to fire simultanously with them) added to your mapView and disabling userInteractionEnabled of your subview?
I use the following code to listen to Taps on the mapView without interfering with the standard gestures:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mtd_handleMapTap:)];
// we require all gesture recognizer except other single-tap gesture recognizers to fail
for (UIGestureRecognizer *gesture in self.gestureRecognizers) {
if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
UITapGestureRecognizer *systemTap = (UITapGestureRecognizer *)gesture;
if (systemTap.numberOfTapsRequired > 1) {
[tap requireGestureRecognizerToFail:systemTap];
}
} else {
[tap requireGestureRecognizerToFail:gesture];
}
}
- (void)mtd_handleMapTap:(UITapGestureRecognizer *)tap {
if ((tap.state & UIGestureRecognizerStateRecognized) == UIGestureRecognizerStateRecognized) {
// Get view frame rect in the mapView's coordinate system
CGRect viewFrameInMapView = [self.mySubview.superview convertRect:self.mySubview.frame toView:self.mapView];
// Get touch point in the mapView's coordinate system
CGPoint point = [tap locationInView:self.mapView];
// Check if the touch is within the view bounds
if (CGRectContainsPoint(viewFrameInMapView, point)) {
// tap was on mySubview
}
}
}