Iphone 从触摸视图的点拖动视图
我知道如何拖动视图或对象:Iphone 从触摸视图的点拖动视图,iphone,objective-c,xcode,ipad,drag,Iphone,Objective C,Xcode,Ipad,Drag,我知道如何拖动视图或对象: - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [[event allTouches] anyObject]; if( [touch view] == ViewMain) { CGPoint location = [touch locationInView:self.view]; ViewMa
- (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上拖动应用程序时,我想做一些类似的事情:
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;
}
}
确保定位。y-星形以及东西的x侧是正确的。我忘了提到这个例子只是沿着x轴拖动一些东西。确保做location.y-startY以及东西的x面都是正确的。我忘了提到这个例子只是沿着x轴拖动一些东西。