我正在使用:
[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];
其中 view 是类 mainview(子类 uiview)的对象。在主视图中,我创建类 Button 的对象(子类 uiview):
[self addSubview:but];
然后在 Button 类中我制作了 UIButton,与事件 UIControlEventTouchDragInside 相关联(见文章开头)。
那么我应该如何更正我的代码,让按钮正确移动到触摸点???
附言对不起我糟糕的英语=)
最佳答案
如果您真的想坚持使用 UIControlEventTouchDragInside,请使用下面的代码片段。我个人会使用 UIPanGestureRecognizer相反。
- (IBAction)moved:(UIButton *)sender {
UITouch *touch = [touch self];
CGPoint touchPoint = [touch locationInView: sender.superview];
sender.center = touchPoint;
}
关于ios - 触摸移动 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7920273/