Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The following lines of code represents objects that i insert into a scrollView.

My problem is, none of the images inside the scrollview are drag-and-droppable.

When the images are added alone, that is outside the scrollview, they are perfectly click - and drag-and-droppable.

It appears every event that occur triggers the scrollViews listener, and not the clicked image listener.

The scrollView is perfectly scrollable regardless of how the images are added.

How can i trigger the image drag-and-drop if clicked, and still be able to scroll it's parent view - the scrollView?

#import "BookShelfSection.h"

@implementation BookShelfSection



- (id)initBookShelfWithX:(CGFloat)x y:(CGFloat)y width:(CGFloat)width height:(CGFloat)height
{
    self = [super initWithFrame:CGRectMake(x, y, width, height)];
    self.userInteractionEnabled = YES;
    return self;
}

    - (void)addPages:(NSArray *)fileNameArray
{
    for (id shelf in fileNameArray) {
        UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, self.frame.size.width, self.frame.size.height)];
        UIImage *image = [UIImage imageNamed:[NSString stringWithString:shelf]];
        UIImageView *page = [[UIImageView alloc] initWithImage:image];
        page.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        page.contentMode = UIViewContentModeBottom;

        UIImage *image2 = [UIImage imageNamed:@"car_1.png"];
        Item *item = [[Item alloc] initWithImage:image2 andPosition:CGPointMake(100, 100)];
        page.userInteractionEnabled = YES;
        item.userInteractionEnabled = YES;
        [view addSubview:page];
        [view addSubview:item];

        [self addPageView:view];
    }

}


@end

EDIT: This is how i perform my drag and drop: The Item Class:

#import "Item.h"

@implementation Item

- (id)initWithImage:(UIImage *)image andPosition:(CGPoint)position
{

    CGRect cellRectangle = CGRectMake(position.x, position.y,image.size.width ,image.size.height );

    self = [[Item alloc] initWithFrame:cellRectangle];
    if (self) {
        [self setImage:image];
        [self setUserInteractionEnabled:YES];
    }
    return self;
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
    [[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint pt = [[touches anyObject] locationInView:self];
    CGRect frame = [self frame];
    frame.origin.x += pt.x - startLocation.x;
    frame.origin.y += pt.y - startLocation.y;
    [self setFrame:frame];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGRect frame = [self frame];
    int originX = (int)roundf(frame.origin.x);
    int originY = (int)roundf(frame.origin.y);
    NSLog(@"Touch ended at point: [%d, %d]", originX, originY);


}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch cancelled");
}

@end
share|improve this question
put scrollview.userInteractionEnabled=YES; and for images also – Sunny Apr 25 at 5:50
edited, no luck yet. Thanks – Eyeball Apr 25 at 5:56
How do you perform the drag and drop? – Avi Tsadok Apr 25 at 5:57
Edited, see the Item class, Thanks. – Eyeball Apr 25 at 5:59
I edited the way i create item. – Eyeball Apr 25 at 6:12

2 Answers

Add that to the UIImageView:

page.userInteractionEnabled = YES;
item.userInteractionEnabled = YES;
share|improve this answer
Edited: Still no progress though. – Eyeball Apr 25 at 5:55

It appears every event that occur triggers the scrollViews listener, and not the clicked image listener.

When you catch this event for scrollview, loop through your added page/images & check the value of userInteractionEnabled ? May be that will help to narrow down the problem.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.