我正在使用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;
}
答案 0 :(得分:3)
保持y位置,改变x
basketView.center = CGPointMake(point.x, basketView.center.y);
答案 1 :(得分: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);
}
答案 2 :(得分: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;
}