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

在 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 回答
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 回答