我试图创建一个应该可拖动的标签。但是被拖动的事件没有被触发(用断点检查)。以下是代码。
- (IBAction)InsertText:(UIBarButtonItem *)sender {
UILabel *lblWatermark = [[UILabel alloc] initWithFrame:currentImage.frame];
lblWatermark.text = @"Copyright";
lblWatermark.userInteractionEnabled = YES;
[lblWatermark sizeToFit];
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(labelDragged:)];
[lblWatermark addGestureRecognizer:gesture];
[currentImage addSubview:lblWatermark];
}
- (void)labelDragged:(UIPanGestureRecognizer *)gesture
{
UILabel *label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];
// move label
label.center = CGPointMake(label.center.x + translation.x,
label.center.y + translation.y);
[gesture setTranslation:CGPointZero inView:label];
}
提前谢谢你,
答案 0 :(得分:5)
发现了错误。其实我也要做
userInteractionEnabled = YES
不仅是标签,还包括我在其中添加标签的Imageview。
答案 1 :(得分:0)
确保您的ViewController是UIGestureRecognizer的委托(.h文件):
@interface View : UIView <UIGestureRecognizerDelegate>
然后设置:
gesture.delegate = self;