我需要拖动我的UIView对象。我使用这个代码,但它不工作
float oldX,oldY; BOOL dragging; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { UILabel *label = (UILabel *)touch.view; if (CGRectContainsPoint(label.frame,touchLocation)) { dragging = YES; oldX = touchLocation.x; oldY = touchLocation.y; } } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { dragging = NO; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { UILabel *label = (UILabel *)touch.view; if (dragging) { CGRect frame = label.frame; frame.origin.x = label.frame.origin.x + touchLocation.x - oldX; frame.origin.y = label.frame.origin.y + touchLocation.y - oldY; label.frame = frame; } } }
解决方法
你的代码有一些奇怪的东西。
你要求自己的位置,一些UIView可能包含你想要检查的UILabel。这就是为什么你不会把touchesXXX添加到你的UILabel子类的一个问题。
这取消了,因为您使用的是label.frame,它是根据其superview.bounds(您的父级UIView您询问的触摸位置)定义的,但这并不意味着最直接的方式来遵循什么继续
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。