iOS:ScrollView内部有可点击的对象

时间:2013-04-25 05:47:34

标签: ios iphone xcode ipad

以下代码行表示我插入scrollView的对象。

我的问题是,scrollview中的所有图片都不能拖放。

当单独添加图像时,即在滚动视图之外,它们完全是单击 - 并且可以拖放。

出现的每个事件都会触发scrollViews侦听器,而不是单击的图像侦听器。

无论图像的添加方式如何,scrollView都可以完全滚动。

如果单击,如何触发图像拖放,仍然可以滚动它的父视图 - 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

编辑:这是我执行拖放的方式: 项目类:

#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

2 个答案:

答案 0 :(得分:0)

将其添加到UIImageView:

page.userInteractionEnabled = YES;
item.userInteractionEnabled = YES;

答案 1 :(得分:0)

  

出现的每个事件都会触发scrollViews侦听器,而不是单击的图像侦听器。

当你为scrollview捕捉到这个事件时,循环浏览你添加的页面/图像&检查userInteractionEnabled的值?可能会有助于缩小问题的范围。