我打算自定义一个UILabel,并通过触摸手势让它移动,所以我创建了一个名为CustomLabel的类,它继承自UILabel,并实现了以下两种处理触摸事件的方法。但是,自定义标签无论如何都无法移动! 但是,当我使用 UIView、UIbutton 等更改自定义标签的父类时,它运行良好。所以任何能告诉我这种现象的原因的人。
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
if(!dragEnable)
{return;}
UITouch *touch = [touches anyObject];
beginPoint = [touch locationInView: self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!dragEnable)
{return;}
UITouch *touch = [touches anyObject];
CGPoint nowPoint = [touch locationInView:self];
float offsetX = nowPoint.x - beginPoint.x;
float offsetY = nowPoinr.y - beginPoint.y;
self.center = CGPointMake(self.center.x+offsetX, self.center+offsetY);
}
如果我的任何问题如此基本,我深表歉意。任何帮助或建议将不胜感激。
我很
抱歉我的问题太基本了,我已经找到了解决方案。我忘了为标签对象设置 userInteractionEnabled 为 YES。对于这个错误,我感到非常抱歉。