2014-04-12 39 views
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); 
} 

我道歉,如果我的任何问题都是如此基本的。 任何帮助或建议将不胜感激。

+0

你把日志touchesBegan:withEvent:看看它是否被调用?你是否为标签设置了userInteractionEnabled为YES(标签默认为NO)? – rdelmar

+0

另外值得一提:您设置nowPoint有坐标self'的'方面,而'center'坐标一般都在上海华方面... – architectpianist

+0

非常感谢。我忘了将userInteractionEnabled设置为YES –

回答

0

我对我的问题道歉是如此基本,我已经找到了解决办法。我忘了为标签对象设置userInteractionEnabled为YES。对于这个错误我很抱歉。