0
我打算自定义的UILabel,并把它与触摸手势移动,所以我创建了一个名为类是CustomLabel从继承的UILabel并实现了移交触摸事件以下两种方法。但是,CustomLabel无法移动!但是,当我用UIView,UIbutton等改变CustomLabel的父类时,它运行良好。所以任何人都可以告诉我关于这个现象的原因。自定义的UILabel不能触摸手势移动
-(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);
}
我道歉,如果我的任何问题都是如此基本的。 任何帮助或建议将不胜感激。
你把日志touchesBegan:withEvent:看看它是否被调用?你是否为标签设置了userInteractionEnabled为YES(标签默认为NO)? – rdelmar
另外值得一提:您设置nowPoint有坐标self'的'方面,而'center'坐标一般都在上海华方面... – architectpianist
非常感谢。我忘了将userInteractionEnabled设置为YES –