0

touchesMoved メソッドを使用して、画像が画面の下部に沿って誰かの指を「たどる」ようにしています。したがって、画像は指の x 位置に従いますが、y 位置は無視され、下部で垂直方向に固定されたままになります (水平方向には固定されません)。これを実装する方法はありますか?

これは私のコードです:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // create basket image that will be shown on the screen
    basketView = [[UIImageView alloc]
    initWithImage:[UIImage imageNamed:@"bucket.png"]];
    basketView.frame = CGRectMake(130.0, 412.0, 50.0, 50.0);
    [self.view addSubview:basketView];
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get current touch location
    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint point = [touch locationInView:self.view];
    // update location of the image
    basketView.center = point;
}
4

3 に答える 3

3

y 位置を維持し、x を変更します

basketView.center = CGPointMake(point.x, basketView.center.y);
于 2012-11-29T17:57:41.423 に答える
1

次のようにしてください...

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        // get current touch location
        UITouch *touch = [[event touchesForView:self.view] anyObject];
        CGPoint point = [touch locationInView:self.view];

        point.y=basketView.center.y;   //Fix 'y' of basket
        // update location of the image
        basketView.center = point;
    }
于 2012-11-29T17:59:56.200 に答える
1

In touchesMoved これを行う

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get current touch location
    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint point = [touch locationInView:self.view];
    // update location of the image
    basketView.center = CGPointMake(point.x,basketView.center.y);
}
于 2012-11-29T17:58:12.167 に答える