0

汚いタイトルでごめんなさい:(

私は、他のいくつかのビュー (この場合は uiimageview のサブクラスである IngredientImage) を表示するスクロールビューを持つコントローラーを持っています。

#import "IngredientImage.h"

@implementation IngredientImage    

- (id) initWithImage:(UIImage *)image {
    if (self = [super initWithImage:image]) {

    }
    [self setUserInteractionEnabled:YES];
    return self;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint location = [[touches anyObject] locationInView:self];

    if (CGRectContainsPoint([self frame], location)) {
         NSLog(@"This works...");   
    }
}

- (void)dealloc {
    [super dealloc];
}


@end

ビューをスクロールビューに配置するコードがあります

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addIngredients];

}

- (void)addIngredients {
    NSUInteger i;
    for (i = 1; i <= 10; i++) {
        UIImage *image = [UIImage imageNamed:@"ing.png"];
        IngredientImage *imageView = [[IngredientImage alloc] initWithImage:image];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        CGRect rect = imageView.frame;
        rect.size.height = 50;
        rect.size.width = 50;
        imageView.frame = rect;
        imageView.tag = i;  // tag our images for later use when we place them in serial fashion
        [ingredientsView addSubview:imageView];
        [imageView release];
        [image release];
    }

    UIImageView *view = nil;
    NSArray *subviews = [ingredientsView subviews];

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curYLoc = INGREDIENT_PADDING;
    for (view in subviews) {
        if ([view isKindOfClass:[IngredientImage class]] && view.tag > 0) {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(INGREDIENT_PADDING, curYLoc);
            view.frame = frame;

            curYLoc += (INGREDIENT_PADDING + INGREDIENT_HEIGHT);
        }
    }

    // set the content size so it can be scrollable
    [ingredientsView setContentSize:CGSizeMake([ingredientsView bounds].size.width, (10 * (INGREDIENT_PADDING + INGREDIENT_HEIGHT)))];
}

問題は、最初のビューだけがタッチ イベントを処理することです。その理由はわかりません :(

手伝って頂けますか?

ありがとう

4

1 に答える 1

4

電話すると

CGPoint location = [[touches anyObject] locationInView:self];

imageView の境界に対して場所を設定しています。しかし、あなたのif文では、

if (CGRectContainsPoint([self frame], location))

場所がフレーム内にあるかどうかを尋ねています。しかし、フレームと境界は異なります。フレームは、スーパービューに相対的な座標を提供します。境界は、ビュー自体に相対的に与えます。

これを修正するには、if ステートメントを次のように変更します。

if (CGRectContainsPoint([self bounds], location))

両方の呼び出しで一貫して同じ座標系を使用しているため、問題は解決するはずです。

于 2010-10-28T17:48:19.270 に答える