Ios 触摸式移动按钮
我正在使用:Ios 触摸式移动按钮,ios,events,uiview,uibutton,touch,Ios,Events,Uiview,Uibutton,Touch,我正在使用: [button addTarget:self action:@selector(moved:) forControlEvents:UIControlEventTouchDragInside]; 功能代码移动: - (IBAction)moved:(id)sender { UIButton *t = sender; UITouch *touch = [touch self]; CGPoint touchPoint = [touch locationInView: self];
[button addTarget:self action:@selector(moved:) forControlEvents:UIControlEventTouchDragInside];
功能代码移动:
- (IBAction)moved:(id)sender {
UIButton *t = sender;
UITouch *touch = [touch self];
CGPoint touchPoint = [touch locationInView: self];
t.center = touchPoint;
}
所以,当我开始拖动按钮时,它突然出现在左上角(坐标:0;0)。我认为这是一个问题,因为CGPoint-touchPoint=[touch-locationInView:self]代码>以错误的方式工作。
我有3个类:viewController,它实现了:
view = [[MainView alloc] initWithFrame:[self.view frame] ];
[self.view addSubview:view];
其中视图是mainview类(uiview子类)的对象。在mainview中,我创建类按钮的对象(子类uiview):
然后在类按钮中,我创建UIButton,与事件uicontrolEventTouchDragins关联(请参阅本文开头)
那个么我应该如何更正我的代码,那个按钮正确地移动到接触点
另外,为我糟糕的英语道歉=)如果您真的想继续使用UIControlEventTouchDragInside,请使用下面的代码片段。就我个人而言,我会使用
Bartosz Ciechanowski-谢谢你的好主意!但看起来是同样的问题;例如,我的触摸点(x;y),但它被识别为(x-100;y-100)!??在我以前的项目中,一切都很好,但现在,我不知道发生了什么。我会直接去UIPangestureRecognitor:)
[self addSubview:but];
- (IBAction)moved:(UIButton *)sender {
UITouch *touch = [touch self];
CGPoint touchPoint = [touch locationInView: sender.superview];
sender.center = touchPoint;
}