user1824518
user1824518

Reputation: 223

implementing touches to drag various images with finger

I'm trying to be able to drag various images around using touches. So far, I'm trying to get it to work with just 1 image, but it's not working (i.e. I can't move the image with my finger). What am I doing wrong?

I have several files with the following code:

DragView.h

@interface DragView : UIImageView {
}
@end

DragView.m

#include "DragView.h"
    @implementation DragView

    - (void)touchesMoved:(NSSet *)set withEvent:(UIEvent *)event {
        CGPoint p = [[set anyObject] locationInView:self.superview];
        self.center = p;
    }

    @end

ViewController.h

#import <UIKit/UIKit.h>
#import "DragView.h"

@interface ViewController : UIViewController {
}

@property (nonatomic, strong) DragView *basketView;

@end

ViewController.m

 #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    @synthesize basketView;

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        basketView = [[DragView alloc]
                      initWithImage:[UIImage imageNamed:@"basket.png"]];
        basketView.frame = CGRectMake(140, 340.2, 60, 30);
        [self.view addSubview:basketView];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end

Upvotes: 1

Views: 58

Answers (1)

Artem Shmatkov
Artem Shmatkov

Reputation: 1433

Try this:

basketView.userInteractionEnabled = YES;

Documentation says that:

New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.

Upvotes: 1

Related Questions