How I can drag/move an imageView on pdf view? (Objective-C)

267 views Asked by At

What I want is that, on top of pdf view I want to add an image (one or more than 1 image). Once I add from some button, it has to come(appear) on pdf view to any location, and based on my choice I can able to drag/move that image on any location/position on the pdf view.

What I tried:

Example1: I took one ViewController and and added one imageView programatically on the view. And using some touch event method I can easily move/drag the image anywhere on the view

and here is the code for this,

    #import "ViewController.h"

@interface ViewController ()
{
    UIImageView * imageview;
    CGRect frame;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect myImageRect = CGRectMake(100, 100, 200, 35);
    self->imageview = [[UIImageView alloc] initWithFrame:myImageRect];
    [self->imageview setImage:[UIImage imageNamed:@"signer"]];
    [self.view addSubview:self->imageview];
}
 

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [[event allTouches] anyObject];
    //CGPoint touchLocation = [touch locationInView:touch.view];
    CGPoint touchLocation = [touch locationInView:touch.view];

    CGRect frame = [self.view convertRect:self->imageview.frame fromView:self.view];
    NSLog(@"touchesMoved %@", CGRectCreateDictionaryRepresentation(frame));
    
    if ([touch.view isEqual: self.view]) {

        self->imageview.center = touchLocation;

        return;
    }
}

@end

What I Wants to achieve:

Example2: I took one ViewController, and I added pdf view on that. Similarly I added one imageView on pdfView. When I tried to drag/move that imageView its not moving like it is working in example1.

How I can achieve that moving/drag the imageView on pdfView? Anyone can help me please.

Both project code can be found on following link

1

There are 1 answers

0
Dhawal On BEST ANSWER

Problem is that imageview added below the scrollview of pdfview. other issue move imageView not moving like example 1 because pdfView contain scrollview so when you touch on pdfview it calls gesture of pdfview not touchesMoved method declare in ViewController.

Solution:

add this line in viewDidLoad

self->imageview.userInteractionEnabled = YES;

add this line in preparePDFViewWithPageMode method at last

[self.pdfView bringSubviewToFront:self->imageview];

Replace in `touchMoved'

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
       UITouch *touch = [[event allTouches] anyObject];
       CGPoint touchLocation = [touch locationInView:self.pdfView];
       self->imageview.center = touchLocation;
}

enter image description here