我使用了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);
在touchesMoved方法中用
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取当前触摸位置
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint point = [touch locationInView:self.view];
// 更新图片位置
basketView.center = CGPointMake(point.x,basketView.center.y);
}