ios - 触摸移动 UIButton

标签 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];

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/

相关文章:

ios - 在苹果商店发布 iPhone 应用程序时不使用我们的开发者名称

javascript - 如何覆盖onLoad?

java - java中如何拦截事件

iphone - Storyboard 中的 Root View Controller

ios - 将 UIView 转换到 SKView 时线程 1 信号 SIGABRT 崩溃

ios - 在 IOS 中使用 DatePicker 而不是键盘的两个单独的文本字段

ios - iPhone将图像上传到Facebook相册并附上评论

ios - 如何使用 UIAutomation 测试是否从我的应用程序打开了链接?

c# - 如何捕获容器中的超链接单击事件 - 松散耦合的方式?

ios - 以编程方式增加 TableView 行中自定义 View 的高度(结果也是该行的高度)