我使用了touchesMoved方法,實現一張圖片隨著手指在屏幕底部滑動而移動,就是沿著x坐標移動,但是如果手指在縱向的y坐標上滑動,圖片就沒有反應,有沒有方法讓圖片既能水平移動也能上下移動?
我的代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
// 創建圖片顯示在屏幕中
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
{
// 獲取當前觸摸位置
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint point = [touch locationInView:self.view];
//更新圖片位置
basketView.center = point;
}
y坐標的不用變了,改一下x坐標的:
basketView.center = CGPointMake(point.x, basketView.center.y);