1

タッチの場所を記録しようとしています。以下は私のコードです。私が理解している限り、iPadを縦向きに持っていると仮定すると、左上隅に触れると(0,0)の位置が得られ、右下隅は(768, 1024)になります。 . ただし、左上隅の (-6, -18) と右下隅の (761,1003) のような値を取得しています。どうやら座標がずれたようです。self.bounds をトレースすると、{{0,0}, {768, 1024}} が得られます。誰かが私にこれを説明できますか?境界 {{0,0}, {768, 1024}} の間にある x と y の値を取得したいと思います。事前にどうもありがとうございました。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     CGRect bounds = [self bounds]; 
     NSLog(@"frame: %@", NSStringFromCGRect(bounds)); // this value was traced as frame: {{0, 0}, {768, 1024}}

     UITouch* touch = [[event touchesForView:self] anyObject]; 
     location = [touch locationInView:self];
     NSLog(@"Location: %@", NSStringFromCGPoint(location));

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
     CGRect bounds = [self bounds]; 

     UITouch* touch = [[event touchesForView:self] anyObject]; 
     location = [touch locationInView:self];
     NSLog(@"Location: %@", NSStringFromCGPoint(location));

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

     CGRect bounds = [self bounds]; 

     UITouch* touch = [[event touchesForView:self] anyObject]; 
     location = [touch locationInView:self];
     NSLog(@"Location: %@", NSStringFromCGPoint(location));

}
4

3 に答える 3

0

y値の-20オフセットは、「ステータスバーが最初は非表示になっている」をYESに設定することで修正されます。iPadでプログラムを実行するたびに、x値とy値の範囲が異なるため、残りの問題はハードウェアの問題のように見えます。シミュレータでプログラムを実行しているときに、この問題は発生しませんでした。シミュレーターでは、x値の範囲は1〜767、y値の範囲は1〜1023であり、これはかなり正しいです。

于 2012-05-11T16:15:37.837 に答える
0

これが私がビューのために持っているものです:

@implementation TouchesView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUserInteractionEnabled:YES];
        [self setBackgroundColor:[UIColor colorWithRed:1.0f green:0.6f blue:0.6f alpha:1.0f]];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self];
    NSLog(@"(%.1f, %.1f)", touch.x, touch.y);
    NSLog(@"%@", NSStringFromCGPoint(touch));
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}

@end

そして、これは初期化とView Controllerへの追加です:

TouchesView *touchView = [[TouchesView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:touchView];

これは私にとってはうまくいきます。

于 2012-05-11T14:59:29.983 に答える
0

まだコメントできないので、回答を投稿する必要があります。ビューの背景色を別の色に設定して、左上隅にあるかどうかを確認してみましたか?

于 2012-05-10T19:50:27.337 に答える