Drag and drop an image from UIPopoverController to UIViewController - ios

I'm showing a UIPopoverController in a ViewController, and I want to drag one image from the popover to the ViewController.
I know that to do it, I need to use a UIGestureRecognizer, but I don't know how. I can move the image around the popover, but I can't drag and drop it to the ViewController.
My code:
-(void)viewDidLoad
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveImage:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[self.view addGestureRecognizer:panGesture];
}
- (void)moveImage:(UIPanGestureRecognizer *)recognizer
{
CGPoint newCenter = [recognizer translationInView:self.view];
if([recognizer state] == UIGestureRecognizerStateBegan) {
beginX = ball.center.x;
beginY = ball.center.y;
}
newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y);
[ball setCenter:newCenter];
}

Related

UIPanGestureRecognizer always move from location (0, 0)

I'd like to drag customView.
But, when I drag the customView, it always move from location (0, 0).
Please look at following image.
the red parts is the customView, and when I drag it, it move to the location.
How do I fix it to make move customView correctly?
I have this code.
labelview is the customView(subclass of UIView).
- (void)viewDidLoad
{
[super viewDidLoad];
_labelView = [[LabelView alloc]initWithFrame:CGRectMake(20, 60, 200, 100)];
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(panAction:)];
[_labelView addGestureRecognizer:pan];
[self.view addSubview:_labelView];
}
- (void)panAction:(UIPanGestureRecognizer *)sender
{
CGPoint p = [sender translationInView:self.view];
CGPoint movedPoint = CGPointMake(_labelView.center.x + p.x, _labelView.center.y + p.y);
sender.view.center = movedPoint;
[sender setTranslation:movedPoint inView:self.view];
}
While the other two answers are technically correct, they are not ideal solutions because when you add a gesture recognizer to self.view then anytime you drag anywhere in self.view you will drag your LabelView, but I imagine you only want to drag it when you actually have your finger on the LabelView itself.
So, just keep what you have, but change this one line:
[sender setTranslation:movedPoint inView:self.view];
to
[sender setTranslation:CGPointZero inView:self.view];
You have implemented the logic upside down. It has to be exactly other way around. Set the ´UIPanGestureRecognizer´ to UIViewController's view and set the center of your custom view.
- (void)viewDidLoad
{
[super viewDidLoad];
_labelView = [[LabelView alloc]initWithFrame:CGRectMake(20, 60, 200, 100)];
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(panAction:)];
// [_labelView addGestureRecognizer:pan]; must be removed
[self.view addGestureRecognizer:pan]; // must be added
[self.view addSubview:_labelView];
}
- (void)panAction:(UIPanGestureRecognizer *)sender
{
CGPoint p = [sender translationInView:self.view];
CGPoint movedPoint = CGPointMake(_labelView.center.x + p.x, _labelView.center.y + p.y);
[_labelView setCenter:movedPoint];
}
I solved by writing following code.
Thank you for answers.
- (void)viewDidLoad
{
[super viewDidLoad];
_labelView = [[LabelView alloc]initWithFrame:CGRectMake(20, 60, 200, 100)];
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(panAction:)];
[_labelView addGestureRecognizer:pan];
[self.view addSubview:_labelView];
}
- (void)panAction:(UIPanGestureRecognizer *)sender
{
CGPoint p = [sender translationInView:sender.view];
CGPoint movedPoint = CGPointMake(sender.view.center.x + p.x, sender.view.center.y + p.y);
sender.view.center = movedPoint;
[sender setTranslation:CGPointZero inView:sender.view];
}

Using UIPanGestureRecognizer to scroll smoothly like UITableView or UIScrollView

I am using UIPanGestureRecognizer to slide/scroll a UIView. The following code works well and I am able to slide/scroll the panView. My question is when I end the gesture I should make the panView(UIView) scroll smoothly smiler like UITableView or UIScrollView scroll. So please help me out what should I code in UIGestureRecognizerStateEnded state. Please refer the below code. Thanks in advance!!
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scrollpanView:)];
[panRecognizer setDelegate:self];
[panView addGestureRecognizer:panRecognizer];
-(void)scrollpanView:(id)sender{
UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;
CGPoint translatedPoint = [gesture locationInView:[panView superview]].x;
if (UIGestureRecognizerStateBegan){
panXAxis = [gesture locationInView:[panView superview]].x;
translatedPoint = CGPointMake(panXAxis+translatedPoint.x, 50);
[panView setCenter:translatedPoint];
}
if ( UIGestureRecognizerStateChanged){
translatedPoint = CGPointMake(panXAxis+translatedPoint.x, 50);
[panView setCenter:translatedPoint];
}
if (UIGestureRecognizerStateEnded){
CGPoint velocity = [gesture velocityInView:[panView superview]]
// How should I scroll smoothly using the 'velocity' value
}
}

Move UITableView with UISwipeGestureRecognizer

I am trying to mix an UISwipeGestureRecognizer with an UITableView.
What I would like to do is, while I am doing the swipe gesture, move at the same time the UITableView outside the window and refresh the table data.
I am going to show you with images...
This is my view:
My View
And I would like to get something like this:
Desired View
I am able to move the table when the swipe gesture is ended, but not while I am doing the gesture, and that is what I want.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
swipeToRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(changeToGroup:)];
[swipeToRight setDelegate:self];
[swipeToRight setDirection:UISwipeGestureRecognizerDirectionRight];
[[self table]addGestureRecognizer:swipeToRight];
swipeToLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(changeToContact:)];
[swipeToLeft setDelegate:self];
[swipeToLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self table]addGestureRecognizer:swipeToLeft];
}
- (void)changeToGroup:(UISwipeGestureRecognizer *)swipeGesture
{
NSLog(#"Right to group");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:0];
}
- (void)changeToContact:(UISwipeGestureRecognizer *)swipeGesture
{
NSLog(#"Left to contact");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:1];
}
I thought that I could do it with UIGestureRecognizerStateBegan, adding the animations inside that event, but I can't receive it...
Could anyone help me?
Thanks a lot!!.
Try this,
use UIPanGestureRecognizer
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan:)];
[recognizer setMaximumNumberOfTouches:1];
[recognizer setDelegate:self];
[[self table]addGestureRecognizer:recognizer];
and in
- (void) handlePan:(UIPanGestureRecognizer *)recognizer {
UIView *view = self.tableView;
if(recognizer.state == UIGestureRecognizerStateBegan) {
// remember where the pan started
//panGestureOrigin is a CGPoint variable
panGestureOrigin = view.frame.origin;
//optional to keep an enum to keep direction
//self.panDirection = MenuPanDirectionNone;
}
CGPoint translatedPoint = [recognizer translationInView:view];
if(translatedPoint.x > 20) {
//self.panDirection = MenuPanDirectionRight;
[self handleRightPan:recognizer];
} else if(translatedPoint.x < 0) {
//self.panDirection = MenuPanDirectionLeft;
[self handleLeftPan:recognizer];
}
}
- (void) handleRightPan:(UIPanGestureRecognizer *)recognizer {
//animate here
if(recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"Right to group");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:0];
}
}
- (void) handleLeftPan:(UIPanGestureRecognizer *)recognizer {
// animate here
if(recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"Left to contact");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:1];
}
}

How to center superview along with view when using UIPanGestureRecognizer?

I Created a sample application, when we click on a button another view(ResizingViewController) view shows up.
when i drag ResizingViewController it is moving correctly but i can see another view behind it.I think it is super view (as show in in this image, but not able to make both move at same time).
please help me to fix it?(if i drag view then both should move to same position)
my code is as below
- (IBAction)click_Action:(id)sender {
if (!resizingViewController) {
return;
}
//Creating Right Button, asigning a method to it.
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Close"
style:UIBarButtonItemStylePlain target:self action:#selector(done)];
[resizingViewController.navigationItem setRightBarButtonItem:rightButton];
theNavigationController = [[UINavigationController alloc]
initWithRootViewController:resizingViewController];
[theNavigationController.navigationBar setTintColor:[UIColor blueColor]];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
theNavigationController.modalPresentationStyle
=UIModalPresentationFormSheet;
}
[self presentViewController:theNavigationController animated:YES completion:nil];
CGRect r = CGRectMake(100, 100, 300, 400);
r = [self.view convertRect:r toView:self.view];
theNavigationController.view.superview.frame = r;
//Adding gesture reconizer to resizingviewcontroller
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanFrom:)];
[theNavigationController.view addGestureRecognizer:panGestureRecognizer];
}
-(void)handlePanFrom:(UIPanGestureRecognizer *) recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view];
//Changing Center of the the view as soon as user selects the view and drags
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
CGPoint r = CGPointMake(recognizer.view.center.x+100,
recognizer.view.center.y+100);
r = [self.view convertPoint:r toView:self.view];
recognizer.view.superview.center = r;
//Should not forget to give below code, else view goes out of bounds.
[recognizer setTranslation:CGPointZero inView:self.view];
}
OK i fixed this.(marking this as answer so that if any one have same problem can use this)
-(void)handlePanFrom:(UIPanGestureRecognizer *) recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view.superview];
recognizer.view.superview.center =
CGPointMake(recognizer.view.superview.center.x+translation.x,
recognizer.view.superview.center.y+translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];
}

swipe with one finger and two finger swipe on the same UIView subclass

I have a custom implementation of UITableViewCell.
The UITableViewCell can be swiped to the left or the right.
I use a UIPanGestureRecognizer for the same.
UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
}
#pragma mark - horizontal pan gesture methods
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:[self superview]];
// Check for horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y)) {
return YES;
}
return NO;
}
-(void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
// if the gesture has just started, record the current centre location
// SOME CODE
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
// translate the center
//SOME CODE
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
// the frame this cell would have had before being dragged
//SOME CODE
}
}
Now I want to be able to support two finger swipe on on the entire screen such that even if a two finger swipe is done on a UITableViewCell, the above code is not triggered.
How can I achieve this ?
If you set the maximumNumberOfTouches on your gesture recognizer to 1, it will no longer accept multi-finger swipes:
UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
recognizer.maximumNumberOfTouches = 1;

Resources