我知道如何拖动视图或对象:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
ViewMain.center = location;
}
}
但我想从触摸它的位置开始拖动该视图或图像。例如,如果我拖动(我突出显示视图并在光标上放置蓝色):
我开始拖动的那一刻,如果我将鼠标向右拖动20 px然后我希望视图也拖动20 px而不是:
也许我必须将视图的中心更改为我触摸它的位置。我怎么能这样做?
换句话说,我想做的事情就像在iPad上拖动应用程序时那样:
答案 0 :(得分:10)
float startX = 0;
float startY = 0;
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
startX = location.x - ViewMain.center.x;
startY = ViewMain.center.y;
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
location.x =location.x - startX;
location.y = startY;
ViewMain.center = location;
}
}